const MAX := 5 score(1) := 10 score(2) := 20 score(3) := 30 score(4) := 40 score(5) := 50That way you can trace through the code to see what it does. The code will work for any value of MAX but 5 is big enough to see what's happening but not so big it would take too long to trace the code.
Value of i Value in total ---------- ------------------------------------------------------------------------------------ 1 total := total + score(i) -> total := 0 + score(1) -> total := 0 + 10 -> total := 10 2 total := 10 + score(2) -> total := 10 + 20 -> total := 30 3 total := 30 + score(3) -> total := 30 + 30 -> total := 60 4 total := 60 + score(4) -> total := 60 + 40 -> total := 100 5 total := 100 + score(5) -> total := 100 + 50 -> total := 150
Value of i Value in total ---------- ------------------------------------------------------------------------------------ 1 copy(i) := score(MAX - i + 1) -> copy(1) := score(5 - 1 + 1) -> copy(1) := score(5) 2 copy(2) := score(5 - 2 + 1) -> copy(2) := score(4) 3 copy(3) := score(5 - 3 + 1) -> copy(3) := score(3) 4 copy(4) := score(5 - 4 + 1) -> copy(4) := score(2) 5 copy(5) := score(5 - 5 + 1) -> copy(5) := score(1)
var value : array -5 .. 5 of intStores the values of the squares of the numbers from -5 to 5 in the array values
var wage : array 'A' .. 'E' of realLet's the user enter the hourly wage for the different job categories and prints it. Note: the :5:2 in the put statement is so the hourly wage will always have exactly 2 decimal places
var switch : array 0 .. 5 of booleanAssigns the value false to every element in the array.
const LIST_SIZE := 10 % note: works for any number, doesn't have to be 10 var list : array 1 .. LIST_SIZE of int % note: it works for any type of array, doesn't have to be int var temp : int % note: needs to be whatever type the array is (in this case int)Reverses the array. Suppose in our case LIST_SIZE := 5. The loop will be for i : 1 .. 5 div 2 so it will only execute 1 .. 2
Value of i Value in total ---------- ------------------------------------------------------------------------------------ 1 temp := list(i) -> temp := list(1) -> temp := 10 list(i) := list(LIST_SIZE - i + 1) -> list(1) := list(5 - 1 + 1) -> list(1) := list(5) -> list(1) := 50 list(LIST_SIZE - i + 1) := temp -> list(5 - 1 + 1) := 10 -> list(5) := 10 Note: first and last entry in array have been swapped: array is now 50 20 30 40 10 2 temp := list(i) -> temp := list(2) -> temp := 20 list(i) := list(LIST_SIZE - i + 1) -> list(2) := list(5 - 2 + 1) -> list(2) := list(4) -> list(2) := 40 list(LIST_SIZE - i + 1) := temp -> list(5 - 2 + 1) := 20 -> list(4) := 20 Note: second and second last entry in array have been swapped: array is now 50 40 30 20 10
const LIST_SIZE := 10 % note: works for any number, doesn't have to be 10 var list : array 1 .. LIST_SIZE of int % note: it works for any type of array, doesn't have to be int var temp : int % note: needs to be whatever type the array is (in this case int)The array will not be changed. This is because the array is reversed twice. The loop this time will be for i : 1 .. 5. It does the same thing as part (d) for the first 2 times through the loop. But it keeps going so it reverses the array again - back to original order.
Value of i Value in total ---------- ------------------------------------------------------------------------------------ 1 array is now 50 20 30 40 10 2 array is now 50 40 30 20 10 3 array is now 50 40 30 20 10 (3rd element swapped with 3rd last element (itself) 4 array is now 50 20 30 40 10 (4th element swapped with 4th last element (second element) 5 array is now 10 20 30 40 50 (5th element swapped with 5th last element (first element)
for i : 1 .. SAMPLE_SIZE sample(i) := 0 end for
sample(3) := 28
sample(8) := sample(1)
sample(9) := sample(1) + sample(2)
for i : 1 .. SAMPLE_SIZE sample(i) := 2 * sample(i) end for
for i : 1 .. SAMPLE_SIZE sample(i) := abs(sample(i)) end for
for i : 1 .. SAMPLE_SIZE if i mod 2 = 1 then put sample(i) end if end for
Could also be done more efficiently like this:
for i : 1 .. SAMPLE_SIZE by 2 put sample(i) end for
var temp : int := sample(1) sample(1) := sample(2) sample(2) := temp
var value : array 1 .. 100 of int for count : 1 .. 100 get value(count) end for
var inventory : array 1 .. 4 of int := init (1 ,2, 3, 4) for size : 1 .. 4 put inventory(size), " of size ", size end for
const FIRST_CHOICE := 'A' const LAST_CHOICE := 'E' var total : array FIRST_CHOICE .. LAST_CHOICE of int var response : char % Initialize array for counting responses. for i : FIRST_CHOICE .. LAST_CHOICE total(i) := 0 end for % Read responses and increment corresponding array location to count the % frequency of each response. loop get response exit when response = '$' total(response) := total(response) + 1 end loop % Write summary of responses. for i : FIRST_CHOICE .. LAST_CHOICE put "There were ", total(i), " ", i, "s." end for
ECCBBBEAAEDCACBBEAEDBBC$ There were 4 As. There were 7 Bs. There were 5 Cs. There were 2 Ds. There were 5 Es. |
var numbers : array 1 .. 10 of real var maximum : real for i : 1 .. 10 get numbers(i) end for maximum := numbers(1) for i : 2 .. 10 if numbers(i) > maximum then maximum := numbers(i) end if end for put "The maximum value is ", maximum
var numbers : array 1 .. 10 of real var maximum, minimum: real for i : 1 .. 10 get numbers(i) end for maximum := numbers(1) minimum := numbers(1) for i : 2 .. 10 if numbers(i) > maximum then maximum := numbers(i) end if if numbers(i) < minimum then minimum := numbers(i) end if end for put "The maximum value is ", maximum put "The minimum value is ", minimum
% Note: this wouldn't work if we needed to use the original values again somewhere in the program var number : real var maximum, minimum : real get number minimum := number maximum := number for i : 2 .. 10 get number if number > maximum then maximum := number end if if number < minimum then minimum := number end if end for put "The maximum value is ", maximum put "The minimum value is ", minimum
var numbers : array 0 .. 100 of int var maximum : int := 0 var mark, count : int := 0 % initialize array for i : 0 .. 100 numbers(i) := 0 end for % Enter marks loop put "Enter mark, -1 to end" get mark exit when mark < 0 numbers(mark) := numbers(mark) + 1 end loop % Find the mode number for i : 0 .. 100 if numbers(i) > maximum then maximum := numbers(i) end if end for % Find how many modes there are for i : 0 .. 100 if numbers(i) = maximum then count := count + 1 end if end for % Print grammatically correct sentence depending on 1 mode or more than 1 mode put "The mode".. if count = 1 then put " is ".. else put "s are ".. end if % Print the mode(s) for i : 0 .. 100 if numbers(i) = maximum then put i, " ".. end if end for put ""