Flashcards in 2.5 Role of a Variable Deck (8)

Loading flashcards...

1

## Fixed Value

###
The role of a variable is a fixed value if its value is not changed while the program is running after initialization.

The example program asks user the radius of a circle and then prints out the area of that circle. Variable radius is a fixed value. This variable gets its value (on line 7) during the program’s run once only and the value never changes after that. A fixed value can be used in different places in a program - twice on line 8 in the example.

(*1*) PROGRAM AreaOfCircle (input,output);

(*2*) CONST PI = 3.14;

(*3*) VAR radius: real;

(*4*) BEGIN

(*5*) writeln;

(*6*) write('Enter the radius of a circle: ');

(*7*) readln(radius);

(*8*) writeln('The area of the circle is ', PI * radius * radius)

(*9*) END.

2

## Stepper

###
Stepper goes through a succession of values in some systematic way.

Below is an example of a loop structure where the variable multiplier is used as a stepper. The example program outputs a multiplication table while the stepper goes through the values from one to ten.

(*1*) PROGRAM MultiplicationTable (output);

(*2*) VAR multiplier: integer;

(*3*) BEGIN

(*4*) FOR multiplier := 1 TO 10 DO

(*5*) writeln(multiplier, ' * 3 = ', multiplier*3)

(*6*) END.

A stepper can be used, for example, for counting and traversing an array.

3

## Most-recent holder

###
A variable is a most-recent holder if its value is the latest gone through value of a certain group or simply the latest input value.

The example program repeatedly (on line 6) asks the user for the input until the input is valid. In this program the variable side is a most-recent holder since it holds the latest input value at the time.

(*1*) PROGRAM AreaOfSquare (input,output);

(*2*) VAR side: real;

(*3*) BEGIN

(*4*) REPEAT

(*5*) write('Enter the length of the side of a square: ');

(*6*) readln(side)

(*7*) UNTIL side > 0;

(*8*) writeln('The area of the square is ', side * side)

(*9*) END.

4

## Most-wanted holder

###
The value of a most-wanted holder is the best or otherwise the most-wanted value out of the values gone through so far. There are no restrictions for measuring the superiority of the values: the most-wanted can mean, for example, the smallest or the biggest number or a number closest to a certain value.

The example program finds out which is the smallest of the ten integers given as an input. The variable smallest is a most-wanted holder since it is given (on line 8) the most recent value if it is smaller than the smallest one so far.

(*1*) PROGRAM SearchSmallest (input,output);

(*2*) VAR i, smallest, number: integer;

(*3*) BEGIN

(*4*) write('Enter the first number: '); readln(smallest);

(*5*) FOR i := 2 TO 10 DO

(*6*) BEGIN

(*6*) write('Enter number ', i, ': ');

(*7*) readln(number);

(*8*) IF number < smallest THEN smallest := number

(*9*) END;

(*10*) writeln ('The smallest number was ', smallest)

(*11*) END.

The variable i is a stepper and number is a most-recent holder.

5

## Gatherer

###
The value of a gatherer is to accumulate all the values gone through so far.

The example program accepts integers given as an input one by one until the user inputs the number -999 after which the program calculates the mean value of the inputs. The variable sum is a gatherer: the total of the inputs is gathered (on line 10) in it.

(*1*) PROGRAM MeanValue (input,output);

(*2*) VAR count: integer;

(*3*) sum, number: real;

(*4*) BEGIN

(*5*) sum := 0;

(*6*) count := 0;

(*7*) REPEAT

(*8*) write('Enter a number, -999 to quit: ');

(*9*) readln(number);

(*10*) IF number <> -999 THEN sum := sum + number;

(*11*) IF number <> -999 THEN count := count + 1

(*12*) UNTIL number = -999;

(*13*) IF count > 0 THEN writeln('The mean value is ', sum / count)

(*14*) END.

The variable count is a stepper and number is a most-recent holder.

6

## Follower

###
A follower always gets the old value of another known variable as its new value. The example program asks the user for twelve integers and finally outputs the biggest difference between the two successive input integers. The variable previous is a follower: it follows the variable current (on line 9).

(*1*) PROGRAM BiggestDifference (input,output);

(*2*) VAR month, current, previous, biggestDifference: integer;

(*3*) BEGIN

(*4*) write('Enter the first value: '); readln(previous);

(*5*) write('Enter the second value: '); readln(current);

(*6*) biggestDifference := current - previous;

(*7*) FOR month := 3 TO 12 DO

(*8*) BEGIN

(*9*) previous := current;

(*10*) write('Enter the value for month: ', month);

(*11*) readln(current);

(*12*) IF (current – previous) > biggestDifference THEN

(*13*) biggestDifference := current - previous

(*14*) END;

(*15*) writeln('The biggest difference was ', biggestDifference)

(*16*) END.

The variable month is a stepper, current is a most-recent holder and biggestDifference is a most-wanted holder.

Followers are used a lot with linked data structures to point to the data element previous to the one in process.

7

## Transformation

###
A variable is a transformation variable if it always gets its new value from a fixed calculation of values of other variables. For example, a program might store the result of a conversion of a measurement in inches to a measurement in centimetres by multiplying the inches value by 2.54.

In the sample program for the temporary role in the next section tax is a transformation variable.

8