Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.

Similar presentations


Presentation on theme: "Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular."— Presentation transcript:

1 Computer Science 1620 Multi-Dimensional Arrays

2 we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular student int grades[4]; store a list of hockey players player players[50]; however, sometimes data has more structure than this

3 Example: Store the assignment grades for multiple students essentially a 'table' of information 18 16 9 12 13 15 17 16 14 5 2 10 Mark 1 Mark 2 Mark 3 Mark 4 Student 1 Student 2 Student 3

4 Example: Store the assignment grades for 5 different students Solution 1: Store 5 different arrays int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4];

5 Example: Store the assignment grades for 50 different students Solution 1: Store 50 different arrays int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; int grades6[4]; int grades7[4]; int grades8[4]; int grades9[4]; int grades10[4]; int grades11[4]; int grades12[4]; int grades13[4]; int grades14[4]; int grades15[4]; int grades16[4]; int grades17[4]; int grades18[4]; int grades19[4]; int grades20[4]; int grades21[4]; int grades22[4]; int grades23[4]; int grades24[4]; int grades25[4]; int grades26[4]; int grades27[4]; int grades28[4]; int grades29[4]; int grades30[4]; int grades31[4]; int grades32[4]; int grades33[4]; int grades34[4]; int grades35[4]; int grades36[4]; int grades73[4]; int grades38[4]; int grades39[4]; int grades40[4]; int grades41[4]; int grades42[4]; int grades43[4]; int grades44[4]; int grades45[4]; int grades46[4]; int grades47[4]; int grades48[4]; int grades49[4]; int grades50[4];

6 Multi-Dimensional Arrays Solution 2: 'flatten' the table into one array 18 16 9 12 13 15 17 16 14 5 2 10 int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10};

7 what happens when I want the second mark of the second student? we must translate index in array = row * (# of columns) + column index in array = 1 * (4) + 1 = 5 Multi-Dimensional Arrays int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; 18 16 9 12 13 15 17 16 14 5 2 10

8 A Table of Information what do you notice about this declaration? int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; grades1, grades2, grades3, grades4, and grades5 all have the same type an array of 4 integers Question: since they all have the same type, can I store them all in the same array? have an array of array of 4 integers?

9 2D Arrays you can declare an "array of arrays" this creates 3 arrays, all of length 4 when a 2D array is declared, it is often referred to using table syntax each array is often referred to as a row the number of elements in each row refers to the number of columns in the structure int a[3][4];

10 Declaring a 2D array use two subscript notations first subscript: number of rows second subscript: number of columns int a[3][4]; 3 rows 4 columns

11 Indexing a 2D array use the same subscript notation as for a single-dimensional array use two! remember: indices always start at 0, not 1 a[1][2] = 17; refers to second row refers to third column 18 16 9 12 13 15 17 16 14 5 2 10

12 2D Arrays vs. Multiple Arrays store the assignment grades for 50 different students int grades1[4]; int grades2[4]; int grades3[4]; int grades4[4]; int grades5[4]; int grades6[4]; int grades7[4]; int grades8[4]; int grades9[4]; int grades10[4]; int grades11[4]; int grades12[4]; int grades13[4]; int grades14[4]; int grades15[4]; int grades16[4]; int grades17[4]; int grades18[4]; int grades19[4]; int grades20[4]; int grades21[4]; int grades22[4]; int grades23[4]; int grades24[4]; int grades25[4]; int grades26[4]; int grades27[4]; int grades28[4]; int grades29[4]; int grades30[4]; int grades31[4]; int grades32[4]; int grades33[4]; int grades34[4]; int grades35[4]; int grades36[4]; int grades73[4]; int grades38[4]; int grades39[4]; int grades40[4]; int grades41[4]; int grades42[4]; int grades43[4]; int grades44[4]; int grades45[4]; int grades46[4]; int grades47[4]; int grades48[4]; int grades49[4]; int grades50[4];

13 2D Arrays vs. Multiple Arrays store the assignment grades for 50 different students int grades[50][4];

14 2D Arrays vs. 1D Arrays what happens when I want the second mark of the second student? we must translate index in array = row * (# of columns) + column index in array = 1 * (4) + 1 = 5 Multi-Dimensional Arrays int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; 18 16 9 12 13 15 17 16 14 5 2 10

15 2D Arrays vs. 1D Arrays what happens when I want the second mark of the second student? with a 2D array, each index is separate Multi-Dimensional Arrays int a[3][4]; // code for initializing a[1][1] = 15; // this sets the 2 nd student's 2 nd mark to 15 18 16 9 12 13 15 17 16 14 5 2 10

16 Initializing a 2D array 2D arrays can be initialized just as a 1D array can int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; use braces to separate individual rows row 1row 2row 3 int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} };

17 int one[12] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[3][4] = { 18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10 }; Initializing a 2D array inner braces can be omitted: when inner braces omitted, C++ fills table one row at a time, from left to right 18 16 9 12 13 15 17 16 14 5 2 10

18 Initializing a 2D array the first subscript value can be omitted, just as with single-dimension arrays the second subscript value cannot be omitted int one[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10}; int two[][4] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; int three[][] = { {18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10} }; Error! Must include column size.

19 2D arrays and loops our previous array discussions showed strong relationship with loops initialize an index variable to 0 loop up to, but not including, size of array increment index variable by 1 process array element inside loop int a[5]; // some code goes here int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result;

