-4

I have encountered a problem with printing 2D arrays. Here is my code, any help will be greatly appreciated.

#include<iostream>

using namespace std;

int main()
{
int NumRow, NumColumn;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 1; N_column < NumColumn; N_column++)
{
    for (int N_row = 1; N_row < NumRow; N_row++)
{
    cout << anArray[N_row,N_column];
}
}
return 0;
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Steven
  • 1
  • 2
  • `cout << anArray[N_row,N_column];` should be `cout < – JackV Aug 05 '15 at 20:35
  • 1
    That [comma operator](http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) isn't doing what you think it is... – scohe001 Aug 05 '15 at 20:39
  • 1
    Before you try arrays of arrays (C++ doesn't have "2d" arrays), you might need to think about the term "*array of arrays*", and how that would be expressed in code. Then you would also understand why e.g. `anArray[N_row,N_column]` is wrong. And you also need to find a better understanding of simple arrays in general, and how they are indexed. I suggest you check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for a good beginner book or tutorial. – Some programmer dude Aug 05 '15 at 20:40

3 Answers3

4

3 problems:

  1. Array indexes start at 0.
  2. NumColumn, NumRow are uninitialized.
  3. wrong syntax [y,j], use [i][j]

Try like this:

...
int NumRow = 2, NumColumn = 2;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 0; N_column < NumColumn; N_column++)
{
    for (int N_row = 0; N_row < NumRow; N_row++)
    {
         cout << anArray[N_row][N_column];
    }
}
...
Amit
  • 45,440
  • 9
  • 78
  • 110
  • Thank you, it really helped me to print out an answer, but one thing, why is it that I get: 1324 right next to each other instead of 1 2 and 3 4 under neath? – Steven Aug 05 '15 at 20:42
  • Not real problems but would improve the code nevertheless: 4. `NumRow` and `NumColumn` are not const. 5. `NumRow` and `NumColumn` are not used in the initialization of `anArray`. 6. `using namespace std;` – Emil Laine Aug 05 '15 at 20:43
  • 2
    @Steven Did you print a line break in between? No. The compiler can't read your mind. – Emil Laine Aug 05 '15 at 20:43
  • 1
    @Steven they are "connected" since you didn't print any separator (a space for example) and the order is "wrong" because you're iterating the outside array in the inside for and the inside arrayS in the outside for. switch the column / row variables in the `for`s to fix that – Amit Aug 05 '15 at 20:50
  • @zenith - very true, but hardly the right audience for that, don't you think? maybe after a few books :-) – Amit Aug 05 '15 at 20:51
  • @Amit Agreed, that's why it's only a comment :) – Emil Laine Aug 05 '15 at 20:51
2

You declare

int NumRow, NumColumn;

but you never assign a value to them. Use

int NumRow = 2, NumColumn = 2;

instead. Also, C-arrays start at 0, not at 1, so you must update your for-loops as well:

for (int N_column = 0; ...

    for (int N_row = 0; ...

Last, change the output statement, as multidimensional arrays need to be reached in a different way:

cout << anArray[N_row][N_column];
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
1

There are few issues in your code:

1st: You declare NumRow, NumColumn but use them without initializing them before which cause to Undefined Behaviour.

Solution: Initialized them

NumRow = 2;
NumColumn = 2;

2nd: Array syntax in the following line-

cout << anArray[N_row,N_column];

it should be

cout << anArray[N_row][N_column];

3rd: C++ arrays are zero indexed, so you should start initializing the loop control variables like following:

for (int N_column = 0; N_column < NumColumn; N_column++)
{                   ^^^
    for (int N_row = 0; N_row < NumRow; N_row++)
    {               ^^^^
        //...
mazhar islam
  • 5,561
  • 3
  • 20
  • 41