42
CHAPTER 10 ARRAYS II Applications and Extensions

CHAPTER 10 ARRAYS II Applications and Extensions

Embed Size (px)

Citation preview

Page 1: CHAPTER 10 ARRAYS II Applications and Extensions

CHAPTER 10

ARRAYS IIApplications and

Extensions

Page 2: CHAPTER 10 ARRAYS II Applications and Extensions

In this chapter, you will: Learn how to implement the sequential search

algorithm Explore how to sort an array using the selection

sort algorithm Learn how to implement the binary search

algorithm Discover how to manipulate data in a two-

dimensional array Learn about multidimensional arrays

Page 3: CHAPTER 10 ARRAYS II Applications and Extensions

LIST PROCESSING

A list is defined as a set of values of the same type.

Some of the basic operations that are performed on a list are:

1. Search the list for a given item.

2. Sort the list.

3. Insert an item in the list.

4. Delete an item from the list.

Page 4: CHAPTER 10 ARRAYS II Applications and Extensions

Sequential Searchfound is set to false;

for(loc = 0; loc < length; loc++)if(list[loc] is equal to searchItem){

found is set to trueexit loop

}

if(found)return loc;

elsereturn –1;

If the function seqSearch returns a value greater than or equal to 0, it is a successful search; otherwise, it is an unsuccessful search.

Page 5: CHAPTER 10 ARRAYS II Applications and Extensions

int seqSearch(const int list[], int listLength, int searchItem)

{ int loc;

bool found = false;

for(loc = 0; loc < listLength; loc++) if(list[loc] == searchItem) {

found = true;break;

}

if(found) return loc;else return -1;

}

Page 6: CHAPTER 10 ARRAYS II Applications and Extensions

Sorting a List: Selection Sort• This algorithma. Finds the location of the smallest element

b. Move the smallest element to the beginning of the unsorted list.

for(index = 0; index < length – 1; index++){

a. Find the location, smallestIndex, of the smallest element in list[index]...list[length].

b. Swap the smallest element with list[index]. That is, swap list[smallestIndex] with list[index].

}

Page 7: CHAPTER 10 ARRAYS II Applications and Extensions

Step a:smallestIndex = index; //assume first element is the

//smallest

for(minIndex = index + 1; minIndex < length; minIndex++)

