Title: Computer Science 1620
1Computer Science 1620
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 grades4
- store a list of hockey players
- player players50
- however, sometimes data has more structure than
this
3- Example Store the assignment grades for
multiple students - essentially a 'table' of information
Mark 2
Mark 1
Mark 3
Mark 4
18 16 9 12 13 15 17 16 14 5 2 10
Student 1
Student 2
Student 3
4- Example Store the assignment grades for 5
different students - Solution 1 Store 5 different arrays
int grades14 int grades24 int
grades34 int grades44 int grades54
5- Example Store the assignment grades for 50
different students - Solution 1 Store 50 different arrays
int grades14 int grades24 int
grades34 int grades44 int grades54 int
grades64 int grades74 int grades84 int
grades94 int grades104 int grades114 int
grades124 int grades134 int
grades144 int grades154 int
grades164 int grades174 int
grades184 int grades194 int
grades204 int grades214 int
grades224 int grades234 int
grades244 int grades254 int
grades264 int grades274 int
grades284 int grades294 int
grades304 int grades314 int
grades324 int grades334 int
grades344 int grades354 int
grades364 int grades734 int
grades384 int grades394 int
grades404 int grades414 int
grades424 int grades434 int
grades444 int grades454 int
grades464 int grades474 int
grades484 int grades494 int grades504
6Multi-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
7Multi-Dimensional 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
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
8- A Table of Information
- what do you notice about this declaration?
- 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?
int grades14 int grades24 int
grades34 int grades44 int grades54
92D 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 a34
10- Declaring a 2D array
- use two subscript notations
- first subscript number of rows
- second subscript number of columns
3 rows
int a34
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
refers to second row
18 16 9 12 13 15 17 16 14 5 2 10
a12 17
refers to third column
12- 2D Arrays vs. Multiple Arrays
- store the assignment grades for 50 different
students
int grades14 int grades24 int
grades34 int grades44 int grades54 int
grades64 int grades74 int grades84 int
grades94 int grades104 int grades114 int
grades124 int grades134 int
grades144 int grades154 int
grades164 int grades174 int
grades184 int grades194 int
grades204 int grades214 int
grades224 int grades234 int
grades244 int grades254 int
grades264 int grades274 int
grades284 int grades294 int
grades304 int grades314 int
grades324 int grades334 int
grades344 int grades354 int
grades364 int grades734 int
grades384 int grades394 int
grades404 int grades414 int
grades424 int grades434 int
grades444 int grades454 int
grades464 int grades474 int
grades484 int grades494 int grades504
13- 2D Arrays vs. Multiple Arrays
- store the assignment grades for 50 different
students
int grades504
14Multi-Dimensional Arrays
- 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
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
15Multi-Dimensional Arrays
- 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
18 16 9 12 13 15 17 16 14 5 2 10
int a34 // code for initializing a11
15 // this sets the 2nd student's 2nd mark to 15
16- Initializing a 2D array
- 2D arrays can be initialized just as a 1D array
can
int one12 18, 16, 9, 12, 13, 15, 17, 16, 14,
5, 2, 10 int two34 18, 16, 9, 12,
13, 15, 17, 16, 14, 5, 2, 10
int one12 18, 16, 9, 12, 13, 15, 17, 16, 14,
5, 2, 10 int two34 18, 16, 9, 12,
13, 15, 17, 16, 14, 5, 2, 10
row 1
row 2
row 3
use braces to separate individual rows
17- 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
int one12 18, 16, 9, 12, 13, 15, 17, 16, 14,
5, 2, 10 int two34 18, 16, 9, 12, 13,
15, 17, 16, 14, 5, 2, 10
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 two4 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 a5 // some code goes here int
result 0 for (int i 0 i lt 5 i)
result ai 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) result aij
cout gtgt "Sum " ltlt sum ltlt 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 a45 // some code goes here int sum
0 for (int i 0 i lt 4 i) for (int j
0 j lt 5 j) sum aij cout
gtgt "Sum " ltlt sum ltlt 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?
sum all elements in first row, divide by 4
18 16 9 12 13 15 17 16 14 5 2 10
31- Find first student's average
int grades34 18, 16, 9, 12,
13, 15, 17, 16,
14, 5, 2, 10 int sum 0 for (int j
0 j lt 4 j) sum grades0j cout
gtgt "First student average " ltlt sum/4.0 ltlt 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?
sum all elements in first row, divide by 3
18 16 9 12 13 15 17 16 14 5 2 10
33- Find first student's average
int grades34 18, 16, 9, 12,
13, 15, 17, 16,
14, 5, 2, 10 int sum 0 for (int i
0 i lt 3 i) sum gradesi0 cout
gtgt "First assignment average " ltlt sum/3.0 ltlt
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 a45) int sum 0
for (int i 0 i lt 4 i) for (int j 0
j lt 5 j) sum aij
return sum
36- Example given a 2D array, write a function that
returns sum of elements in array
int sumArray(int a5, int size) int sum
0 for (int i 0 i lt size i) for
(int j 0 j lt 5 j) sum aij
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 lt size i)
for (int j 0 j lt size2 j)
sum aij return sum
This won't compile!
38- Beyond two dimensions
- you can include as many dimensions as you like
- int a345
- a123 6
- Rules
- when initializing arrays, the sizes of all
dimensions except the first must be declared
explicitly - int a45 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 void printArray(int a4, int size) Step
1 Print each row in the matrix, following each
with a new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
41 void printArray(int a4, int size) Step
1 For each row in the matrix Step 1.1 Print out
row Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
42 void printArray(int a4, int size) Step
1 For each row in the matrix Step 1.1 Print out
row Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
43 void printArray(int a4, int size) for
(int i 0 i lt size i) Step 1.1 Print out
row Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
44 void printArray(int a4, int size) for
(int i 0 i lt size i) Step 1.1 Print out
row Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
45 void printArray(int a4, int size) for
(int i 0 i lt 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
46 void printArray(int a4, int size) for
(int i 0 i lt 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
47 void printArray(int a4, int size) for
(int i 0 i lt size i) for (int j 0 j lt
4 j) Step 1.1.1 print item at
that column Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
48 void printArray(int a4, int size) for
(int i 0 i lt size i) for (int j 0 j lt
4 j) Step 1.1.1 print item at
that column Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
49 void printArray(int a4, int size) for
(int i 0 i lt size i) for (int j 0 j lt
4 j) cout ltlt setw(5) ltlt
aij Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
50 void printArray(int a4, int size) for
(int i 0 i lt size i) for (int j 0 j lt
4 j) cout ltlt setw(5) ltlt
aij Step 1.2 Print new line
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
51 void printArray(int a4, int size) for
(int i 0 i lt size i) for (int j 0 j lt
4 j) cout ltlt setw(5) ltlt
aij cout ltlt endl
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
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 void add(int size, int a4, int b4, int
c4) Step 1 Add each item from a and b,
place result in C
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
54 void add(int size, int a4, int b4, int
c4) Step 1 Add each corresponding item
from a and b, place result in c's corresponding
location
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
55 void add(int size, int a4, int b4, int
c4) Step 1 for each corresponding item
in a and b Step 1.1 Place the sum of these
items in c's corresponding location
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
56 void add(int size, int a4, int b4, int
c4) Step 1 for each corresponding item
in a and b Step 1.1 Place the sum of these
items in c's corresponding location
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
57 void add(int size, int a4, int b4, int
c4) 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
58 void add(int size, int a4, int b4, int
c4) 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
59 void add(int size, int a4, int b4, int
c4) for (int i 0 i lt 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
60 void add(int size, int a4, int b4, int
c4) for (int i 0 i lt 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
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
61 void add(int size, int a4, int b4, int
c4) for (int i 0 i lt size i)
for (int j 0 j lt 4 j) Step 1.1.1
Place the sum of these items in c's corresponding
location
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
62 void add(int size, int a4, int b4, int
c4) for (int i 0 i lt size i)
for (int j 0 j lt 4 j) Step 1.1.1
Place the sum of these items in c's corresponding
location
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line
63 void add(int size, int a4, int b4, int
c4) for (int i 0 i lt size i)
for (int j 0 j lt 4 j) cij
aij bij
- Algorithm
- Step 1 Print each row in the matrix, following
each with a new line