It is possible to use variable identifiers which are related visually, e.g. score1, score2, ... score100. However, the code to find the highest score out of even five variables that are named in this fashion is both repetitive and long.
high_score := score1; if ( score2 > high_score ) then high_score := score2; if ( score3 > high_score ) then high_score := score3; if ( score4 > high_score ) then high_score := score4; if ( score5 > high_score ) then high_score := score5
It would become absolutely unbearable to write and read code which would repeatedly access these variables for various purposes, such as finding the lowest, the average, the median, the mode, or reading the values from the keyboard, or printing them out on the screen.
High-level computer languages, such as Pascal and C, solve this problem by using a data type called an array. This data type allows you to use a single identifier for a whole set of values of the same type, e.g., all integers or all chars.
One-dimensional arrays are a simple matter once you have worked with strings, which can be considered one-dimensional arrays of chars.
Here we will have a look at an array of scores of type integer. A complete sample program is provided at the end of this file.
Consider an array of integers as declared below.
scores : array [1..100] of integer;
scores is the identifier for this array which can hold up to 100 integer values. Individual elements are accessed using the identifier and a subscript, which must be one of a range of ordinals. Here the ordinal (integer) subscripts range from 1 to 100. This array can be diagrammed as follows:
| Value | 55 | 74 | . . . | 56 | 52 |
| Subscript | 1 | 2 | . . . | 99 | 100 |
| Component Name | scores[1] | scores[2] | . . . | scores[99] | scores[100] |
To assign the value 74 to the second element in the array, you could use the following statement.
scores[2] := 74;
Initializing all of the values to zero is a simple matter:
for i := 1 to 100 do scores[i] := 0;
To find the sum of all of the values in the array, the following would suffice:
sum := 0; for i := 1 to 100 do sum := sum + scores[i];
To be useful, however, unless the array is always full, we must either use one of the elements as an end-of-list marker or keep a count in another variable. Here we will look at the latter technique using the following variable.
num_scores : integer;
This is a program which initializes an array of integer scores with 5 values and then finds and prints the highest score.
Program FindHighest (output);
var
scores : array [1..100] of integer;
num_scores : integer;
high_score : integer;
i : integer;
begin
{ initialize counter }
num_scores := 0;
{ initialize array & count elements }
scores[1] := 55;
num_scores := num_scores + 1;
scores[2] := 74;
num_scores := num_scores + 1;
scores[3] := 45;
num_scores := num_scores + 1;
scores[4] := 88;
num_scores := num_scores + 1;
scores[5] := 67;
num_scores := num_scores + 1;
{ initialize high_score }
high_score := scores[1];
{ look for higher score,
replacing high_score value, if higher one found }
for i := 2 to num_scores do
if ( scores[i] > high_score) then
high_score := scores[i];
{ report findings }
write ( high_score, ' is the highest score out of ');
for i := 1 to num_scores - 1 do
write ( scores[i], ', ');
writeln( 'and ', scores[num_scores], '.')
end.