CSCI261E/F
Lecture 11: Multi-Dimensional Arrays September 29, 2010
CSCI261E/F Lecture 11: Multi-Dimensional Arrays September 29, 2010 - - PowerPoint PPT Presentation
CSCI261E/F Lecture 11: Multi-Dimensional Arrays September 29, 2010 ? Binary Search Algorithm (to a programmer, like an old favorite pair of jeans) Binary Search Define a target (what are you searching for?) Compare target to middle
CSCI261E/F
Lecture 11: Multi-Dimensional Arrays September 29, 2010
(to a programmer, like an old favorite pair of jeans)
Binary Search
middle item?
Binary Search
Review
values of a specific type (int, char, etc)
Review
Values inside arrays have indexes that point to them.
‘a’ ‘b’ ‘c’
0 1 2 Array indices start with 0.
Review
Arrays and loops work well together! “Computer, for each number i, print myarray[i].”
int array_size = 10; char myarray[array_size]; // myarray[10] for (int i = 0; i < array_size; i++) { cout << myarray[i]; }
Declaring Arrays
double s[6]; // declare an array s // that holds 6 doubles char v[3] = {‘a’, ‘b’, ‘c’}; // declare an array v // that holds 3 chars // and init with values string x[3] = {“spam”}; // declare an array x that // holds 3 strings and init // all three values to “spam” int z[] = {1, 2, 3}; // computer assumes an array // size of 3
A Visual Example
char v[3] = {‘a’, ‘b’, ‘c’};
‘a’ ‘b’ ‘c’
0 1 2 Three elements, v[0], v[1] and v[2].
Array No-No’s
char v[3] = {‘a’, ‘b’, ‘c’}; cout << v[5]; // index out of range cout << v[3]; // index out of range cout << v[-1]; // index out of range
And remember, in C++ you can’t change an array’s size after you declare it.
Why Arrays?
we want collections of values.
Multi-dimensional Arrays
Arrays Can Hold Arrays
‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’
1 2
1 2 1 2 1 2
“Two-Dimensional” Arrays
‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’
1 2
1 2 1 2 1 2
“Two-Dimensional” Arrays
‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’
1 2
1 2 1 2 1 2
a b c q r s x t c
1 2 2 2 1
2D Arrays
a b c q r s x t c
1 2 2 1 Hmm... looks like a table! Or one of my data files!
a b c q r s x t c 120 200 300 20 30 40 1 3 5 1.2 3.0 4.2 22.1 3.4 22.0 4.4 3.2 1.7
Declaring 2D Arrays
char my_array[4][2];
“Computer, create an array that can hold 4 elements, and inside each element create an array that can hold 2 elements.”
1 2
1 1 1 1
3
Initializing 2D Array Values
char my_array[4][2] = {{‘a’,’b’}, {‘o’,’j’}, {‘o’,’k’}, {‘c’, ‘d’}};
a b
1 2
1 1 1
c d
1
3
Exercise
Declaring 2D Arrays
int my_exes[4][2];
“Computer, create a 4 x 2 (rows x columns) table.”
1 2 3 1
Accessing Values
“Fred” “Savage” “Corey” “Haim”
1 2
1 1
“Luke” “Perry”
1
string actors[3][2] = {“Fred”, “Savage”, “Corey”, “Haim”, “Luke”, “Perry” };
How do you access the string “Haim” ?
Accessing Values
“Fred” “Savage” “Corey” “Haim”
1 2
1 1
“Luke” “Perry”
1
string actors[3][2] = {“Fred”, “Savage”, “Corey”, “Haim”, “Luke”, “Perry” };
actors[1][1]; // ? actors[2][1]; // ? actors[0][0]; // ? actors[0]; // ? actors[2]; // ?
Accessing Values
Your brain may prefer the table idea...
“Fred” “Savage” “Corey” “Haim” “Luke” “Perry” “Mr.” “T”
1 2 3 1
actors[3][1]; // ? actors[2][1]; // ? actors[1]; // ? myarray[row][column];
Looping Through 2D Arrays
Simple: use two for loops.
Looping Through 2D Arrays
string mycars[][2] = {“BMW”, “M5”, “Porsche”, “911”}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << mycars[i][j]; } }
“Computer, for each row i in the array, print the value in jth column.”
Exercise
int mydata[5][4]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { mydata[i][j] = i + j; } }
Example: Reading Data
ifstream breakups(“breakupdata.txt”); string mybreakups[4][2]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { breakups >> mybreakups[i][j]; } } 2 3 2 2 1 3 2 1
i broke up with her | she broke up with me
Example
// assuming mybreakups[4][2] is populated for (int i = 0; i < 4; i++) { cout << “During week “ << (i +1); cout << “ i broke up with her “ << mybreakups[i][0] << “ times and she broke up with me “ << mybreakups[i][1] << “ times.”; } } 2 3 2 2 1 3 2 1
i broke up with her | she broke up with me
Homework