1 / 6

Multidimensional Arrays

Multidimensional Arrays. Name the collection. Number the elements. HW 0. HW 1. HW 2. HW 3. HW 4. student 0. 10. 7. 8. 9. 5. student 1. 5. 6. 8. 3. 9. row. student 2. 7. 4. 6. 9. 10. column. Definition : An ordered collection of values of

keren
Download Presentation

Multidimensional Arrays

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. Multidimensional Arrays Name the collection Number the elements HW 0 HW 1 HW 2 HW 3 HW 4 student 0 10 7 8 9 5 student 1 5 6 8 3 9 row student 2 7 4 6 9 10 column Definition: An ordered collection of values of identical type Example: array score (2 dimensional) Scores for 3 students on 5 homeworks C expressions: score[2][0] is 7 2*score[1][3] is 6 142Q -1

  2. Declaring a 2D array score is a 2 dimensional array of integers of size 80 by 6 score is an array of size 80 of type array of size 6 of integers type name [row_size][line_size]; e.g. #define MAX_STUDENTS 80 #define MAX_HWS 6 ... int score[MAX_STUDENTS][MAX_HWS]; or (think of score as a 1D array of 1D arrays) score[0][0], score[0][1], ..., score[79][5] are the elements of the array or score[0], score[1], ..., score[79] are the rows of the array and each row is itself an array 142 Q -2

  3. Using 2D Arrays nstudents nhws j score 0 1 2 3 4 5 i 0 0 1 2 3 ? ? 1 4 5 6 7 ? ? 2 8 9 10 11 ? ? 3 ? ? ? ? ? ? Reading a 2D array scanf("%d%d",&nstudents,&nhws); if (nstudents <= MAX_STUDENTS && nhws <= MAX_HWS) { for(i=0; i<nstudents; i++) for(j=0; j<nhws; j++) scanf("%d",&score[i][j]); } Input 3 4 0 1 2 3 4 5 6 7 8 9 10 11 142 Q -3

  4. 2D Arrays as parameters can omit cannot omit formal parameter (need [ ] and dimensions) array element (just like a standard variable) actual parameter (no & or [ ]) void read2D (int a[MAX_STUDENTS][MAX_HWS], int nstudents, int hws) { int i,j; for(i=0; i<nstudents; i++) for(j=0; j<nhws; j++) scanf("%d",&a[i][j]); } int main(void) { int score[MAX_STUDENTS][MAX_HWS]; int nstudents, nhws; scanf("%d%d",&nstudents,&nhws); if (nstudents<=MAX_STUDENTS && nhws <= MAX_HWS) read2D(score,nstudents,nhws); .... } 142 Q -4

  5. 2D Arrays and Pointers Recall 1D Array Check the arithmetic on pointers array name is a pointer to the first element of the array a[i] same as *(a+i) For a 2D Array array name is a pointer to the first row a[i][j] is equivalent to *(*(a+i)+j) a + i : move by i rows from a[0][0] *a + j: move by j elements from a[0][0] 142 Q -5

  6. 3 rows and 3 columns int a[3][3]; a a+1 a+2 *a+1 1 3 5 4 2 9 8 0 7 row 1 row 2 row 3 test it on the computer in the memory What is **a ? 1 (same as a[0][0]) *(*a+1) ? 3 (same as a[0][1]) **(a+2) ? 8 (same as a[2][0]) 142 Q -6

More Related