Multiple-Subscripted Array What it is How to use it How to declare it How to initialize it.

  • Published on
    21-Dec-2015

  • View
    215

  • Download
    2

Transcript

  • Slide 1
  • Multiple-Subscripted Array What it is How to use it How to declare it How to initialize it
  • Slide 2
  • Multiple-Subscripted Arrays Use more than one subscript (can have up to 12) Double-subscripted array (2 subscripts)
  • Slide 3
  • Double-subscripted array good for representing tables in memory one subscript is the row second subscript is the column
  • Slide 4
  • Double-subscripted array a double-subscripted array with 3 rows and 4 columns 3 by 4 array row 0 row 1 row 2 column 0 column 1 column 2 column 3 a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[2][0] a[1][1]a[1][2]a[1][3] a[2][1]a[2][2]a[2][3] array name = a row subscript column subscript
  • Slide 5
  • Referencing elements in a double-subscripted array To set the element in row 2 and column 3 to the value 4 use: a [2] [3] = 4; name of the array row subscript column subscript
  • Slide 6
  • Declaring a 3 by 4 array int a [3] [4]; must tell compiler name, size (2 sizes) and type all elements of the array must be the same type any data type is fine (int, float, char, etc.)
  • Slide 7
  • Declaring a double- subscripted array Must use constants to declare row and column sizes. int a [3] [4]; char letters [2] [26]; #define ROW 3 #define COLUMN 4 int a [ROW] [COLUMN]; const int row = 3; const int column = 4; int a [row] column];
  • Slide 8
  • Initializing a double-subscripted array const int row = 3; const int column = 4; int a [row] [column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { a [i][j] = 0; }
  • Slide 9
  • Initializing a double-subscripted array with declaration const int row = 3; const int column = 4; int a[row] [column] = {{1,2,3,4}, {1,2,3,4}, {1,2,3,4}}; 2341 1 1 2 2 3 3 4 4

Recommended

View more >