07 Array Processing
07 Array Processing
Array processing
Objectives
Outline
Array processing
Initializing the elements of an array
Searching an array
Writing out the contents of an array
Programming examples using arrays
Two-dimensional arrays
Chapter summary
Programming problems
73
1.
Array processing
Arrays are one of the most powerful programming tools available. They provide the
programmer with a way of organizing a collection of homogeneous data items (that is, items that
have the same type and the same length) into a single data structure. An array, then, is a data
structure that is made up of a number of variables all of which have the same data type for
example, all the exam scores for a class of 30 mathematics students. By using an array, a single
variable name such as scores can be associated with all 30 exam scores.
The individual data items that make up the array are referred to as the elements of the array.
Elements in the array are distinguished from one another by the use of an index or subscript,
enclosed in parentheses, following the array name for example: scores (3).
The subscript indicates the position of an element within the array; so, scores (3) refers to the
third exam score, or the third element of the array scores, and scores (23) refers to the 23rd
exam score.
The subscript or index may be a number or a variable, and may then be used to access any item
within the valid bounds of an array for example:
scores(6), or
element(index)
Arrays are an internal data structure that is, they are required only for the duration of the
program in which they are defined. They are a very convenient mechanism for storing and
manipulating a collection of similar data items in a program, and a programmer should be
familiar with the operations most commonly performed on them. Arrays are sometimes referred
to as tables.
Operations on arrays
Arrays can be used:
to load initial information into an array that is later required for processing
to process the elements of the array
to store the results of processing into the elements of an array, or
to store information, which is then written to a report.
Usually, the elements of an array are processed in sequence, starting with the first element. This
can be accomplished easily in pseudocode with either a DO loop or a DOWHILE loop.
74
been established and the subscript is named index. The number of elements in the array is
stored in the variable number_of_elements.
75
then looks at the other elements of the array to see if a smaller value exists. The smallest value is
then printed.
Find_smallest_element
Set smallest_element to array (1)
DO index = 2 to number_of_elements
IF array (index) < smallest_element THEN
smallest_element = array (index)
ENDIF
ENDDO
Print smallest_element
END
2.
Because an array is an internal data structure, initial values must be placed into the array before
any information can be retrieved from it. These initial values can be assigned to the elements of
the array as constants, or they can be read into the array from a file.
76
END
Note that each element of the array must be the size of the largest month that is, September
so, the shorter month names must be padded with blanks.
Note that the processing will terminate when either the input file has reached EOF or the array
is full. An error message will be printed if there are more input data items than there are
elements in the array.
77
Note that the processing will terminate when either the sentinel record has been read or the
array is full. An error message will be printed if there are more input data items than there are
elements in the array.
Paired arrays
Many arrays in business applications are paired that is, there are two arrays that have the
same number of elements. The arrays are paired because the elements in the first array
correspond to the elements in the same position in the second array. For example, a sales
number array can be paired with a sales name array. Both arrays would have the same number
of elements, with corresponding sales numbers and sales names. When you have determined
where in the sales number array a particular salespersons number appears, retrieve the
salespersons name from the corresponding position in the sales name array. In this way, the
salespersons number and name can appear on the same report, without any extra keying.
In the following example, an algorithm has been designed to read a file of product codes and
corresponding selling prices for a particular company and to load them into two corresponding
arrays, named product_codes and selling_prices. In the algorithm, the subscript is index, and
the field max_num_elements contains the total number of elements in each array.
Read_values_into_paired_arrays
Set max_num_elements to required value
Set index to zero
Read first input record
DOWHILE (NOT EOF input record) AND (index < max_num_elements)
index = index + 1
product_codes (index) = input product_code
selling_prices (index) = input selling_price
Read next record
ENDDO
IF (NOT EOF input record) AND index = max_num_elements THEN
Print Array size too small
ENDIF
END
3.
Searching an array
A common operation on arrays is to search the elements of an array for a particular data item.
The reasons for searching an array may be:
78
to edit an input value that is, to check that it is a valid element of an array
to retrieve information from an array
to retrieve information from a corresponding element in a paired array.
When searching an array, it is an advantage to have the array sorted into ascending sequence,
so that, when a match is found, the rest of the array does not have to be searched. If you find an
array element that is equal to an in put entry, a match has been found and the search can be
stopped. Also, if you find an array element that is greater than an input entry, no match has
been found and the search can be stopped. Note that if the larger entries of an array are
searched more often than the smaller entries, it may be an advantage to sort the array into
descending sequence.
An array can be searched using either a linear search or a binary search.
79
position of the section of the table being searched, and high_element indicates the top position.
The maximum number of elements that the array can hold is placed in the variable
max_num_elements.
The binary search will continue until the data item has been found, or there can be no more
halving operations (that is, low_element is not less than high_element).
Binary_search_ of_an_array
Set element_found to false
Set low_element to 1
Set high_element to max_num_elements
DOWHILE (NOT element_found) AND (low_element < = high_element)
index = (low_element + high_element) / 2
IF input_value = array (index) THEN
Set element_found to true
ELSE
IF input_value < array (index) THEN
high_element = index - 1
ELSE
low_element = index + 1
ENDIF
ENDIF
ENDDO
IF element_found THEN
Print array (index)
ELSE
Print value not found, input_value
ENDIF
END
4.
The elements of an array can be used as accumulators of data, to be written to a report. Writing
out the contents of an array involves starting with the first element of the array and continuing
until all elements have been written. This can be represented by a simple DO loop.
In the following pseudocode algorithm, the name of the array is array and the subscript is
index. The number of elements in the array is represented by number_of_elements.
Write_values_of_array
DO index = 1 to number_of_elements
Print array (index)
ENDDO
END
5.
80
A Defining diagram
Input
Processing
Output
18 exam scores
18 exam scores
class_average
C Solution algorithm
Process_exam_scores
Set total_score to zero
DO index = 1 to 18
Prompt operator for score
Get scores (index)
total_score = total_score + scores (index)
ENDDO
Compute average_score = total_score / 18
DO index = 1 to 18
Display scores (index)
ENDDO
Display average_score
END
A Defining diagram
Input
Processing
Output
integer_average
integer_count
81
A Defining diagram
Input
Processing
Output
sales records
sales_number
error_message
82
index = index + 1
ENDIF
ENDDO
IF NOT element_found THEN
Print invalid sales number, input sales number
ENDIF
Read sales record
ENDDO
END
Freight charges
1-100
3.00
101-500
5.00
501-1000
7.50
1001-3000
12.00
3001-5000
16.00
5001-9999
35.00
A Defining diagram
Input
Processing
Output
entry weight
freight_charge
error_message
83
add 1 to index
ELSE
Set element_found to true
ENDIF
ENDDO
IF element_found THEN
freight_charge = freight_charges (index)
Display Freight charge is, freight_charge
ELSE
Display invalid shipping weight, entry weight
ENDIF
END
6.
Two-dimensional arrays
So far, all algorithms in this chapter have manipulated one-dimensional arrays that is, only
one subscript is needed to locate an element in an array. In some business applications, there is
a need for multidimensional arrays, where two or more subscripts are required to locate an
element in an array. The following freight charges array is an example of a two-dimensional
array, and is an extension of Example 7.9 above. It is a two-dimensional array because the
calculation of the freight charges to ship an article depends on two values: the shipping weight of
the article, and the geographical area or zone to which it is to be shipped, namely zones 1, 2, 3
or 4.
The range of shipping weights, in grams, is provided in the same one-dimensional array as in
Example 7.9, as follows.
Shipping weights
(grams)
1-100
101-500
501-1000
1001-3000
3001-5000
5001-9999
Freight charges ($) (by shipping zone)
1
2.50
3.50
4.00
5.00
3.50
4.00
5.00
6.50
4.50
6.50
7.50
10.00
10.00
11.00
12.00
13.50
13.50
16.10
20.00
27.50
32.00
34.00
35.00
38.00
In the freight charges array, anyone of four freight charges may apply to a particular shipping
weight, depending on the zone to which the shipment is to be delivered. Thus, the array is set
84
out as having rows and columns, where the six rows represent the six shipping weight ranges,
and the four columns represent the four geographical zones.
The number of elements in a two-dimensional array is calculated as the product of the number
of rows and the number of columns. in this case, 6 x 4 = 24.
An element of a two-dimensional array is specified using the name of the array, followed by two
subscripts, enclosed in parentheses, separated by a comma. The row subscript is specified first,
followed by the column subscript. Thus, an element of the above freight charges array would be
specified as freight_charges (row_index, column_index). So, freight_charges (5, 3) refers to the
freight charge listed in the array where the fifth row and the third column intersect that is, a
charge of $20.00.
85
First, the one-dimensional shipping_weights array is searched for the correct weight category
(row_index) and then the two-dimensional freight_charges array is looked up using that weight
category (row_index) and geographical zone (column_index) .
Calculate_Freight_Charges
Set row index to 1
Set element_found to false
Prompt for shipping_weight, zone
Get shipping_weight, zone
DOWHILE (NOT element_found) AND (row_index <= 6)
IF shipping_weights (row_index) < input shipping_weight THEN
add 1 to row_index
ELSE
Set element_found to true
ENDIF
ENDDO
IF element_found THEN
IF zone = (1 or 2 or 3 or 4) THEN
freight_charge = freight_charge (row_index, zone)
Display Freight charge is, freight_charge
ELSE
Display invalid zone, zone
ENDIF
ELSE
Display invalid shipping weight, input shipping_weight
ENDIF
END
Search method 2
In the following algorithm, an input employee number is validated against a two-dimensional
array of employee numbers, which has 10 rows and five columns. The array is searched
sequentially, by columns within rows, using two DOWHILE loops until a match is found. If no
match is found, an error message is printed.
Search_employee_numbers
Set row_index to 1
Set employee_found to false
Read input employee_number
DOWHILE (NOT employee_found) AND (row_index <=10)
Set column_index to 1
DOWHILE (NOT employee_found) AND (column_index <= 5)
IF employee_numbers (row_index, column_index) = input
employee_number THEN
Set employee_found to true
ENDIF
column_index = column_index + 1
ENDDO
row_index = row_index + 1
ENDDO
IF NOT employee_found THEN
Print invalid employee number, input employee_number
ENDIF
END
86
Chapter summary
This chapter defined an array as a data structure made up of a number of variables or data
items that all have the same data type and are accessed by the same name. The individual
elements that make up the array are accessed by the use of an index or subscript beside the
name of the array for example, scores (3).
Algorithms were developed for the most common operations on arrays, namely:
Programming examples using both one and two-dimension arrays were developed.
87