1

I am trying to make a function that prints out two dimensional arrays. I did one that prints out 1d arrays.

 #include <iostream>
 using namespace std;

void printArray (int theArray[],int sizeOfArray);
int main ()

{
int array1[3] = {1,3,7};
int array2[5] = {123,5,23,2,324};

printArray(array1, 3);
printArray(array2, 5);

}  

void printArray (int theArray[],int sizeOfArray){


for (int x=0; x<sizeOfArray; x++) {
    cout<<theArray[x] <<" ";
}
cout<<endl;  
}

I wrote these codes for printing out 2d arrays but I failed.

#include <iostream>
using namespace std;

void printArray (int theArray[][],int sizeOfRow, int sizeOfCol);

int main ()

{
int array[2][3] = {{1,3,7},{5,3,2}};

printArray(array, 2,3);


}

void printArray (int theArray[][],int sizeOfRow, int sizeOfCol){


for (int x=0; x<sizeOfRow; x++) 

    for (int y=0; y<sizeOfCol; y++) {

        cout<<theArray[x][y] <<" ";
}
cout<<endl;
}

My compiler says array has incomplete element type 'int[]'. What are the right codes for printing out 2d arrays?

Fraukn
  • 83
  • 1
  • 2
  • 6
  • 1
    Your loop is fine, but I think the problem here is the need to specify the column size on `theArray` – Lews Therin May 02 '13 at 20:42
  • You could include the [prettyprinter](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) and say `cout << theArray;` :-) – Kerrek SB May 02 '13 at 20:44
  • @Fraukn [Higly recommended reading.](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – jrok May 02 '13 at 21:06
  • "The logic is that arrays are not pointers. int theArray[] doesn't mean you're passing an array (as you can't pass them by value, anyway) but a pointer - it's a syntactic sugar for int* (a rather bitter and old sugar that stems from C). int theArray[][], if valid, would mean the same as int (*theArray)[] (a pointer to array). The problem is, int[] is an incomplete type and the compiler wouldn't know what the pointer would point to. Thus, you need to specify the size and tell it that it should point to an array of three ints: int (*theArray)[3], the same as int[][3]" [written by jrok] – Lews Therin May 02 '13 at 21:08
  • @LewsTherin Your answer wasn't wrong, though, no need to delete it. – jrok May 02 '13 at 21:09

3 Answers3

4

Since array size has to be known during compile time, you can use templates to provide flexibility to the function.

template< typename T, size_t N, size_t M >
void printArray( T(&theArray)[N][M]  ) {
    for ( int x = 0; x < N; x ++ ) {
        for ( int y = 0; y < M; y++ ) {
            cout << theArray[x][y] << " ";
        }
    }
}

printArray( array ); 

this is much nicer since there is no need to pass the dimension of array anywhere, also work for 3D, 4D array by adding extra parameter to the template.

yngccc
  • 5,594
  • 2
  • 23
  • 33
  • 1
    You don't need to pass `sizeOfCol` then since it is implicitly "passed" as `N`, unless you want to print only part of array. – n0rd May 02 '13 at 20:50
  • Sadly I do think this work for arrays which are determined by reading a file or user input as the size is not known at compile time. –  Jan 29 '21 at 19:58
1

For a multi-dimensional array you need to declare the size of the array in your function declaration.

So your method needs to look like:

void printArray (int theArray[][3],int sizeOfRow, int sizeOfCol)

Then it should compile and work.

canhazbits
  • 1,664
  • 1
  • 14
  • 19
0

A tip for printing out two dimensional arrays -- it is easier to use the variables r and c (i.e. for row and columns) to visualize how the array is being printed out.

As noted above, the column size is REQUIRED by the language when passing 2d arrays.

The function header should look as follows:

void printArray (int theArray[][3],int sizeOfRow, int sizeOfCol)
MrPickle5
  • 522
  • 4
  • 9
  • 31