Professional Documents
Culture Documents
Array Processing
Bimo Sunarfri Hantono
Electrical Engineering and Information Technology
Universitas Gadjah Mada
Array Processing
Objective
To introduce arrays and the uses off arrays
To develop pseudocode algorithms for common operations on
arrays
To illustrate the manipulation of single- and two-dimensional arrays
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
Array Processing
One of the most powerful programming tools available
Help to organize a collection of homogeneous data items
(same type and length). The individual data items that make
up the array are called as element of the array
Example:
element(index)
Scores(3) indicated the third exam score
Arrays are an internal data structure that is, they are required
only for the duration of the program in which they are defined
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.
Paired Arrays
Two array with the same number of elements are paired
because they correspond each other.
Example:
A student number ID array and student name array
Paired Arrays
Read_values_into_paired_array
Set max_num_elements to required value
Set index to zero
Read first input value
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 is too small`
ENDIF
END
Searching an Array
The reasons for searching an array may be:
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.
Processing
Prompt for scores
Get scores
Compute class average
Display scores
Display class average
Output
18 exam scores
class_average
Processing
Read integer values
Compute integer average
Compute integer count
Display integer average
Display integer count
Output
integer_average
integer_count
read?
Solution algorithm
Process_integer_array
Set integer_total to zero
Set integer_count to zero
DO index = 1 to 100
integer_total = integer_total + numbers(index)
ENDDO
integer_average = integer_total / 100
DO index = 1 to 100
IF numbers(index) > integer_average THEN
add 1 to integer_count
ENDIF
ENDDO
Display integer_average, integer_count
END
Processing
Read sales records
Validate sales numbers
Print error message
Output
error_message
Shipping 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
Processing
Prompt for entry weight
Get entry weight
Search shipping weights array
Compute freight charges
Display freight charge
Output
freight_charge
error_message
Two-dimensional Arrays
Where two subscripts are required to locate an element in an
array.
The number of elements is calculated as the product of the
number of rows and the number of columns
Specified by: array(row_index, column_index)
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.00
20.00
27.50
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_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
Summary
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.
Most Common operations on arrays:
loading a set of initial values into the elements of an array
processing the elements of an array
searching an array, using a linear or binary search, for a particular
element
writing out the contents of an array to a report
Thank You