Lab11-2D-Arrays_m.docx - ICS 103 Computer Programming in C Lab#12 Two Dimensional Arrays Objective Practice with 2-Dimensional Arrays Declaration A 2-D

Lab11-2D-Arrays_m.docx - ICS 103 Computer Programming in C...

This preview shows page 1 - 2 out of 5 pages.

ICS 103: Computer Programming in C Lab #12: Two Dimensional Arrays Objective: Practice with 2-Dimensional Arrays Declaration A 2-D array can be visualized as a matrix or a table of data elements consisting of rows and columns. Declaration is similar to 1-D array except an additional number is provided in bracket representing the size of the second dimension. Example : int grades[100][6] The first size indicates the number of rows and the second the number of columns The indexing starts from 0. In the above example the row indexes are from 0 to 99 and column indexes are from 0 to 5. Initialization A 2-D array can be initialized at declaration by nesting the braces for each row in another braces as shown below: int x[3][2] = { {17,22}, {-33,65}, {14,31}} In the above, if the data is not enough, the remaining elements are set to zero. If number of values is more than the size of each row, then we get the compilation error “too many initializers”. Data can also be read into a 2-D array using nested loops. This can be row-wise or column-wise as shown below: Row-wise for (row=0; row<rsize, row++) for (col=0; col<csize; col++) scanf(“%d”, &a[row][col]); Column-wise
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read all 5 pages?

  • Summer '14
  • Darwish

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture