1 arrays … the sequel applications and extensions chapter 10

35
1 Arrays … The Sequel Applications and Extensions Chapter 10

Upload: donte-kidman

Post on 30-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Arrays … The Sequel Applications and Extensions Chapter 10

1

Arrays … The SequelApplications and Extensions

Chapter 10

Page 2: 1 Arrays … The Sequel Applications and Extensions Chapter 10

2

Applying What You Learn

Searching through arrays efficiently

Sorting arrays

Using character arrays as "STRINGS"

strings … get it? hahahaha

Page 3: 1 Arrays … The Sequel Applications and Extensions Chapter 10

3

Lists• Defn => A collection of homogeneous

components– linear collection– variable length collection

• Length <=> the actual number of values stored in the list

• Example -- a file of time card information

Joe, 40, Clyde, 38.5, Sniudly, 42.75 ...

Page 4: 1 Arrays … The Sequel Applications and Extensions Chapter 10

4

Lists

• Arrays can be used to implement a list– declare the array large– keep track of how many elements used

• We often do operations on the lists– create a list, add an item, delete an item– print the list, search the list for a value– sort the list

• A list of numbersscores : 85 79 92 57 68 80 . . . scores : 85 79 92 57 68 80 . . .

0 1 2 3 4 5 98 99

Page 5: 1 Arrays … The Sequel Applications and Extensions Chapter 10

5

Sequential Search

• Consider the list unordered (not sorted)

• For a function to search for a targert we must specify– name of the array to be searched– length of the list (number of array elements)– a flag parameter which tells whether or

not the search was successful– an index value to be returned which tells where in the list

the item was found

scores

scores : 85 79 92 57 68 80 . . . scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99

5

boolean & found

int & location

Page 6: 1 Arrays … The Sequel Applications and Extensions Chapter 10

6

Sequential Search

• Algorithm example

• Note use of reference parameters for the found flag and the location

void search (int list[ ], int length, int target, boolean & found, int &location) { location = 0; while ((location < length) && (target != list[location])) location++; found = (index < length); } // if found == TRUE, location OK . . . search (scores, 5, 92, found_it, where_its_at);

void search (int list[ ], int length, int target, boolean & found, int &location) { location = 0; while ((location < length) && (target != list[location])) location++; found = (index < length); } // if found == TRUE, location OK . . . search (scores, 5, 92, found_it, where_its_at);

scores : 85 79 92 57 68 80 . . . scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99

Page 7: 1 Arrays … The Sequel Applications and Extensions Chapter 10

7

Sorted List -- Faster Search

• Sorted list => components arranged in order– alphabetical– numerically ascending or descending

• Advantage of a sorted list– need to search only until the value found is larger

than target value

Page 8: 1 Arrays … The Sequel Applications and Extensions Chapter 10

8

Sorting

• Means arranging the list elements into some order (for instance, strings into alphabetical order, or numbers into ascending or descending order).

Dale NellWeems ChipHeadington MarkCooper SoniaHuang Jeff

Cooper Sonia Dale NellHeadington MarkHuang Jeff Weems Chip

sorting

Page 9: 1 Arrays … The Sequel Applications and Extensions Chapter 10

9

Sorting Algorithm

• Make a pass through the list, look for smallest number

list 1 : 85 79 92 57 68 80 . . . list 1 : 85 79 92 57 68 80 . . .

list 2 :list 2 :

Page 10: 1 Arrays … The Sequel Applications and Extensions Chapter 10

10

Sorting Algorithm

• Make a pass through the list, look for smallest number

• Write that number in another list, cross it off first list

list 1 : 85 79 92 57 68 80 . . . list 1 : 85 79 92 57 68 80 . . .

list 2 : 57list 2 : 57

Page 11: 1 Arrays … The Sequel Applications and Extensions Chapter 10

11

Sorting Algorithm

• Make a pass through the list, look for smallest number

• Write that number in another list, cross it off first list

• Repeat process, always look for smallest number remaining

list 1 : 85 79 92 57 68 80 . . . list 1 : 85 79 92 57 68 80 . . .

list 2 : 57 68 list 2 : 57 68

Page 12: 1 Arrays … The Sequel Applications and Extensions Chapter 10

12

Sorting Algorithm

• Make a pass through the list, look for smallest number

• Write that number in another column, cross it off first list

• Repeat process, always look for smallest number remaining

