Adam
Adam

Reputation: 10066

Printing 2d array, C++

I'm coming from Matlab where I could just right-click a 2D matrix to see an excel-like visualization of what's inside it. Now I'm working in C++ (Visual Studio) and am looking for something similar for 2d arrays, eg a visualization like:

myArray = [ 1 2 3
            4 5 6
            7 8 9 ]

What the best way to do something like this in C++?

Upvotes: 0

Views: 2555

Answers (2)

SU3
SU3

Reputation: 5409

First of all, this is not correct syntax for declaration/initialization of arrays in C++. I don't know if there's any IDE that will visualize an array for you, but you can do it in code with just two for loop like this. This also shows the correct syntax for arrays.

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
  int myArray[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
  };

  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j)
      cout << myArray[i][j] << ' ';
    cout << endl;
  }

  return 0;
}

Or, if you want to make it convenient for debugging, you can define a preprocessor directive like this

#include <iostream>
#include <iomanip>

using namespace std;

#define test_array(name,ni,nj,w)      \
  cout << #name " = {\n";             \
  for (int i=0; i<ni; ++i) {          \
    cout << "  ";                     \
    for (int j=0; j<nj; ++j)          \
      cout << setw(w+1) << myArray[i][j]; \
    cout << endl;                     \
  }                                   \
  cout << '}' << endl;

int main(int argc, char **argv)
{
  int myArray[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
  };

  test_array(myArray,3,3,2)

  return 0;
}

The fourth argument will allow you to set column width, so you can have nice alignment.

Upvotes: 2

jaggedSpire
jaggedSpire

Reputation: 4523

If you're referring to an output to the console, then you can simply write a function that takes in an array and its size, and prints that information to the console using cout.

However, if you are using this for debugging, there is another option for data visualization: mousing over the variable when it hits a breakpoint you have set in the program. There are a few caveats to this: If I'm recalling correctly, Visual Studio will not automatically display the entire range of elements in the mouseover visualization if you have dynamically allocated your array, because the IDE does not know the size of the array to properly display it. This problem will be resolved if you use statically sized arrays, or any of the STL container classes, one of which--vector--is typically recommended for use in place of dynamically allocated arrays, as they self-manage their memory and provide several useful functions for manipulating their contained sets of data.

As you progress farther into C++, you may wind up creating your own class to hold matrices, or downloading a library that has that class implemented. If you want to view the contained information in a way you prefer in this eventual case, you may also use one of Visual Studio's ways of customizing the visualization of data. I like Natvis because it's simple to learn and gets results quickly.

Upvotes: 1

Related Questions