Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4.

Similar presentations


Presentation on theme: "Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4."— Presentation transcript:

1

2 Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

3 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];

4 To access an element in a 2d array, use double square brackets. Example: grades[i][j]=score; cout << grades[0][0];

5 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;

6 Initializing a 2d array Use double curly braces { { } } int identity[N][N]={ {1,0,0}, {0,1,0} {0,0,1} } ;

7 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);

8 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. Row 0Row 1Row 2Row 3

9 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)

10 // 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

11 Think about: What if you would want the class average for exam 2?

12

13 1d array of pointers In picture form: each location can hold an address. 0 1 2 3 4 5 6

14 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.

15 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.

16 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 j th element in the 0 th array of the transactions array. translated by compiler into: *(transactions[0]+j)

17 see handout.


Download ppt "Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4."

Similar presentations


Ads by Google