CSCI261E/F Lecture 11: Multi-Dimensional Arrays September 29, 2010 - - PowerPoint PPT Presentation

csci261e f
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSCI261E/F

Lecture 11: Multi-Dimensional Arrays September 29, 2010

slide-2
SLIDE 2

?

slide-3
SLIDE 3

Binary Search Algorithm

(to a programmer, like an old favorite pair of jeans)

slide-4
SLIDE 4

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
  • f the list.
slide-5
SLIDE 5

Binary Search

  • Assumes a sorted collection
  • Think “divide and conquer”
  • Logarithmic time (why?)
slide-6
SLIDE 6

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

Review

Values inside arrays have indexes that point to them.

‘a’ ‘b’ ‘c’

0 1 2 Array indices start with 0.

slide-8
SLIDE 8

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-9
SLIDE 9

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

slide-10
SLIDE 10

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-11
SLIDE 11

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-12
SLIDE 12

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-13
SLIDE 13

Multi-dimensional Arrays

slide-14
SLIDE 14

Arrays Can Hold Arrays

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

1 2

1 2 1 2 1 2

slide-15
SLIDE 15

“Two-Dimensional” Arrays

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

1 2

1 2 1 2 1 2

slide-16
SLIDE 16

“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-17
SLIDE 17

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-18
SLIDE 18

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-19
SLIDE 19

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-20
SLIDE 20

Exercise

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

Declaring 2D Arrays

int my_exes[4][2];

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

1 2 3 1

slide-22
SLIDE 22

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-23
SLIDE 23

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

slide-24
SLIDE 24

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

Looping Through 2D Arrays

Simple: use two for loops.

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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-28
SLIDE 28

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-29
SLIDE 29

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

slide-30
SLIDE 30

Homework

  • Review 6.1 and 7.1
  • Ignore p260-262 “Function Arguments”
  • Ignore p308-311 “Function Arguments”
  • Read 6.9
  • Complete assignment PowerPlantMax