I tried fixing these errors but to no avail. Can someone please explain to me what I need to do to fix them?

#include <iostream>
using namespace std;

int main()

int arr_3d[4][3] = {{1},
{1, 2},
{1, 2, 3},
{1, 2, 3, 4}};
int row, col;
for (row = 0; row < 4; ++row)
{
cout << endl;
for (col = 0; row < 3; ++col)
cout << setw(3) << arr_3d[row][col];
}
return 0;
}

ERRORS

1>------ Build started: Project: TempConverter7.cpp, Configuration: Debug Win32 ------
1> TempConverter7.cpp.cpp
1>c:\users\mjlhr\documents\visual studio 2010\projects\tempconverter7.cpp\tempconverter7.cpp\tempconverter7.cpp.cpp(4): error C2015: too many characters in constant
1>c:\users\mjlhr\documents\visual studio 2010\projects\tempconverter7.cpp\tempconverter7.cpp\tempconverter7.cpp.cpp(4): error C2006: '#include' : expected a filename, found 'constant'
1>c:\users\mjlhr\documents\visual studio 2010\projects\tempconverter7.cpp\tempconverter7.cpp\tempconverter7.cpp.cpp(4): fatal error C1083: Cannot open include file: '': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Aside from the missing left brace on line 5, there are a few other problems I can see straight off the bat.

For starters: In order to use setw, you'll need to #include <iomanip>.
Then there's your '3D' array; which from your initialisers, looks more like a 4D array. So you need to set the dimensions of your array accordingly. What you've actually got there is a 4x4 array.
What are you trying to do here??

Then there is the matter of the break condition in your 2nd for loop at line 14. Surely that should be using col, not row:
for(col=0;col<4;++col) << Note, col < 4 rather than < 3, because you are currently using a 4x4 array!
With the 2nd for loop in its current state (using row in the break condition), you're going to end up overstepping the bounds of your array and/or getting a segfault at runtime.

No, there's only a single statement under the 2nd for loop, so there is no need for enclosing braces there! But without the correct indentation in the OPs code, it does initially look as if there should be an opening brace! :)

int arr_3d[4][3] means make an array with 4 rows and three columns. On line 9 you have {1, 2, 3, 4}. How many columns are in that row?

This may be what you were looking for ?

#include <iostream>

using namespace std;

int const ROWS = 2;
int const COLS = 3;

void print( int a[][ROWS][COLS], int numTables );
void print( int a[ROWS][COLS] );

int main()
{

    int ary3d[][ROWS][COLS] =
    {
        {
            {1,2,3},
            {4,5,6}
        },

        {
            {7,8,9},
            {0,1,2}
        },

        {
            {3,4,5},
            {6,7,8},
        },

        {
            {9,0,1},
            {2,3,4}
        }
    } ;

    int numTables = sizeof( ary3d ) / sizeof( ary3d[0] );

    print( ary3d, numTables );
    cout << "\nnumTables = " << numTables << endl;

    return 0;
}


void print( int a[ROWS][COLS] )
{
    for( int i = 0; i < ROWS; ++ i )
    {
        for( int j = 0; j < COLS; ++ j )
            cout << ' ' << a[i][j];
        cout << endl;
    }
}

void print( int a[][ROWS][COLS], int tables )
{
    for( int i = 0; i < tables; ++i )
    {
        cout << "Table " << i+1 << endl;
        print( a[i] ); // print table i
        cout << endl;

    }
}

Edited 2 Years Ago by David W: fixed typo ... added text

This article has been dead for over six months. Start a new discussion instead.