Comp 1672 Class Notes for Jan 17, 2002 Using 2D arrays in a program Example of declaring: int arr[5][3]; // creates space in memory for 5 rows, 3 columns of ints Examples of declaring and initializing // The next line declares and initializes a 2-row, 4 column array of floats float arr[][] = { {1.1 , 2.2, 0, 0 }, {0, 0, 0, 0} }; // Initialers omitted are set to 0. In fact, the next line has the // Identical outcome to the previous line float arr[4][2] = { {1, 1} }; See 2Darray program from Wednesday for examples of how to manipulate individual rows, individual columns, and the entire array Remember that even though our pictures of arrays look two-dimensional, the computer stores memory in a long linear (1-dimensional) list. So, the computer has to do some calculations to make it look to us like it's really 2-D. It stores the data row-by-row, exactly in the order that we initialize 2-D arrays. So, when we access arrays with an expression like testscores[1][2], it must calculate how many elements to skip. To access the element testscores[i][j], look at element number i*num_cols+j. You can use this technique in your code as well, to store a 2-D array using a single dimensional array. The previous paragraph shows us that when we pass a 2-D array to a function, we will have to also pass the number of columns. This is done as part of the array parameter, as in the following prototype void print_array(int arr[][num_cols]); Note that you do not need to fill in the number of rows, although if you want to you may fill that in as well, for documentation purposes.