1 / 13

Chapter 10 –Multi-Dimensional Numeric Arrays

Chapter 10 –Multi-Dimensional Numeric Arrays. Name of array. Two sets of [ ] so two dimensional. Type of array. Multidimensional Arrays. Visual image of matrix or table for two-dimensional array Subscript for each dimension Declared with value for each dimension int b[2] [3];.

bijan
Download Presentation

Chapter 10 –Multi-Dimensional Numeric 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. Chapter 10 –Multi-Dimensional Numeric Arrays

  2. Name of array Two sets of [ ] so two dimensional Type of array Multidimensional Arrays • Visual image of matrix or table for two-dimensional array • Subscript for each dimension • Declared with value for each dimensionint b[2] [3]; Number of elements 2*3 = 6 Lesson 10.1

  3. Initializing • Initialized by row • Example: int b[2] [3] ={51, 52, 53, 54, 55, 56}; • Elements would be:b[0] [0] = 51 b[0] [1] = 52 b[0] [2] = 53b[1] [0] = 54 b[1] [1] = 55 b[1] [2] = 56 Remember – subscripts begin at zero! Lesson 10.1

  4. Initializing • Can use braces to separate rows int c [4] [3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}}; Advantage is visual table! Lesson 10.1

  5. int c[ ] [3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; If far left dimensionsize left blank, it isdeclared implicitlyby values given – inthis case 4 rows. Initializing int c[4] [3] = {{1,2}, {4, 5, 6}, {7}, {10,11,12}}; If values left outof row, implicitly initialized to zero. c[2] [2] = 0 Lesson 10.1

  6. Creates new line or row Printing for (j = 0; j < 2; j++) { for (k = 0; k < 3; k++) { cout << b[j] [k]; } cout << endl; } • Usually use nested for loop • Two dimensions, two loops • First loop controls rows • Second loop controls columns Lesson 10.1

  7. Read number of columns and rows from file prior to array elements Reading Array From File infile >> num_rows >> num_cols; for (j = 0; j < num_rows; j++) { for (k = 0; k < num_cols; k++) { infile >> a[j] [k]; } } Lesson 10.2

  8. Storage of Two-Dimensional Arrays • Fills row by row • If have fewer elements, then storage to the right of those filled un-initialized elements • Three and greater dimensions • Far right subscript increments first • Other subscripts increment in order from right to left Lesson 10.2

  9. Storage If file gave number of rows as 3 and number of columns as 4 then have non-contiguous storage. array[5] [9] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 2 3 4 * * * * * 2 4 6 8 * * * * * 3 5 7 9 * * * * * * * * * * * * * * * * * * * * * * * Lesson 10.2

  10. array size e[I] [J] [K] array element, e[x] [y] [z] array size e[2] [3] [4] array element, e[0] [1] [2] Storage Location • Formula to locate arrays element's position in memory sequence location = x * (J * K) + y * (K) + z +1 sequence location = 0 * (3 *4) + 1 * (4) + 2+1 = 7 Lesson 10.2

  11. Passing Array to Function Declaration void funcName (type, type, type[ ] [max_cols]); Call funcName (rows, cols, arrayName); void funcName (type r, type c, type array[ ] [max_cols]); Header Lesson 10.2

  12. const Qualifier • Place in front of array name in function declaration and header • Assures that array is not modified in function void funcName (type, type, const type [ ] [ col ]); Declaration Lesson 10.2

  13. Summary Learned how to: • Initialize multidimensional arrays • Print multidimensional arrays • Read a file for array element values • Visualize storage • Pass an array to a function

More Related