cis162ab c n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CIS162AB - C++ PowerPoint Presentation
Download Presentation
CIS162AB - C++

Loading in 2 Seconds...

play fullscreen
1 / 32

CIS162AB - C++ - PowerPoint PPT Presentation


  • 187 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CIS162AB - C++' - carmela


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cis162ab c

CIS162AB - C++

Array Processing

Juan Marquez08_arrays2.ppt

overview of topics
Overview of Topics
  • Declaring Arrays - Reviewed
  • Loading Arrays - Reviewed
  • Processing Arrays
  • Sorting Arrays
  • Searching Arrays
  • Parallel Arrays
array processing
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
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
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.
partially filled arrays
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
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
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
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
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
sortArrayAscending( ) part 2

holdValue = testScore[i]; //swap the values

testScore[i] = testScore[minIndex];

testScore[minIndex] = holdValue;

}

return;

}

sort algorithm demonstration
Sort Algorithm Demonstration

const int MAX_SIZE = 20;

int testScore[MAX_SIZE];

load array
Load Array

Read 6 numbers from keyboard and load them into array.

-1 end input.

Numbers Entered = i

i = 6

sortarrayascending 1 st position
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
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
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
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
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.

slide20

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
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
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
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
searchArray( ) – part 3

if (false == numberFound)

cout << searchNumber << " is not on the list.\n";

return;

}// end of searchArray()

searcharray match found
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
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
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
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
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
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
Parallel Arrays Assignment
  • P09 uses parallel arrays to process the employee information.
summary
Summary
  • Declaring Arrays
  • Loading Arrays
  • Processing Arrays
  • Sorting
  • Searching
  • Parallel Arrays