-2

I'm simply trying to print the first seven rows of a two dimensional array. Whats wrong with my code?

I keep getting the seven rows in one long row instead of their own individual rows. Everything I've googled/read leads me to believe that my code is correct, but it still doesn't work. I've tried using \n and endl;, but that sends it down, each integer on its own line instead of the lines ending at 15.

//these are the declarations
int row = 12;
int col = 15;
int someArray[row][col];
for (row = 0; row < 12; row++)
    for (col = 0; col < 15; col++)
        someArray[row][col] = col;

//this is where my problem is
for (int row = 0; row < 7; row++)
    for (int col = 0; col < 15; col++)
        cout << someArray[row][col] << " "

I need the output to be the first seven rows of this array, separated into their respective lines of output, but upon compilation and execution, the output is one long line of these seven rows.

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
Birch Poirier
  • 11
  • 1
  • 6
  • 2
    You never print any newline characters.. – Jesper Juhl Jun 09 '19 at 16:54
  • I've tried a newline character inside the last pair of quotations, but the output then gave every integer its own line, not the whole row. – Birch Poirier Jun 09 '19 at 17:00
  • 1
    This `nt someArray[row][col];` is not valid C++ - the dimensions of arrays must be compile-time constants. –  Jun 09 '19 at 17:00
  • Print newline character in the end of the first loop i.e after the second loop. – muaz Jun 09 '19 at 17:04
  • Consider enabling more compiler checks: (2x) error: ISO C++ forbids variable length array ‘someArray’ [-Werror=vla] (1x) warning: declaration of ‘row’ shadows a previous local [-Wshadow] (1x) warning: declaration of ‘col’ shadows a previous local [-Wshadow] – 2785528 Jun 09 '19 at 17:42

2 Answers2

0

Add new line character:

cout << endl;

Or

cout << "\n";

Now your code should look like:

for (int row = 0; row < 7; row++){
    for(inr col = 0; col < 15; col++){
         cout << someArray[row][col] << " ";
    }
    cout << "\n"; // or cout << endl;
}
Szymek G
  • 31
  • 6
-1

Add a newline character:

for (int row = 0; row < 7; row++){
    for (int col = 0; col < 15; col++){
        cout << someArray[row][col] << " ";
    }
    cout << endl;
}
DSC
  • 1,153
  • 7
  • 21