Name : Perfect Student Mark : 32 /25
1. Are the following variable names valid or invalid?
Valid or Invalid?
a. C3P0 Valid
b. 145R Invalid
c. R*2 Invalid
d. TimeAndDate Valid
(4 marks)
2. Point out the errors in the following program (by circling each error and attaching a brief explanation to each circle) :
program Age(input,output); const Young = 14; procedure GetAge(var TheAge:real); #Does not agree with type in calls begin write('Please enter your age :'); readln(TheAge); end; procedure Age(var TheAge:integer); #Procedure name same as program name begin if TheAge <= Young then writeln('A youngster'); #No ; before else else writeln('A senior'); end; var UserInput:integer; #Should be before procedures begin {----Main} Age(17); #Passing constant to var parameter GetAge(UserInput); Age(UserInput); end; #Should be a .(6 marks)
3. Convert the if-then-else statement in the following code into an equivalent case statement. You can assume that the user will enter an appropriate character.
var MomOrDad:char; begin readln(MomOrDad); if (MomOrDad = 'M') or (MomOrDad = 'm') then writeln('Hi mom') else if (MomOrDad = 'D') or (MomOrDad = 'd') then writeln('Hi dad'); case MomOrDad of 'M','m':writeln('Hi mom'); 'D','d':writeln('Hi dad'); end;(5 marks)
4. The formula to compute the number of different ways R items can be selected from a group of N items is N!/(R!*(N-R)!). This is called the number of combinations. Complete the function Combinations in the program below, to implement this formula.
program Gamble(input,output); {----The core of a gambling program } var Items,Select:integer; function Factorial(N:integer):integer; {----Computer the factorial of a number } begin if N <= 1 then Factorial:=1 else Factorial:=N*Factorial(N-1); end; function Combinations(Items,ToSelect:integer):integer; begin Combinations :=Factorial(Items) div (Factorial(ToSelect) * Factorial(Items - ToSelect)); end; begin {----Main} write('Enter number of items and number to select '); readln(Items,Select); writeln('There are ',Combinations(Items,Select),' ways to select ', Select:1,' items from ',Items:1); end.(5 marks)
5. The real variable Price holds the value 154.56 (to represent a value of $154.56). Give a Pascal statement that will write out Price in a suitable format.
writeln('$',Price:6:2)(3 marks)
6. What is the main
a) advantage Packed arrays are more space efficient
b) disadvantage Packed arrays are slower to access
of using a packed array (as opposed to a non-packed array)?
(2 marks)
7. Given the declarations
type DataType = record Size:real; Counted:integer; end; DataFileType = file of DataType;
write a procedure that reads a file of DataFileType, and writes all the Size and Counted fields' values to the screen, one pair per output line. The external name of the file is info.dat.
procedure ReadFile; var InputFile:DataFileType; InputData:DataType; begin reset(InputFile,'info.dat'); while not(eof(InputFile)) do begin read(InputFile,InputData); writeln(InputData.Size,' ',InputData.Counted); end; close(InputFile); end;(7 marks)