Monday, 11 February 2013

Arrays


Arrays

Loops allow the programmer to execute multitudes of instruction and manipulate large
amounts of information in a controlled way. Suppose we wanted to write a program that

collected light measurements once every 15 minutes for a whole day. Suppose further that at
the end of the measurement period we wanted to create a bar graph of the light measurements
so that we could visualize the variation of the light measurements throughout the day. To
create the bar graph, we would need to be able to retrieve each and every light measurement
from beginning to end. That is a total of 96 values.
Typically, the way that we store and retrieve values is by placing them into variables.
It would be tedious to have to declare 96 variables in a program, so most programming
languages, RobotC included, allow the programmer to declare arrays of variables. An array
is an indexed collection of variables of the same datatype. Indexed means that each variable
(also called an element) of the array can be accessed using an integer index.
The syntax for declaring an array of variables is
 
[ datatype ] [ variable name ][[ SIZE ]];
 
This is just like an ordinary variable declaration, but the variable name is followed by
an integer in square brackets indicating the number of elements associated with the array
name. In the case of our light measurements, which will be of integer-type, we might use
the declaration
int light_readings[96];
This straightforward declaration quickly gives the programmer 96 variables of the form
light_readings[0], light_readings[1], light_readings[2], : : :, light_readings[95]
to work with. Notice that the rst element of the array has index 0, that subsequent element
indices each increment by one, and that the last element has index 95. Array indices always
start at 0 and end with an index one less than the declared size of the array.
Listing 6.6 shows a snippet of instruction that collects light measurements every 15
minutes for 24 hours.
The program initializes an integer-type counter variable, count, to 0, and declares an
integer-type array of 96 elements for the light readings. The while-loop, with trivial predicate,
takes a reading, increments the counter, and pauses for 15 minutes before repeating. When
the array is full, the break statement causes the loop to terminate. Notice that we may use
the variable count as the array index. It starts at 0 and increments by 1 each time through
the loop so that each light reading is assigned to a di erent array element. Notice also that
the break statement occurs after the counter increment, but before the wait10Msec() calls.
An array bound overrun occurs if you attempt to use an out of range index to access an
array element. This common error can be dicult to nd because the compiler cannot detect
it. Such errors can lead to unpredictable behavior in the same way that uninitialized variables
can. Correcting array bound overruns involves careful inspection of all array operations and
clearly determining the value of any index variables. For example, in the program in Listing
6.6, if we simply swap the order of the counter increment and the break statement, we will
introduce an array bound overrun. Checking the index before incrementing will lead to an
attempt to use the illegal value 96 as an array index the next time through the loop.

# pragma config ( Sensor , S1 , lightsensor , sensorLightInactive )
task main () {
int count =0;
int light_readings [96];
while ( true ) {
light_readings [ count ] = SensorValue [ lightsensor ];
count ++;
if( count == 95) { break ; }
wait10Msec (30000);
wait10Msec (30000);
wait10Msec (30000);
}
//
}
 
Listing 6.6: This program uses a while-loop to collect light sensor readings every 15 min-
utes for 24 hours and store the data in the array light_readings. The three calls to
wait10Msec(30000) are required because the wait10Msec() function can only accept argu-
ments that are less than 32767. To wait 15 minutes requires an argument of 90000 which is
too large.


No comments:

Post a Comment