1 / 17

2d Arrays and arrays of pointers

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:

tonyhill
Download Presentation

2d Arrays and arrays of pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2d Arrays and arrays of pointers

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

  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.

  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. arrays of pointers

  13. 1d array of pointers In picture form: each location can hold an address.

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

  17. see handout.

More Related