Multidimensional Arrays INFSY 440 Spring 2004 Lecture 7

  • Slides: 17
Download presentation
Multidimensional Arrays INFSY 440 Spring 2004 Lecture 7

Multidimensional Arrays INFSY 440 Spring 2004 Lecture 7

Two-Dimensional Arrays A two-dimensional array is used to represent a table with rows and

Two-Dimensional Arrays A two-dimensional array is used to represent a table with rows and columns – Each item must be of the same data type – Access the array by specifying the row and column indices of the item in the table row column int quiz_score[15] [3];

Two-Dimensional Arrays How would you reference: 1. 3 rd student’s second quiz? 2. 10

Two-Dimensional Arrays How would you reference: 1. 3 rd student’s second quiz? 2. 10 th student’s first quiz? Answer: 1. quiz_score[2][1] 2. quiz_score[9][0]

Two-Dimensional Arrays Two-dimensional arrays are useful for: – representing board games chess tick-tac-toe Scrabble

Two-Dimensional Arrays Two-dimensional arrays are useful for: – representing board games chess tick-tac-toe Scrabble – computer graphics where the screen is thought of as a two-dimensional array

Two-Dimensional Arrays Sum the rows int num_students = 15, num_quizzes = 3, total=0; double

Two-Dimensional Arrays Sum the rows int num_students = 15, num_quizzes = 3, total=0; double average; int quiz_score[num_students] [num_quizzes]; for(int i=0; i<num_quizzes; i++) { total = total + quiz_score[3][i]; //sum row 3 } average = total / num_quizzes;

Two-Dimensional Arrays Sum the columns int num_students = 15, num_quizzes = 3, total=0; double

Two-Dimensional Arrays Sum the columns int num_students = 15, num_quizzes = 3, total=0; double average; int quiz_score[num_students] [num_quizzes]; for(int i=0; i<num_students; i++) { total = total + quiz_score[i][1]; //sum 1 st column } average = total / num_students; cout << “Class average for the quiz is “<<average<<endl;

Two-Dimensional Arrays Initialize a small array int num_students = 2, num_quizzes = 3; int

Two-Dimensional Arrays Initialize a small array int num_students = 2, num_quizzes = 3; int quiz_score[num_students] [num_quizzes] = { {100, 98, 99}, {87, 92, 75} };

Two-Dimensional Arrays How would you initialize this table? int num_students = 15, num_quizzes =

Two-Dimensional Arrays How would you initialize this table? int num_students = 15, num_quizzes = 3; int quiz_score[num_students] [num_quizzes];

Two-Dimensional Arrays Most processing of data stored in 2 dimensional array involves – processing

Two-Dimensional Arrays Most processing of data stored in 2 dimensional array involves – processing by row – or processing by column

Two-Dimensional Arrays Row Processing for (row = min. Row; row < row. Length; row++)

Two-Dimensional Arrays Row Processing for (row = min. Row; row < row. Length; row++) for (col = min. Col; col < col. Length; col++) …… //Whatever processing is required

Two-Dimensional Arrays Column Processing for (col = min. Col; col < col. Length; col++)

Two-Dimensional Arrays Column Processing for (col = min. Col; col < col. Length; col++) for (row = min. Row; row < row. Length; row++) …… //Whatever processing is required

Passing Two-Dimensional Arrays as Parameters One-dimensional Array – Formal parameters in a function array

Passing Two-Dimensional Arrays as Parameters One-dimensional Array – Formal parameters in a function array base address in memory array size void some_function (float list[ ], int size) { }

Passing Two-Dimensional Arrays as Parameters Two_dimensional Array – Formal parameters in a function array

Passing Two-Dimensional Arrays as Parameters Two_dimensional Array – Formal parameters in a function array base address in memory omit the size of the first dimension include the size of the second dimension void another_function (int list[ ][4], int size) { }

Multidimensional Arrays C++ does not place a limit on the number of dimensions that

Multidimensional Arrays C++ does not place a limit on the number of dimensions that an array can have.

Multidimensional Arrays Example: – Chain of department stores – Monthly sales figures kept for

Multidimensional Arrays Example: – Chain of department stores – Monthly sales figures kept for each item by store – Three pieces of information per item month sold store where purchased, item number

Multidimensional Arrays const int NUM_ITEMS = 100; const int NUM_STORES = 10; typedef int

Multidimensional Arrays const int NUM_ITEMS = 100; const int NUM_STORES = 10; typedef int Sales. Type[NUM_STORES][12][NUM_ITEMS]; Sales. Type sales; int item, store, month; int number_sold, current_month; //number of components in sales is 12, 000 // (10 x 12 x 100)

Multidimensional Arrays for(item = 0; item < NUM_ITEMS; item++) { number_sold = 0; for(store

Multidimensional Arrays for(item = 0; item < NUM_ITEMS; item++) { number_sold = 0; for(store = 0; store < NUM_STORES; store++) for(month = 0; month <= current_month; month++) number_sold+=sales[store][month][item]; cout << “Item #” << item << “ Sales to date = “ << number_sold << endl; }