170 likes | 220 Views
2d Arrays and arrays of pointers. Defining a 2d Array. A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];. When would you use a 2d array. Suppose you have multiple elements of data about several people (or records). Example:
E N D
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
When would you use a 2d array Suppose you have multiple elements of data about several people (or records). Example: You have 8 grades each for 30 students. Define a 2d array to store the grades. double grades[30][8];
To access an element in a 2d array, use double square brackets. Example: grades[i][j]=score; cout << grades[0][0];
Iterating through a 2d array Use a nested loop. Example: create an NxN identity matrix. const in N=10; int identity[N][N]; for (int i=0; i<N; i++) for (int j=0; j<N; j++) if (i==j) identity[i][j]=1; else identity[i][j]=0;
Initializing a 2d array Use double curly braces { { } } int identity[N][N]={ {1,0,0}, {0,1,0} {0,0,1} } ;
Passing a 2d array to a function RULE: You can leave out the number of rows, but you MUST specify the number of columns. example: void print_array(int arr[][COLS], int numrows);
In Memory Although we think of a 2d array as a matrix, memory is 1d, and therefore a multidimensional array is actually stored as a 1d array in memory. It is stored in row-major order.
Relating 2d arrays to pointers (e.c.) int arr[numrows][numcols]; arr[0] refers to the address of row 0. That is, it is of type 1d array (alternatively int*). So, *arr[0] is the contents of the first element of the first row (i.e. arr[0][0]). arr[i][j] is translated by the compiler into: *(arr[i]+j) *(*(arr+i)+j)
// USING 2D ARRAYS const int NUM_STUDENTS = 30; const int NUM_SCORES = 8; double scores[NUM_STUDENTS][NUM_SCORES]; double total, average; // for each student (row) for (int i=0; i<NUM_STUDENTS; i++) get_scores(scores, NUM_STUDENTS); // read in the students’ scores for (int i=0; i<NUM_STUDENTS;i++) // calculate each student’s average { total=0; for (int j=0; j < NUM_SCORES; j++) { total+=scores[i][j]; } // end for j average = total/NUM_SCORES; cout << “average for student” << i+1 << “is” << average << endl; } // end for I
Think about: What if you would want the class average for exam 2?
1d array of pointers In picture form: each location can hold an address.
When to use • You have 30 students, but each takes a different number of exams. You can dynamically allocate an array of scores for each student. • You have 1000 accounts, and each account has its own transaction list. Define an array of pointers to char, and let each account have a dynamically allocated array of transactions.
How can we do that?? char *transactions[NUM_ACCOUNTS]; // transactions is an array of type pointer to char transactions[0] = new char [transnumber]; // now, the first location of the transactions array points to the beginning of a dynamically allocated array.
Using the array of pointers Since a pointer is allowed to be used with subscripting [ ], we can use the array of pointers as if it were a 2d array. So, transactions[0][j] refers to the jth element in the 0th array of the transactions array. translated by compiler into: *(transactions[0]+j)