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

csci261
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSCI261

Lecture 18: Multi-Dimensional Arrays

slide-2
SLIDE 2

?

slide-3
SLIDE 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
slide-4
SLIDE 4

Review

Values inside arrays have indexes that point to them.

‘a’ ‘b’ ‘c’

0 1 2 Array indices start with 0.

slide-5
SLIDE 5

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]; }

slide-6
SLIDE 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].

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

slide-8
SLIDE 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.
slide-9
SLIDE 9

Multi-dimensional Arrays

slide-10
SLIDE 10

Arrays Can Hold Arrays

‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’

1 2

1 2 1 2 1 2

slide-11
SLIDE 11

“Two-Dimensional” Arrays

‘a’ ‘b’ ‘c’ ‘q’ ‘r’ ‘s’ ‘x’ ‘t’ ‘c’

1 2

1 2 1 2 1 2

slide-12
SLIDE 12

“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

slide-13
SLIDE 13

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

slide-14
SLIDE 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.”

1 2

1 1 1 1

3

slide-15
SLIDE 15

Initializing 2D Array Values

char my_array[4][2] = {{‘a’,’b’}, {‘o’,’j’}, {‘o’,’k’}, {‘c’, ‘d’}};

a b

  • j
  • k

1 2

1 1 1

c d

1

3

slide-16
SLIDE 16

Exercise

  • int temp[2][5];
  • string exes_this_week[3][2];
  • double mydata[3][2];
  • char vowels[1][5];
  • char vowels[5][1];
slide-17
SLIDE 17

Declaring 2D Arrays

int my_exes[4][2];

“Computer, create a 4 x 2 (rows x columns) table.”

1 2 3 1

slide-18
SLIDE 18

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

slide-19
SLIDE 19

Accessing Values

“Fred” “Savage” “Corey” “Haim”

1 2

1 1

“Luke” “Perry”

1

actors[1][1]; // ? actors[2][1]; // ? actors[0][0]; // ? actors[0]; // ? actors[2]; // ?

string actors[3][2] = {“Fred”, “Savage”, “Corey”, “Haim”, “Luke”, “Perry” };

slide-20
SLIDE 20

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];

slide-21
SLIDE 21

Looping Through 2D Arrays

Simple: use two for loops.

slide-22
SLIDE 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 jth column.”

slide-23
SLIDE 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; } }

slide-24
SLIDE 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 2 2 1 3 2 1

i broke up with her | she broke up with me

slide-25
SLIDE 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 2 2 1 3

i broke up with her | she broke up with me

slide-26
SLIDE 26

Homework

  • Read Etter 7.1
  • Complete assignment 21_colorGrid