Algorithms and Programming eXercises #11
Sub-Programming - Solutions
1. Procedure Greet;
{clears the screen and simply greets the user}
begin
clrscr;
writeln('Hello, and I really mean that')
end; {Greet}
2. Procedure DrawBorder;
{draws a border of asterisks around the screen}
var row, col : byte;
begin
for col := 1 to 79 do
write('*');
for row := 1 to 22 do
begin
write('*');
for col := 1 to 20 do
write(' ');
writeln('*')
end
end; {DrawBorder}
3. Type months = (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
Var month : months;
Procedure WriteMonth;
{outputs the long monthname of a supplied month}
begin
case month of
jan : writeln('January');
feb : writeln('February');
mar : writeln('March');
: {I am too lazy to type all of these}
nov : writeln('November')
else writeln('December')
end; {Case month}
end; {writeMonth}
Begin
month := months(random12);
writeMonth
End.
4. Function Die:byte;
{returns a random die event}
begin
Die := random(6)+1
end; {Die}
Begin
writeln(Die)
End.
5. Function Yes:boolean;
{returns TRUE is Y/y entered, FALSE if N/n}
var reply : char;
begin
repeat
reply := readkey;
writeln(reply)
until upcase(reply) in ['Y','N'];
yes := upcase(reply) = 'Y'
end; {Yes}
Begin
if Yes
then writeln('the user wants to continue')
else writeln('the user does NOT want to continue')
End.
6. ...using the above sub-programs (ie. I am too lazy to retype them)
Begin
repeat
Die;
writeln('Another Roll? :')
until not(yes)
End.
7. Type yearLevels = 5..12;
Function EnteredYearLevel:yearLevels;
{accepts user input until they enter a valid year level, then returns with that}
{still unstable if the user types characters}
var reply : integer;
begin
repeat
write('Enter a year level please :');
readln(reply)
until (reply>=5) and (reply<=12)
EnteredYearLevel := reply
end; {EnteredYearLevel}
8. Function Average:real;
{accepts the number of elements, then the elements and then calculates the
average of the elements and returns it}
var num, {the number of elements}
total, {a cumulative total}
loop,
element : integer; {an element}
begin
total := 0;
write('How many elements ?:');
readln(num);
for loop := 1 to num do
begin
write('Please type in integer number ',num', :');
readln(element);
total := total + element
end;
Average := total/num
end; {Average}
|