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 item in the list. • Does the target == the middle item? Done. • Does the target come before or after the middle item? • Repeat this procedure on the first/second half of the list.
Binary Search • Assumes a sorted collection • Think “divide and conquer” • Logarithmic time (why?)
Review • An array is a simple data structure • Like a list or a set • Arrays contain references to multiple values of a specific type ( int , char , etc) • int myarray[10] // declaration • char myarray[3] = {‘a’, ‘b’, ‘c’} // initialization • myarray[1] // access
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! int array_size = 10; char myarray[array_size]; // myarray[10] for (int i = 0; i < array_size; i++) { cout << myarray[i]; } “Computer, for each number i , print 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? • Variables just store one value, and sometimes we want collections of values. • Great for storing information from data files. • Great for sequentially iterating (looping) over.
Multi-dimensional Arrays
Arrays Can Hold Arrays ‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’ 0 1 2 0 1 2 0 1 2 0 1 2
“Two-Dimensional” Arrays ‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’ 0 1 2 0 1 2 0 1 2 0 1 2
“Two-Dimensional” Arrays ‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’ 0 1 2 0 1 2 0 1 2 0 1 2 1 0 2 0 a b c 1 q r s 2 x t c 2
2D Arrays 1 0 2 0 a b c 1 q r s 2 x t c Hmm... looks like a table ! Or one of my data files! 1.2 3.0 4.2 a b c 120 200 300 22.1 3.4 22.0 q r s 20 30 40 4.4 3.2 1.7 x t c 1 3 5
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.” 0 1 0 1 0 1 0 1 3 0 1 2
Initializing 2D Array Values char my_array[4][2] = {{‘a’,’b’}, {‘o’,’j’}, {‘o’,’k’}, {‘c’, ‘d’}}; a b o j o k c d 0 1 0 1 0 1 0 1 3 0 1 2
Exercise • int temp[2][5]; • string exes_this_week[3][2]; • double mydata[3][2]; • char vowels[1][5]; • char vowels[5][1];
Declaring 2D Arrays int my_exes[4][2]; “Computer, create a 4 x 2 (rows x columns) table.” 0 1 0 1 2 3
Accessing Values string actors[3][2] = {“Fred”, “Savage”, “Corey”, “Haim”, “Luke”, “Perry” }; “Fred” “Savage” “Corey” “Haim” “Luke” “Perry” 0 1 0 1 0 1 2 1 0 How do you access the string “Haim” ?
Accessing Values string actors[3][2] = {“Fred”, “Savage”, “Corey”, “Haim”, “Luke”, “Perry” }; “Fred” “Savage” “Corey” “Haim” “Luke” “Perry” 0 1 0 1 0 1 2 1 0 actors[1][1]; // ? actors[2][1]; // ? actors[0][0]; // ? actors[0]; // ? actors[2]; // ?
Accessing Values Your brain may prefer the table idea... 0 1 actors[3][1]; // ? actors[2][1]; // ? 0 “Fred” “Savage” actors[1]; // ? 1 “Corey” “Haim” 2 “Luke” “Perry” myarray[row][column]; 3 “Mr.” “T”
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 j th 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 i broke up with her | she broke up with me 2 2 1 3 2 1
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 i broke up with her | she broke up with me 2 2 1 3 2 1
Homework • Review 6.1 and 7.1 • Ignore p260-262 “Function Arguments” • Ignore p308-311 “Function Arguments” • Read 6.9 • Complete assignment PowerPlantMax
Recommend
More recommend