Chapter 10 Assignment Page

Processing Arrays

Once the data has been input into the arrays, you can process the data. You will learn two methods used to process the data. Using the first method, you will process the entire array. This means you want to start at the first location in the array and perform some type of processing on each element of the array. Using the second method, you will look for a specific item in the array. This means you want to search the array until you find a match. Once a match is found, some type of processing is performed on that item.
 

Processing the entire array

To process the entire array, the computer first needs to point to the first item in the array using a subscript. A counter controlled loop is always used for processing. To be able to process the items in the array, you need to know where to start processing and where to stop processing. Let’s look at an example where we want to process the items in an array of accumulators. Let’s add the numbers together to print a total. Remember these problems only cover the processing portion. The data must already be input into the array before you can perform the processing portion.

Accum
 
1.     23
2.     35
3.     45
4.     56


DOWHILE loop

wpeB.gif (1256 bytes)

DOUNTIL loop

C10AP.1.gif (1256 bytes)

This problem sets an accumulator Total to 0 and POS to 1 (to point to the first location in the array). A counter controlled loop is used to add the value in each location of the array to Total. After all processing is performed, the value in Total is printed.

Example: This module will print the total number of times the name Smith occurs in the array.

Names
 
1.     Jones
2.     Smith
3.     Baker
4.     Smith
5.     Clark
DOWHILE loop

wpeD.gif (1256 bytes)

DOUNTIL loop

wpeD.gif (1256 bytes)

Searching the Array

In this section, we will look at two types of searches, the binary and sequential search.
 

Sequential Search

The sequential search tells the computer to begin at the first item in the array and compare it to the item you are trying to locate. If a match is found, the search is terminated. If a match is not found, the computer will look at the next item in the array. Each array item is look at in sequence until a match is found. This method is a slow method to use if there are many items in the array.

Example: In this module, the computer will locate the first Smith in the array and print out the location of the name in the array.

When using a sequential search, the computer should stop searching when a match is found. A program switch is used to stop processing when the value is found. The computer should also print a message if the value it is searching for is not found.
 

Program Switch

A program switch (boolean variable) is a variable that can only store the value true or false. The program switch is initialized to either true or false and when a match is found, the opposite value is placed in the variable. When the variable contains the correct value, the computer will stop searching. In the example below, the program switch FOUND will be used. The variable will be initialized to FALSE and when a match is found, the variable will be set to TRUE. When the value is TRUE, the computer will stop searching.
 

Value not found

If the value the computer is searching for is not found, at the end of processing the variable FOUND will still be set to FALSE. (the initial value) After all processing has completed, the computer will check the variable FOUND, if it is still set to FALSE, the value was not located and an error message is printed.

Names
 
1.     Jones
2.     Smith
3.     Baker
4.     Smith
5.     Clark
DOWHILE loop

wpeE.gif (1256 bytes)

DOUNTIL loop

wpeE.gif (1164 bytes)

Binary Search

The binary search is covered in Chapter 10 in the textbook. The binary search will cut out the items you need to search through in half, eliminating one half of the items the computer needs to search through. This method only works if the items in the array are sorted in ascending or descending order before the search. Using this method, the computer will locate the item in the middle of the array. (If there are an even number of items, then the program will select up or down to locate the item closest to the middle.) Once the middle item is located, the computer will compare this item to the item you are trying to locate, if there is a match the search is terminated. If there is not a match the computer will search through either the top half of the remaining items or the bottom half of the remaining items to try to locate a match. If the item you are trying to locate is less than the item in the middle, then the top half is searched and the items in the top half is ignored. If the item you are trying to locate is greater than the item in the middle, then the bottom half is searched and the items in the bottom half is ignored. This method automatically ignores one-half of the items the computer needs to search.