# A couple of questions concerning arrays

• 03-22-2006
stevedawg85
A couple of questions concerning arrays
First of all, thanks for all those who have helped me in the past and those who are about to.

I only have my prog written for the first part of my Q, the 2nd part, i need help setting up.

1) How do i store the numbers in a 5 x 3 array called Data[5][3]. I can have the user input data in the following prog, but how do i specifically call it "Data [5][3]. The reason I need to know this is becuase:

2) I need to have the program pass the array Data[ ][ ] from main ( ) to a function. My prof told me to use, float mean (const int Data [ ][ ], int, int ). I think it should look something like, mean (Data [ ][ ], 5, 3)

heres my prog
Code:

```#include <iostream> #include <iomanip> using namespace std; const int numrow = 5;                        const int numcol = 3;                                                int main () {         double data[numrow][numcol];                         for (int i = 0; i <numrow; i++) {         for (int j = 0; j < numcol; j++) {         cout << "enter grades for row # " << (i + 1)<< ": ";         cin >> data [i][j]; } }                 for (int a = 0; a < numrow; a++) {   for (int b = 0; b < numcol; b++) {         cout << setw (4) << data [a][b]; }         cout << endl; }           return 0; }```
• 03-22-2006
SlyMaelstrom
When your professor told you to call the function as func(int [][numcol], int, int), the second and third arguements aren't the array size. Those are already global constants. What he wants them to be is the specific index you want to access, I'd imagine. Say you want to access arry[2][2], you'd call
Code:

`func(arry, 2, 2);`
• 03-22-2006
Bench82
Remember that data[4][2] is the 3rd element in the 5th array.
Code:

```// Try to visualise the 2D array indexes something like this, 0: [0] [1] [2] 1: [0] [1] [2] 2: [0] [1] [2] 3: [0] [1] [2] 4: [0] [1] [2]```
Also, remember that "data[]" is an array of pointers (to arrays). Hence, the function can only deduce how many arrays there are (more accurately, it checks the number of elements of data[]). So, it cannot deduce the size of each individual "sub" array (For lack of a better term) - so you must specify that explicitly.

I believe you may have misunderstood his instructions, but the closest to what you described would be something like:
Code:

```void output(int arr2D[][3], int a) // some number of arrays, as defined by 'a', of size 3 {     for (int i=0; i!=a; ++i)     {         for (int j=0; j!=3; ++j)             cout << arr2D[i][j] << " ";         cout << endl;     } }```
Personally, I would use STL vectors to do this instead (since vectors keep track of dimensions, life is made much easier).. as you can see, this sort of method is messy.

But if you've been told not to use containers, and have to use arrays, then a better way to do this would be to create a Matrix class, as a wrapper for your 2D array (in other words, make your own container).