• Stop when all numbers have been crossed off

Page 13: 1 Arrays … The Sequel Applications and Extensions Chapter 10

13

Sequential Search in a Sorted List

• Note difference from previous search

void search_ord ( int list[ ], int target, int length, int & index, boolean & found)

{ index = 0;list [length] = target; // store an item beyond end while (target > list [index])

index++;found = (index < length && ltem = = list[index]; )

Explain how the last statement worksExplain how the last statement works

Page 14: 1 Arrays … The Sequel Applications and Extensions Chapter 10

14

Inserting into an Ordered List

• We wish to insert a new number into the list in the right position– find where it goes -- look until you find a number

bigger than the new number

list 2 : 14 22 45 61 87 list 2 : 14 22 45 61 87

length : 5length : 5

5959

Page 15: 1 Arrays … The Sequel Applications and Extensions Chapter 10

15

list 2 : 14 22 45 61 87 list 2 : 14 22 45 61 87

Inserting into an Ordered List

• We wish to insert a new number into the list in the right position– find where it goes -- look until you find a number

bigger than the new number– shift that number all the rest of the elements down

list 2 : 14 22 45 61 87 list 2 : 14 22 45 61 87

length : 5length : 5

5959

Page 16: 1 Arrays … The Sequel Applications and Extensions Chapter 10

16

list 2 : 14 22 45 61 87 list 2 : 14 22 45 61 87

Inserting into an Ordered List

• We wish to insert a new number into the list in the right position– find where it goes -- look until you find a number

bigger than the new number– shift that number all the rest of the elements down– insert the new number in the vacated spot

list 2 : 14 22 45 59 61 87 list 2 : 14 22 45 59 61 87

length : 5length : 5

5959

Page 17: 1 Arrays … The Sequel Applications and Extensions Chapter 10

17

length : 5length : 5

Inserting into an Ordered List

• We wish to insert a new number into the list in the right position– find where it goes -- look until you find a number

bigger than the new number– shift that number all the rest of the elements down– insert the new number in the vacated spot– be sure to increment the length

length : 6length : 6

list 2 : 14 22 45 59 61 87 list 2 : 14 22 45 59 61 87

Page 18: 1 Arrays … The Sequel Applications and Extensions Chapter 10

18

Binary Search in an Ordered List• Examines the element in the middle of the array. • Is it the sought item?

– If so, stop searching.

– Is the middle element too small? • Then start looking in second half of array.

– Is the middle element too large? • Then begin looking in first half of the array.

• Repeat the process in the half of the list that should be examined next.

• Stop when – item is found, or when

– there is nowhere else to look and it has not been located.

Page 19: 1 Arrays … The Sequel Applications and Extensions Chapter 10

19

String Library Routines

String comparison:returns -1 if s1 < s2

returns 0 if they are equalreturns +1 if s1 > s2

String comparison:returns -1 if s1 < s2

returns 0 if they are equalreturns +1 if s1 > s2

• String assignment• String assignment

Returns length of the string

Returns length of the string

Page 20: 1 Arrays … The Sequel Applications and Extensions Chapter 10

20

Using typedef with Arrays

• Specify an array type– this can be used throughout program– helps program self document

• Example :

typedef char default_string [80]; . . .

defalt_string fname, descrip;void reverse (default_string s);

typedef char default_string [80]; . . .

defalt_string fname, descrip;void reverse (default_string s);

Page 21: 1 Arrays … The Sequel Applications and Extensions Chapter 10

21

Two dimensional Arrays

• A collection of components– all of the same type– structured in TWO dimensions– each component accessed by a PAIR of indices

representing the component’s position in each dimension

7 2 13 6 -40 6 3 -9 5

12 4 3 1 -15 2 -2 8 10

0 1 2 3 4

0

1

2

3

Which cell isLocation (2,3) ?

Page 22: 1 Arrays … The Sequel Applications and Extensions Chapter 10

22

Declaring Two Dimensional Arrays

• Syntax: data_type array_name [row_dim][col_dim];

• Example:

• First element isint_table[0][0]

• Last element isint_table[4][3]

7 2 13 6 -40 6 3 -9 5

12 4 3 1 -15 2 -2 8 10

0 1 2 3 4

0

1

2

3

int int_table [5][4];int int_table [5][4];

Page 23: 1 Arrays … The Sequel Applications and Extensions Chapter 10

23

Processing Two-D Arrays

• Arrays processed in some pattern– random– along rows– along columns– whole array

• We will use the declaration shown below:

int int_table [5][4];int row, col;

int int_table [5][4];int row, col;

Page 24: 1 Arrays … The Sequel Applications and Extensions Chapter 10

24

Processing Two-D Arrays

• What does the routine below do with the array? What should we name the function?

0

total_a_row

Page 25: 1 Arrays … The Sequel Applications and Extensions Chapter 10

25

Processing Two-D Arrays

• What does this routine below do with the array? What should we name the function?

0

total_column

Page 26: 1 Arrays … The Sequel Applications and Extensions Chapter 10

26

Processing Two-D Arrays

• This function initializes an array.

• Fill in the blanks with the correct identifiers

3

col

table value

Page 27: 1 Arrays … The Sequel Applications and Extensions Chapter 10

27

Printing a Table

• We must process each row, item by item

• Which will be the inner loop?Which will be the outer loop?

• The LCV of the inner loop must be the one which changes the most often

What goeshere?

row row rowcol col col

endl

Page 28: 1 Arrays … The Sequel Applications and Extensions Chapter 10

28

Passing Arrays as Parameters

• Recall declaration for 1-D array

• we didn’t specify the size in the brackets– we sent the size as a separate parameter

• Recall name of the array is a pointer constant– tells where the array starts in memory– this is what is passed to the function

void whatever ( float num_list [ ], int size);void whatever ( float num_list [ ], int size);

Page 29: 1 Arrays … The Sequel Applications and Extensions Chapter 10

29

Passing 2-D Arrays as Parameters

• For a 2-D array, declare

• We are sending the starting address– also how many elements it takes to jump us to the next row– that is -- the number of columns

• Note that this could be for an array for ANY number of rows, but exactly 4 columns

• As with 1-D, we also send another parameter for the function as a limiting value

void whatever ( float num_table [ ][4], int num_rows);void whatever ( float num_table [ ][4], int num_rows);

Page 30: 1 Arrays … The Sequel Applications and Extensions Chapter 10

30

Alternate 2-D Array Definition

• Think of the 2-D array as an array of arrays

• Example

• Each element of renters is an array of rent_pmts

typedef float rent_pmts [12];rent_pmts renters [6];

typedef float rent_pmts [12];rent_pmts renters [6];

50 50 … 50 50

50 50 … 50 50

… … …

50 50 … 50 50

0

1

5

0 1 … 10 11

Page 31: 1 Arrays … The Sequel Applications and Extensions Chapter 10

31

Multidimensional Arrays

• C++ arrays not limited to two dimensions– limitation is amount of memory

50 50 … 50 50

50 50 … 50 50

… … …

50 50 … 50 50

const int NUM_BLDGS = 10;const int APTS_PER_BLDG = 12;float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG];

