csci261
play

CSCI261 Lecture 18: Multi-Dimensional Arrays ? Review An array is - PowerPoint PPT Presentation

CSCI261 Lecture 18: Multi-Dimensional Arrays ? 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


  1. CSCI261 Lecture 18: Multi-Dimensional Arrays

  2. ?

  3. 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

  4. Review Values inside arrays have indexes that point to them. ‘a’ ‘b’ ‘c’ 0 1 2 Array indices start with 0.

  5. 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] .”

  6. A Visual Example char v[3] = {‘a’, ‘b’, ‘c’}; ‘a’ ‘b’ ‘c’ 0 1 2 Three elements, v[0], v[1] and v[2].

  7. 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.

  8. 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.

  9. Multi-dimensional Arrays

  10. 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

  11. “Two-Dimensional” Arrays ‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’ 0 1 2 0 1 2 0 1 2 0 1 2

  12. “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

  13. 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! a b c 1.2 3.0 4.2 120 200 300 q r s 22.1 3.4 22.0 20 30 40 4.4 3.2 1.7 1 3 5 x t c

  14. 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

  15. 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

  16. Exercise • int temp[2][5]; • string exes_this_week[3][2]; • double mydata[3][2]; • char vowels[1][5]; • char vowels[5][1];

  17. Declaring 2D Arrays int my_exes[4][2]; “Computer, create a 4 x 2 (rows x columns) table.” 0 1 0 1 2 3

  18. 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” ?

  19. 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]; // ?

  20. 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”

  21. Looping Through 2D Arrays Simple: use two for loops.

  22. Looping Through 2D Arrays string mycars[2][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.”

  23. Exercise int mydata[5][4]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { mydata[i][j] = i + j; } }

  24. 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

  25. 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

  26. Homework • Read Etter 7.1 • Complete assignment 21_colorGrid

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend