Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a...

32
Array (continue)

Transcript of Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a...

Page 1: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Array (continue)

Page 2: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.4.7 Using Arrays to Summarize Survey Results Problem:

◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning excellent). Place the 40 responses in an integer array and summarize the results of the poll.

C++ has no array bounds checking to prevent the computer from referring to an element that does not exist.

Thus, an executing program can “walk off” either end of an array without warning.

You should ensure that all array references remain within the bounds of the array.

Page 3: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.4.8 Static Local Arrays and Automatic Local Arrays A program initializes static local arrays when their

declarations are first encountered. If a static array is not initialized explicitly by you,

each element of that array is initialized to zero by the compiler when the array is created.

Page 4: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.
Page 5: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 6: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 7: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 8: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.5  Passing Arrays to Functions

To pass an array argument to a function, specify the name of the array without any brackets.

When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array.◦ Otherwise, we would need to build this knowledge into the called

function itself or, worse yet, place the array size in a global variable. C++ passes arrays to functions by reference—the called

functions can modify the element values in the callers’ original arrays.

The value of the name of the array is the address in the computer’s memory of the first element of the array.◦ Because the starting address of the array is passed, the called function

knows precisely where the array is stored in memory.

Page 9: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.5  Passing Arrays to Functions (cont.) Although entire arrays are passed by reference,

individual array elements are passed by value exactly as simple variables are.

Such simple single pieces of data are called scalars or scalar quantities.

To pass an element of an array to a func-tion, use the subscripted name of the array element as an argument in the function call.

Page 10: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.
Page 11: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.
Page 12: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.
Page 13: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.
Page 14: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 15: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Searching Problem definition:

Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).

(Assumptions: no duplicate entries in the array)

Page 16: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Search Example:

Number

402018 17 22 27 30

X= 40

0 1 2 3 4 5 6

Return: 3

Page 17: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Search Example:

Number

402018 17 22 27 30

X= 32

0 1 2 3 4 5 6

Return: NOT_FOUND (-1)

Page 18: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Searching We will count the number of comparisons

the algorithms make to analyze their performance.◦ The ideal searching algorithm will make the least

possible number of comparisons to locate the desired data.

◦ Two separate performance analyses are normally done: one for successful search and another for unsuccessful search.

Page 19: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Searching

We will count the number of comparisons the algorithms make to analyze their performance.◦ The ideal searching algorithm will make the least

possible number of comparisons to locate the desired data.

◦ Two separate performance analyses are normally done: one for successful search (x is found) and another for unsuccessful search (x is not found)

Page 20: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Linear Search

Search the array from the first to the last position in linear

progression.

int linearSearch (const int array[], int key, int arraySize) { for (int i=0; i< arraySize; i++) if (array[i] == key) return i; return -1;

}

Page 21: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

Linear Search Performance We analyze the successful and unsuccessful

searches separately. Successful Search

◦ Best Case: 1 comparison◦ Worst Case: N comparisons (N – array size)

Unsuccessful Search◦ Best Case = Worst Case: N comparisons

Page 22: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

1. How many comparisons do we need to find x=6 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. 6b. 7c. 8d. 9

Page 23: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

1. How many comparisons do we need to find x=6 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. 6b. 7c. 8 d. 9

Page 24: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

2. How many comparisons do we need to find x=-8 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. 0b. 1c. 2d. 3

Page 25: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

2. How many comparisons do we need to find x=-8 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. 0b. 1c. 2d. 3

Page 26: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

3. How many comparisons do we need to find x=12 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. Undefinedb. 0c. 10d. Infinity ( )

Page 27: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

“Just in time review”

3. How many comparisons do we need to find x=12 using linear search on the following array A (10 elements): -8 -6 -4 -2 0 2 4 6 8 10a. Undefinedb. 0c. 10d. Infinity ( )

Page 28: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.8  Sorting Arrays with Insertion Sort

Sorting data◦ placing the data into some particular order such as ascending

or descending◦ an intriguing prob-lem that has attracted some of the most

intense research efforts in the field of computer science.

Page 29: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

7.8  Sorting Arrays with Insertion Sort (cont.) Insertion sort—a simple, but inefficient, sorting

algorithm. The first iteration of this algorithm takes the second

element and, if it’s less than the first element, swaps it with the first element (i.e., the program inserts the second element in front of the first element).

The second iteration looks at the third element and inserts it into the correct position with respect to the first two elements, so all three elements are in order.

At the ith iteration of this algorithm, the first i elements in the original array will be sorted.

Page 30: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 31: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.

Page 32: Problem: ◦ Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning.

©1992-2010 by Pearson Education, Inc. All Rights

Reserved.