320 likes | 534 Views
CIS162AB - C++. Array Processing Juan Marquez 08_arrays2.ppt. Overview of Topics. Declaring Arrays - Reviewed Loading Arrays - Reviewed Processing Arrays Sorting Arrays Searching Arrays Parallel Arrays. Array Processing. Declare Array Load Array
E N D
CIS162AB - C++ Array Processing Juan Marquez08_arrays2.ppt
Overview of Topics • Declaring Arrays - Reviewed • Loading Arrays - Reviewed • Processing Arrays • Sorting Arrays • Searching Arrays • Parallel Arrays
Array Processing • Declare Array • Load Array • After creating the array, data must be loaded. • Use constant variables with the arraySize to prevent out of range errors. • Process Array • Use elements in calculations or function calls. • Send entire arrays to functions for processing. • Sort, Search, Display • Use a lot of for loops.
Declaring Arrays • Arrays are allocated consecutive memory. • Each element is referenced using an index. • Index are integers. • The number of elements that can be indexed is the declared size (arraySize). • The first element in the array is referenced with an index of zero. • The last index should be the arraySize – 1.
Array Management • After creating the array, the size is not stored anywhere. • Store the arraySize in a variable. • The programmer must make sure that the logic in the program does not allow the index to go out of range. • Individual array elements can be sent to functions as regular arguments. • An entire array can be sent to functions using array parameters. • The size of array is important for processing, so it must also be passed to functions.
Occurs when the array size is larger than needed. Array size is set for maximum entries, but may not always be filled. Use MAX_SIZE to control loading. Count the entries as loaded. Then use the count as the “array size” for processing (ie: numberUsed, numbersEntered). Partially Filled Arrays
Loading Partially Filled Arrays const int MAX_SIZE = 20; int testScore[MAX_SIZE], numbersEntered, i ; cout << “Enter up to 20 test scores, or a negative value to end input: “; for (i = 0; i < MAX_SIZE; i++) {cin >> testScore[i]; //value 50, 40, 100, 30, 10, 20, -1if ( testScore[i] < 0 ) //index 0 1 2 3 4 5 6 { break;} } numbersEntered= i;
Using numbersEntered • The testScore array can hold up to 20 scores. • We entered 6 scores and a –1 to end input. • Only 6 scores should be processed, not the –1. //numbersEntered is 6, so index will be 0 – 5. void displayTestScores(int testScore[ ], int numbersEntered) { for (int i = 0; i < numbersEntered; i++) { cout << testScore[i]; } }
Sorting Arrays • Data is always being sorted. • It is important that we understand how sorts work. • There are various sort algorithms. • We’ll only be looking at the simple Selection Sort. • Selection Sort Algorithm: • Find the lowest value in the array. • Move it to the top of the array. • Move the value that was at the top to where the lowest value came from. • Find the next lowest value, and move it to the 2nd position. • Continue until the end of the array is reached.
sortArrayAscending( ) part 1 void sortArrayAscending(int testScore[ ], int numbersEntered) { int minIndex, minValue, holdValue; for (int i = 0; i < (numbersEntered - 1); i++) //walk through array { //outer loop keeps track minIndex = i; //of where the next value minValue = testScore[i]; //should be placed. for (int i2 = i + 1; i2 < numbersEntered; i2++) { //inner loop finds the if (testScore[i2] < minValue) //lowest value to move { minIndex = i2; //save the new low number found minValue = testScore[i2]; } }
sortArrayAscending( ) part 2 holdValue = testScore[i]; //swap the values testScore[i] = testScore[minIndex]; testScore[minIndex] = holdValue; } return; }
Sort Algorithm Demonstration const int MAX_SIZE = 20; int testScore[MAX_SIZE];
Load Array Read 6 numbers from keyboard and load them into array. -1 end input. Numbers Entered = i i = 6
sortArrayAscending( ) 1st position Move the value in the 1st position to holdValue = 50. Find the lowest value and move it to the 1st position. Move holdValue to the position that the lowest value was found, swap values.
sortArrayAscending( ) 2nd position Move the value in the 2nd position to holdValue = 40. Find the next lowest value and move it to the 2nd position. Move holdValue to the position that the lowest value was found.
sortArrayAscending( ) 3rd position Move the value in the 3rd position to holdValue = 100. Find the next lowest value and move it to the 3rd position. Move holdValue to the position that the lowest value was found.
sortArrayAscending( ) 4th position Move the value in the 4th position to holdValue = 100. Find the next lowest value and move it to the 4th position. Move holdValue to the position that the lowest value was found.
sortArrayAscending( ) 5th position Move the value in the 5th position to holdValue = 50. A lower value than 50 will not be found, but the comparison must still be made.
sortArrayAscending( ) part 1 void sortArrayAscending(int testScore[ ], int numbersEntered) { int minIndex, minValue, holdValue; for (int i = 0; i < (numbersEntered - 1); i++) //walk through array { //outer loop keeps track minIndex = i; //of where the next value minValue = testScore[i]; //should be placed. for (int i2 = i + 1; i2 < numbersEntered; i2++) { //inner loop finds the if (testScore[i2] < minValue) //lowest value to move { minIndex = i2; //save the new low number found minValue = testScore[i2]; } }
Sequential Search Algorithm • Before searching, the data is usually sorted first. • Look through the array elements from the first to the last looking for a match. • After a match is found, we are going to do something with item found later, so the index of where the item was found must be saved. • If the item was not found, we also need to record this using a flag or special value, so that the error can be reported.
searchArray( ) – part 1 void searchArray(int testScore[ ], int numbersEntered) { sortArrayAscending(testScore, numbersEntered); int searchNumber; boolean numberFound = false; cout << “Enter a number to search for: "; cin >> searchNumber;
searchArray( ) - part 2 for(int i=0; i < numbersEntered; i++) { if (searchNumber == testScore[i]) //check if the value entered is { //= to the current array element numberFound = true; cout << "\n" << searchNumber << " is stored in array position “ << (i + 1) << " and is \nreferenced with an index value of “ << i << ".\n"; break; //get out of for-loop } else if (searchNumber < testScore[i]) //Early exit { cout << “Early exit…”; break; //get out of for-loop } }
searchArray( ) – part 3 if (false == numberFound) cout << searchNumber << " is not on the list.\n"; return; }// end of searchArray()
searchArray( ) – Match Found searchNumber = 40. For loop walks through array checking if testScore[i] = searchNumber. When a match is found, the position and index values are displayed (4, 3).
searchArray( ) – Early Exit searchNumber = 25. In the for loop we also check if searchNumber < testScore[i] . If it is, then we know that we will not find the value. We can exit search. Must be sorted.
Parallel Arrays • Arrays can only store a collection of data of the same data type (int, double, etc.). • Use parallel arrays when you have different data types or data that shouldn’t be grouped together such as an employee Id and their hours. • Corresponding arrays can be created, because each element in one array is associated with the element in the same relative position in the other array.
Parallel Array Example empId lastName payRate This data is of different data types, so it can’t be stored in the same array. However, the associated data for each employee is located in the same relative location in each of the arrays. The first entry in each array belongs to Smith.
Processing Parallel Arrays • Parallel arrays make it easy to process related data, such as within the same for loop.cin >> empId[i] >> lastName[i] >> payRate[i]; • When sorting parallel arrays, it is important that the data in all of the arrays be swapped, and not just the one that you sorting by.
Sorting by Name Error empId lastName payRate If this data is sorted by lastName, Allen should end up at the top. If the associated empId and payRate are not swapped, Allen will end up with Smith’s data. The worst part is that in C++ we don’t have an undo – the data could not be restored.
Parallel Arrays Assignment • P09 uses parallel arrays to process the employee information.
Summary • Declaring Arrays • Loading Arrays • Processing Arrays • Sorting • Searching • Parallel Arrays