20 2D arrays and loops to process each element in a 2D array, use a nested loop outer loop: loop through rows inner loop: loop through columns structure of the loops is exactly the same initialize an index variable to 0 loop up to, but not including, size of array increment index variable by 1 process array element inside loop NOTE: this is a guideline, but not the rule

21 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl;

22 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 1. Initialize i to 0

23 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 2. Loop up to, but not including, number of rows

24 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Outer Loop: 3. Increment index by 1

25 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Each pass through this loop accesses a different row. Now write an inner loop to process each row.

26 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 1. Initialize j to 0

27 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 2. Loop up to, but not including, size of row (number of columns)

28 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { result += a[i][j]; } cout >> "Sum = " << sum << endl; Inner Loop: 3. Increment j by 1

29 Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an accumulation variable int a[4][5]; // some code goes here int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } cout >> "Sum = " << sum << endl; Add value to accumulation variable (sum).

30 2D Arrays and Information Access a 2D array affords us the same advantages as any other table e.g. How would I find the average grade of the first student? 18 16 9 12 13 15 17 16 14 5 2 10 sum all elements in first row, divide by 4

31 Find first student's average int grades[3][4] = {{18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10}}; int sum = 0; for (int j = 0; j < 4; j++) { sum += grades[0][j]; } cout >> "First student average = " << sum/4.0 << endl;

32 2D Arrays and Information Access a 2D array affords us the same advantages as any other table e.g. How would I find the average grade of the first assignment? 18 16 9 12 13 15 17 16 14 5 2 10 sum all elements in first row, divide by 3

33 Find first student's average int grades[3][4] = {{18, 16, 9, 12}, {13, 15, 17, 16}, {14, 5, 2, 10}}; int sum = 0; for (int i = 0; i < 3; i++) { sum += grades[i][0]; } cout >> "First assignment average = " << sum/3.0 << endl;

34 2D Arrays as function parameters just like 1D arrays, 2D arrays can be passed as function parameters however, just like in initialization, the column size of the array MUST BE INCLUDED!

35 Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[4][5]) { int sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } return sum; }

36 Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[][5], int size) { int sum = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < 5; j++) { sum += a[i][j]; } return sum; }

37 Example: given a 2D array, write a function that returns sum of elements in array int sumArray(int a[][], int size, int size2) { int sum = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size2; j++) { sum += a[i][j]; } return sum; } This won't compile!

38 Beyond two dimensions you can include as many dimensions as you like int a[3][4][5]; a[1][2][3] = 6; Rules: when initializing arrays, the sizes of all dimensions except the first must be declared explicitly int a[][4][5] = {1, 2, 3 … when passing a multi-dimensional array as a parameter, sizes of all dimensions except the first must be declared!

39 Example: Write a function that takes a 2D array of integers as a parameter, and prints out the array in 2D format put each number in a column of size 5 18 16 9 12 13 15 17 16 14 5 2 10

40 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: Print each row in the matrix, following each with a new line }

41 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: For each row in the matrix Step 1.1 Print out row Step 1.2 Print new line }

42 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { Step 1: For each row in the matrix Step 1.1 Print out row Step 1.2 Print new line }

43 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 Print out row Step 1.2 Print new line }

44 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 Print out row Step 1.2 Print new line }

45 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 for each column in row Step 1.1.1 print item at that column Step 1.2 Print new line }

46 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { Step 1.1 for each column in row Step 1.1.1 print item at that column Step 1.2 Print new line }

47 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step 1.1.1 print item at that column } Step 1.2 Print new line }

48 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step 1.1.1 print item at that column } Step 1.2 Print new line }

49 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } Step 1.2 Print new line }

50 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } Step 1.2 Print new line }

51 Algorithm Step 1: Print each row in the matrix, following each with a new line void printArray(int a[][4], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { cout << setw(5) << a[i][j]; } cout << endl; }

52 Example 2: Write an add function for a 2D array, that adds the two arrays component by component (assume arrays are same size) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 + = 14 16 18 20 22 24 26 28 30 32 34 36

53 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: Add each item from a and b, place result in C }

54 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: Add each corresponding item from a and b, place result in c's corresponding location }

55 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each corresponding item in a and b Step 1.1 Place the sum of these items in c's corresponding location }

56 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each corresponding item in a and b Step 1.1 Place the sum of these items in c's corresponding location }

57 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each row of a (and b) Step 1.1 for each column of a (and b) Step 1.1.1 Place the sum of these items in c's corresponding location }

58 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { Step 1: for each row of a (and b) Step 1.1 for each column of a (and b) Step 1.1.1 Place the sum of these items in c's corresponding location }

59 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { Step 1.1 for each column of a (and b) Step 1.1.1 Place the sum of these items in c's corresponding location }

60 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { Step 1.1 for each column of a (and b) Step 1.1.1 Place the sum of these items in c's corresponding location }

61 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step 1.1.1 Place the sum of these items in c's corresponding location }

62 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { Step 1.1.1 Place the sum of these items in c's corresponding location }

63 Algorithm Step 1: Print each row in the matrix, following each with a new line void add(int size, int a[][4], int b[][4], int c[][4]) { for (int i = 0; i < size; i++) { for (int j = 0; j < 4; j++) { c[i][j] = a[i][j] + b[i][j]; }


Download ppt "Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular."

Similar presentations


Ads by Google