if(list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; //current element in //the list is smaller than //the smallest so far, so

//update smallestIndex

Step b:

temp = list[smallestIndex];list[smallestIndex] = list[index];list[index] = temp;

Page 8: CHAPTER 10 ARRAYS II Applications and Extensions

Before writing the selection sort function, let us apply the above algorithm to sort the following list.

The length of list is 6

Page 9: CHAPTER 10 ARRAYS II Applications and Extensions

Iteration 1: Sort list[0]...list[5]. Step a: Find the index of the smallest element in list[0]...list[5].

Step b: Swap list[smallestIndex] with list[index], that is, list[3] with list[0]. The resulting array is:

Page 10: CHAPTER 10 ARRAYS II Applications and Extensions

Iteration 2: Sort list[1]...list[5]. Here index = 1.Step a: Find the index of the smallest element in list[1]...list[5].

Step b: Swap list[smallestIndex] with list[index]. That is, swap list[1] with list[1].

Page 11: CHAPTER 10 ARRAYS II Applications and Extensions

Iteration 3: Sort list[2]...list[5]. Step a: Find the index of the smallest element in list[2]...list[5].

Step b: Swap list[smallestIndex] with list[index]. That is, swap list[4] with list[2].

Page 12: CHAPTER 10 ARRAYS II Applications and Extensions

Iteration 4: Sort list[3]...list[5]. Step a: Find the index of the smallest element in list[3]...list[5].

Step b: Swap list[smallestIndex] with list[index]. That is, swap list[3] with list[3].

Page 13: CHAPTER 10 ARRAYS II Applications and Extensions

Iteration 5: Sort list[4]...list[5]. Step a: Find the index of the smallest element in list[4]...list[5].

Step b: Swap list[smallestIndex] with list[index]. That is, swap list[5] with list[4].

Page 14: CHAPTER 10 ARRAYS II Applications and Extensions

void selectionSort(int list[], int length){ int index; int smallestIndex; int minIndex; int temp;

for(index = 0; index < length – 1; index++) { //Step a smallestIndex = index;

for(minIndex = index + 1; minIndex < length; minIndex++)

if(list[minIndex] < list[smallestIndex]) smallestIndex = minIndex;

//Step b temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; }}

Page 15: CHAPTER 10 ARRAYS II Applications and Extensions

Sequential search on a sorted listfound is set to false;

for(loc = 0; loc < listLength; loc++)if (list[loc] is greater than or equal to

searchItem){

found is set to trueexit loop

}

if(found) if(list[loc] == searchItem)

return loc; else

return –1;else

return –1;

Page 16: CHAPTER 10 ARRAYS II Applications and Extensions

int seqOrderdSearch(const int list[], int listLength, int searchItem)

{ int loc; //Line 1 bool found = false; //Line 2

for(loc = 0; loc < listLength; loc++)//Line 3 if(list[loc] >= searchItem) //Line 4 {

found = true; //Line 5 break; //Line 6

} if(found) //Line 7 if(list[loc] == searchItem) //Line 8

return loc; //Line 9 else //Line 10

return –1; //Line 11else //Line 12

return –1; //Line 13}

Page 17: CHAPTER 10 ARRAYS II Applications and Extensions

length = 8; searchItem = 35; loc = 0, found = false.

Iteration loc found list[loc] list[loc]>= searchItem1 0 false 4 4 >= 35 is false; loc = 1

2 1 false 18 18 >= 35 is false;loc = 2

3 2 false 29 29 >= 35 is false;loc = 3

4 3 false 35 35 >= 35 is true; found = true

found is true, the if statement at Line 8 executes. list[loc] = 35 and searchItem = 35, the expression

list[loc] == searchItemevaluates to true and the return statement at Line 9 returns the value of loc, which is 3. This is a successful search.

Page 18: CHAPTER 10 ARRAYS II Applications and Extensions

searchItem = 40; loc = 0; found = false. Iteration loc found list[loc] list[loc]>= searchItem1 0 false 4 4 >= 40 is false; loc = 1

2 1 false 18 18 >= 40 is false; loc = 2

3 2 false 29 29 >= 40 is false; loc = 3

4 3 false 35 35 >= 40 is false; loc = 4

5 4 false 44 44 >= 40 is true; found = true

found is true. The if statement at Line 8 executes.

list[loc] = 44 and searchItem = 40. The expression

list[loc] == searchItem

evaluates to false, the statement at Line 9 is skipped, and the return statement at Line 11 executes, which returns –1. This is an unsuccessful search.

Page 19: CHAPTER 10 ARRAYS II Applications and Extensions

Binary Search A binary search is similar to a dictionary search.

A binary search uses the “divide and conquer” technique to search the list.

First, the search item is compared with the middle element of the list.

If the search item is less than the middle element of the list, we restrict the search to the upper half of the list; otherwise, we search the lower half of the list.

Consider the following sorted list of length = 12

Page 20: CHAPTER 10 ARRAYS II Applications and Extensions

• Determine whether 75 is in the list

• Compare 75 with the middle element in the list, list[5] (which is 39). • Because 75 list[5] and 75 > list[5], we then restrict our search to the

list list[6]...list[11]

Page 21: CHAPTER 10 ARRAYS II Applications and Extensions

int binarySearch(const int list[],int listLength,int searchItem)

{int first = 0;int last = listLength - 1;int mid;

bool found = false;

while(first <= last && !found){

mid = (first + last) / 2;

if(list[mid] == searchItem)found = true;

else if(list[mid] > searchItem)

last = mid - 1; else

first = mid + 1;}if(found)

return mid;else return –1;

}//end binarySearch

Page 22: CHAPTER 10 ARRAYS II Applications and Extensions

• searchItem = 89; first = 0, last = 11, and found = false

Iteration first last mid list[mid] Number of Key Comp.

1 0 11 5 39 2

2 6 11 8 66 2

3 9 11 10 89 1 (found is true)

•searchItem = 34; first = 0, last = 11, and found = false

Iteration first last mid list[mid] Number of Key Comp.

1 0 11 5 39 22 0 4 2 19 23 3 4 3 25 24 4 4 4 34 1 (found is true)

Page 23: CHAPTER 10 ARRAYS II Applications and Extensions

searchItem = 22; first = 0, last = 11, and found = false

Iteration first last mid list[mid] Number of Key Comp.

1 0 11 5 39 2

2 0 4 2 19 2

3 3 4 3 25 2

4 3 2 The loop stops (because first > last)

unsuccessful search

Page 24: CHAPTER 10 ARRAYS II Applications and Extensions

Two-dimensional Array: A two-dimensional array is a collection of a fixed number of components arranged in two dimensions, and all components are of the same type.

The syntax for declaring a two-dimensional array is:

dataType arrayName[# of Rows][# of Column];

double sales[10][5];

declares a two-dimensional array sales of 10 rows and 5 columns and every component is of the type float.

Page 25: CHAPTER 10 ARRAYS II Applications and Extensions
Page 26: CHAPTER 10 ARRAYS II Applications and Extensions
Page 27: CHAPTER 10 ARRAYS II Applications and Extensions

Two-Dimensional Array Initialization During DeclarationLike one-dimensional arrays, two-dimensional arrays can be initialized when they are declared.

int board[4][3] = {{2, 3, 1}, {15, 25, 13}, {20,4,7}, {11,18,14}};

Page 28: CHAPTER 10 ARRAYS II Applications and Extensions

Processing Two-dimensional Arraysconst int rows = 7; const int columns = 6; int matrix[rows][columns];int row;int col;int sum;int largest;int temp;

Page 29: CHAPTER 10 ARRAYS II Applications and Extensions
Page 30: CHAPTER 10 ARRAYS II Applications and Extensions

matrix [0][1][2][3][4][5][0]

[1]

[2]

[3]

[4]

[5]

[6]

matrix[5][0]

matrix[5][1]

matrix[5][2]

matrix[5][3]

matrix[5][4]

matrix[5][5]

Initialize the 6th row to zerorow = 5;for(col = 0; col < columns; col++) matrix[row][col] = 0;

Page 31: CHAPTER 10 ARRAYS II Applications and Extensions

Print out the 3rd column to screen output

col = 2;

for(row = 0; row < rows; row++)

cout << matrix[row][col] << endl;

matrix [0][1][2][3][4][5][0]

[1]

[2]

[3]

[4]

[5]

[6]

matrix[3][2]

matrix[4][2]

matrix[5][2]

matrix[6][2]

matrix[0][2]

matrix[1][2]

matrix[2][2]

Page 32: CHAPTER 10 ARRAYS II Applications and Extensions

Print

The following nested for loops print the components of matrix, one row per line.

for(row = 0; row < rows; row++){ for(col = 0; col < columns; col++) cout<<setw(5)<<matrix[row][col]<<" "; cout<<endl;}

Page 33: CHAPTER 10 ARRAYS II Applications and Extensions

Sum of each individual row

for(row = 0; row < rows; row++){ sum = 0; for(col = 0; col < columns; col++) sum = sum + matrix[row][col]; cout<<"Sum of row "<<row+1<<" = "

<<sum<<endl;}

Sum of each individual column

for(col = 0; col < columns; col++){ sum = 0; for(row = 0; row < rows; row++) sum = sum + matrix[row][col]; cout<<"Sum of column "<<col+1<<" = "

<<sum<<endl; }

Page 34: CHAPTER 10 ARRAYS II Applications and Extensions

//Largest element in each row

for(row = 0; row < rows; row++)

{

largest = matrix[row][0];

for(col = 1; col < columns; col++)

if(largest < matrix[row][col])

largest = matrix[row][col];

cout<<"Largest element of row "<<row+1

<<" = "<<largest<<endl;

}

Page 35: CHAPTER 10 ARRAYS II Applications and Extensions

//Largest element in each column

for(col = 0; col < columns; col++)

{

largest = matrix[0][col];

for(row = 1; row < rows; row++)

if(largest < matrix[row][col])

largest = matrix[row][col];

cout<<"Largest element of col "<<col+1

<<" = "<<largest<<endl;

}

Page 36: CHAPTER 10 ARRAYS II Applications and Extensions

Passing Two-dimensional Arrays as Parameters to Functions

• Two-dimensional arrays can be passed as parameters to a function and they are passed by reference.

• The base address, that is, the address of the first component of the actual parameter is passed to the formal parameter.

• When storing a two-dimensional array in the computer’s memory, C++ uses the row order form. That is, first the first row is stored, followed by the second row, which is followed by the third row and so on.

void example(int table[][5], int rowsize){ . . .}

Page 37: CHAPTER 10 ARRAYS II Applications and Extensions

Arrays of Strings• Arrays of Stings and the string Type

string list[100];

• Arrays of Strings and C- Strings (Character Arrays)

char list[100][16];

Page 38: CHAPTER 10 ARRAYS II Applications and Extensions
Page 39: CHAPTER 10 ARRAYS II Applications and Extensions

strcpy(list[1], "Snow White");

for(j = 0; j < 100; j++)cout<<list[j]<<endl;

Page 40: CHAPTER 10 ARRAYS II Applications and Extensions

Other form of Declaring Two-dimensional Arraysconst int numberOfRows = 20;const int numberOfColumns = 10;

typedef int tableType[numberOfRows][numberOfColumns];

We can declare variables of the type tableType.

tableType matrix;

void initialize(tableType table){

int row; int col;

for(row = 0; row < numberOfRows; row++) for(col = 0; col < numberOfColumns; col++)

table[row][col] = 0;}

Page 41: CHAPTER 10 ARRAYS II Applications and Extensions

Multi-dimensional Arrays• Array: An array is a collection of a fixed number of elements (called

components) arranged in n dimensions (n >= 1), called an n-dimensional array.

The general syntax of declaring an n-dimensional array is:

dataType arrayName[intExp1][intExp2]...[intExpn];

where intExp1, intExp2, … , and intExpn are constant expressions yielding positive integer values.

The syntax of accessing a component of an n-dimensional array is:

arrayName[indexExp1][indexExp2]...[indexExpn]

where indexExp1,indexExp2,..., and indexExpn are expressions yielding nonnegative integer values. indexExpi gives the position of the array component in the ith dimension.

Page 42: CHAPTER 10 ARRAYS II Applications and Extensions

The statement declares carDealers to be a three-dimensional array.

double carDealers[10][5][7];

Size of the first dimension is 10 and it ranges from 0 to 9. The size of the second dimension is 5 and it ranges from 0 to 4. The size of the third dimension is 7 and it ranges from 0 to 6. The base address of the array carDealers is the address of the first array

component, that is the address of carDealers[0][0][0]. The total number of components in the array carDealers is 10*5*7 =

350.

carDealers[5][3][2] = 15564.75;

for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++)

carDealers[i][j][k] = 0.0;

initializes the entire array to 0.0.