Roles of variables represent tacit knowledge, i.e., knowledge that experts have but cannot articulate unless especially prompted to do so. As a consequence, even though you - as an expert - know the roles, you do not know that you know. In particular, even though you know what a counter - a special case of the role stepper - is, the names of many roles are not immediately clear and the recognition of the role of a variable is at first not obvious.
This page contains example programs that can be used to familiarize oneself with the role concept and the names of roles so that this tacit knowledge becomes more available. You can do this by identifying the role of each variable in the following programs yourself. Our interpretations are provided on a separate page.
program squares (input, output); (* Print two squares *) var distance : integer; (* Distance between squares *) begin write('Enter the distance between squares: '); readln(distance); writeln; writeln('***', ' ' : distance, '***'); writeln('* *', ' ' : distance, '* *'); writeln('***', ' ' : distance, '***') end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program time (input, output); (* Transform time to seconds *) type hour = 0..23; minute = 0..59 second = 0..59; var hours : hour; (* Hour of the time *) mins : minute; (* Minute of the time *) secs : second; (* Second of the time *) begin write('Enter time (hour min sec , e.g., 13 30 5): '); readln(hours, mins, secs); writeln ('At ', hours : 2, '.', mins : 2, '.', secs : 2, secs + (60 * (mins + 60 * hours)) : 6, ' seconds has elapsed.') end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program multiplication (input, output); (* Print a multiplication table *) var size, (* Size of table *) row, (* Row index *) col: integer; (* Column index *) begin write('Enter size: '); readln(size); writeln (' ' : 15, 'Multiplication table '); write (' ' : 4); for col := 1 to 10 do write(col : 4); writeln; for row := 1 to size do begin write(row : 4); for col := 1 to 10 do write(row*col : 4); writeln end end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program occur (input, output); (* Count occurences of a value in an array *) const last = 7; type ArrayType = array [1..last] of integer; var value: ArrayType; (* Array of values *) i, (* Array index *) key, (* Value to search for *) count: integer; (* Count of occurences *) begin writeln('Enter ', last, ' values: '); for i := 1 to last do read(value[i]); readln; write('Enter value to search for: '); readln(key); count := 0; for i := 1 to last do if value[i] = key then count := count + 1; writeln('There are ', count, ' occurences of ', key, ' in the array'); end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program square (input, output); (* Drawing a square *) const maxSide = 78; (* Max length for sides *) var character : char; (* Character to be used for drawing *) side, (* Length of sides *) i, j : integer; (* Counters for side lengths *) begin write('Enter character for drawing: '); readln(character); write('Enter side length : '); readln(side); while (side < 1) or (side > maxSide) do begin writeln('Length incorrect. Re-enter: '); readln(side) end; writeln; for i := 1 to side do begin for j := 1 to side do write(character); writeln end end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program histogram (input, output); (* Draw a histogram *) const longest = 40; (* Longest bar *) var amount : array [1..12] of real; (* Data for drawing *) max : real; (* Maximum data element *) month, (* Current month *) i : integer; begin for month := 1 to 12 do begin write('Enter amount for month ', month : 2, ': '); readln(amount[month]); if i = 1 then max := amount[1] else if max < amount[month] then max := amount[month] end; writeln; for month := 1 to 12 do begin write(month : 2, ': '); for i := 1 to round(amount[month] / max * longest) do write('*'); writeln end end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program diceGame (input, output); (* Simulate a dice game. *) var die1, die2, (* Values of dice thrown *) total1, total2: integer; (* Sums of values of each player *) firstPlayer: boolean; (* First player's turn ? *) begin Randomize; total1 := 0; total2 := 0; firstPlayer := True; while (total1 < 100) and (total2 < 100) do begin die1 := Random(6) + 1; die2 := Random(6) + 1; if firstPlayer then write('First') else write('Second'); writeln(' player throws: ', die1, die2); if firstPlayer then total1 := total1 + die1 + die2 else total2 := total2 + die1 + die2; firstPlayer := not firstPlayer; end; write('Player '); if total1 > total2 then write('First') else write('Second'); write(' player won the game.') end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program closest (input, output); (* Find the closest number *) var original, (* Closest to this one *) data, (* Current input data *) closest: integer; (* Closest found so far *) begin write('Enter any number: '); readln(original); write('Enter a positive number (negative to end): '); readln(data); closest := data; while data >= 0 do begin if abs(data-original) < abs(closest-original) then closest := data; write('Enter a positive number (negative to end): '); readln(data); end; if closest < 0 then writeln('No positive value entered.') else writeln('The closest to ', original, ' was ', closest) end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program growth (input, output); (* Growth of capital on a bank account *) var capital, (* Capital on the bank account *) percent, (* Interest rate *) factor, (* Factor for yearly growth *) interest: real; (* Interest in current year *) years, (* Time to consider *) i : integer; (* Year counter *) inputOk : Boolean; (* Is input valid ? *) begin write('Enter capital (positive or negative): '); readln(capital); inputOk := false; while not inputOk do begin write('Enter interest rate (%): '); readln(percent); write('Enter time (years) : '); readln(years); inputOk := (percent > 0) and (years > 0); if not inputOk then begin writeln; writeln('Invalid data. Re-enter.') end end; writeln; factor := percent / 100; for i := 1 to years do begin interest := capital * factor; capital := capital + interest; writeln('After ', i : 2, 'years: interest is ', interest : 11 : 2, ' and total capital is ', capital : 12 : 2) end end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program saw (input, output); (* Read a sequence of values and check if they form a 'saw': adjacent values go up and then down. *) const last = 7; type ArrayType = array [1..last] of integer; var value: ArrayType; (* Values to be checked *) i: integer; (* Index of array *) up, (* Current direction is up ? *) Ok: Boolean; (* Does saw property still hold ? *) begin writeln('Enter ', last, ' values:'); for i:=1 to last do read(value[i]); up := value[1] < value[2]; Ok := value[1] <> value[2]; i := 2; while Ok and (i < last) do begin Ok := (up and (value[i] > value[i+1])) or (not up and (value[i] < value[i+1])); up := not up; i := i + 1 end; write('Values '); if not Ok then write('do not '); writeln('form a saw.') end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program smoothedAverage (input, output); (* Largest average of three consecutive months *) var month: integer; (* Current month *) current, previous, preceding, (* Data for three months *) average, (* Current average *) largest: real; (* Largest one found so far *) begin write('Enter 1. value: '); readln(preceding); write('Enter 2. value: '); readln(previous); write('Enter 3. value: '); readln(current); largest := (current + previous + preceding) / 3; for month := 4 to 12 do begin preceding := previous; previous := current; write('Enter ', month, '. value: '); readln(current); average := (current + previous + preceding) / 3; if average > largest then largest := average end; writeln('Largest three month average was ', largest) end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
program number (input, output); (* Read a number with embedded commas, e.g., 123,456 *) const sep = ','; var c: char; (* The character read *) val: integer; (* Accumulated value of number *) begin val := 0; write('Enter number: '); read(c); while ((c >= '0') and (c <= '9')) or (c = sep) do begin if c <> sep then val := val * 10 + ord(c) - ord('0'); read(c) end; writeln('Value is ', val) end.
Determine appropriate role for
each of the variables:
| Possible roles are: |
Last updated: February 8, 2006