View
220
Download
0
Tags:
Embed Size (px)
Citation preview
we used arrays to store a set of data of the same typee.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
Example: Store the assignment grades for multiple studentsessentially a 'table' of information
18 16 9 1213 15 17 1614 5 2 10
Mark 1 Mark 2 Mark 3 Mark 4
Student 1
Student 2
Student 3
Example: Store the assignment grades for 5 different studentsSolution 1: Store 5 different arrays
int grades1[4];int grades2[4];int grades3[4];int grades4[4];int grades5[4];
Example: Store the assignment grades for 50 different studentsSolution 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];
Multi-Dimensional Arrays
Solution 2: 'flatten' the table into one array
18 16 9 1213 15 17 1614 5 2 10
int a[] = {18, 16, 9, 12, 13, 15, 17, 16, 14, 5, 2, 10};
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 1213 15 17 1614 5 2 10
A Table of Informationwhat 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?
2D Arrays
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];
Declaring a 2D arrayuse two subscript notations
first subscript: number of rowssecond subscript: number of columns
int a[3][4];
3 rows
4 columns
Indexing a 2D arrayuse the same subscript notation as for a
single-dimensional arrayuse two!
remember: indices always start at 0, not 1
a[1][2] = 17;
refers to second row
refers to third column
18 16 9 1213 15 17 1614 5 2 10
2D Arrays vs. Multiple Arraysstore the assignment grades for 50 different
studentsint 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];
2D Arrays vs. Multiple Arraysstore the assignment grades for 50 different
students
int grades[50][4];
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 1213 15 17 1614 5 2 10
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 initializinga[1][1] = 15; // this sets the 2nd student's 2nd mark to 15
18 16 9 1213 15 17 1614 5 2 10
Initializing a 2D array2D 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 1 row 2 row 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} };
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 1213 15 17 1614 5 2 10
Initializing a 2D array the first subscript value can be omitted, just
as with single-dimension arrays the second subscript value cannot be
omittedint 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.
2D arrays and loopsour 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 1process 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;
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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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;
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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.
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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)
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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
Example: given a 2D array, find the sum of all elements inside the array solution: add each element of the array to an
accumulation variableint 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).
2D Arrays and Information Accessa 2D array affords us the same advantages
as any other tablee.g. How would I find the average grade of the
first student?
18 16 9 1213 15 17 1614 5 2 10
sum all elements in first row, divide by 4
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;
2D Arrays and Information Accessa 2D array affords us the same advantages
as any other tablee.g. How would I find the average grade of the
first assignment?
18 16 9 1213 15 17 1614 5 2 10
sum all elements in first row, divide by 3
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;
2D Arrays as function parameters just like 1D arrays, 2D arrays can be passed
as function parametershowever, just like in initialization, the column
size of the array MUST BE INCLUDED!
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;}
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;}
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!
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!
Example: Write a function that takes a 2D array of integers as a parameter, and prints out the array in 2D formatput each number in a column of size 5
18 16 9 1213 15 17 1614 5 2 10
AlgorithmStep 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
}
AlgorithmStep 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 matrixStep 1.1 Print out rowStep 1.2 Print new line
}
AlgorithmStep 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 matrixStep 1.1 Print out rowStep 1.2 Print new line
}
AlgorithmStep 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 rowStep 1.2 Print new line
}
}
AlgorithmStep 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 rowStep 1.2 Print new line
}
}
AlgorithmStep 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 columnStep 1.2 Print new line
}
}
AlgorithmStep 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 columnStep 1.2 Print new line
}
}
AlgorithmStep 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
}
}
AlgorithmStep 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
}
}
AlgorithmStep 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
}
}
AlgorithmStep 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
}
}
AlgorithmStep 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;
}
}
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 1617 18 19 2021 22 23 24
+ =14 16 18 2022 24 26 2830 32 34 36
AlgorithmStep 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
}
AlgorithmStep 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
}
AlgorithmStep 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
}
AlgorithmStep 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
}
AlgorithmStep 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
}
AlgorithmStep 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
}
AlgorithmStep 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 }
}
AlgorithmStep 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 }
}
AlgorithmStep 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 } }
}
AlgorithmStep 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 } }
}