const int NUM_BLDGS = 10;const int APTS_PER_BLDG = 12;float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG];

Page 32: 1 Arrays … The Sequel Applications and Extensions Chapter 10

32

Multidimensional Arrays

• C++ arrays not limited to two dimensions

– limitation is amount of memory

50 50 … 50 50

50 50 … 50 50

… … …

50 50 … 50 50

const int NUM_BLDGS = 10;const int APTS_PER_BLDG = 12;float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG];

const int NUM_BLDGS = 10;const int APTS_PER_BLDG = 12;float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG];

This gives a 3-D array with 720 items

apt_pmt [bldg][tennant][apt]

Page 33: 1 Arrays … The Sequel Applications and Extensions Chapter 10

33

Testing and Debugging

• Initialize all components of an array

– no guarantee of what is there to start with

• Use the same number of indeces as the declaration of array

• Make sure indeces are in order

– don’t reverse row and column references

Page 34: 1 Arrays … The Sequel Applications and Extensions Chapter 10

34

Testing and Debugging

• Use meaningful identifiers for the array name and indeces

• Double check upper and lower bounds on indeces

– don’t walk off the edge of the array

• When declaring multidimensional array as a formal parameter

– must state sizes of all but first dimension

Page 35: 1 Arrays … The Sequel Applications and Extensions Chapter 10

35

Testing and Debugging

• When calling function with array as parameter

– sizes of multi-dim actual parameter must match exactly sizes of formal

• Use typedef statement to define multi-dimensional array type

– use this for actual and formal parameter declaration