Computer Science 2015
Computer Science 2015
EXAMINATIONS OF
May
2015
Duration: 3 hours
............................................
Second Examiner
Date: 2015/04/....
____________________
|
|
|
|
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Call a function (sub-program) with at least one loop in it to print the layout of the grid.
[3 marks]
b. Then print the following message:
Welcome to the guessing game. You have 25 tries to guess the number that I am
thinking of between 1 and 30. Each time you make an incorrect guess, we will
show it to you in the GRID OF GUESSES. You may keep guessing until you are
successful or until you have filled the grid.
The algorithm must then generate the number between 1 and 30 and not reveal it to the
user, ask the user to guess the number and then read the guess.
[2 marks]
c. The algorithm must check the guess against the generated number and if they are the
same it must print a message to the screen saying Congrats, you have won!, and then
stop the game.
[2 marks]
d. If the guess is not correct, the algorithm must perform two tasks:
Print to the screen the incorrect guess in the grid in the next available spot.
Check to see if the guess is greater or less than the random number and indicate this
to the user.
[2 marks]
For example, if the random number is 17 and the player guesses 10, the following would be
printed to the screen:
THE GRID OF GUESSES
10
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
Your number is too small. Try again
If the player then guesses 20, the following would be printed to the screen:
THE GRID OF GUESSES
10
|20
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
Page 2 PLEASE TURN OVER
............................................
Second Examiner
Date: 2015/04/....
|
|
|
|
|
____________________
|
|
|
|
|
____________________
Your number is too big. Try again
e. Your algorithm must also keep score. Use a counter that starts at 25 and for every
incorrect guess, decrement it. The more guesses the less points. The user must be
restricted to 25 guesses.
[2 marks]
Print your final score to the screen and to the file: myScores.txt.
[2 marks]
You do not have to use an array to hold the contents of the grid.
YOUR SOLUTION SHOULD INCLUDE DOCUMENTED PSEUDOCODE ONLY.
NO C-CODE IS REQUIRED.
SCHEME:
BEGIN ALGORITHM (MAIN PROGRAM) [ MARK]
DECLARE [ MARK]
integer iSecret
integer iGuess,
integer array pos
integer guesses
integer points
File pointer fp to point to counter.txt
Initialize [[ MARK]
Initialize pos to \s // can work because of ascii
Initialise guesses to 25
/*
a. In this game you have to start by printing a grid with a title as follows:
THE GRID OF GUESSES
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Call a function (sub-program) with at least one loop in it to print the layout of the grid.
*/
............................................
Second Examiner
Date: 2015/04/....
print
Welcome to the guessing game. You have 25 tries to guess the number that I am
thinking of between 1 and 30. Each time you make an incorrect guess, we will show it to
you in the GRID OF GUESSES. You may keep guessing until you are successful or until
you have filled the grid.
// [1 mark]
/*
The algorithm must then generate the number between 1 and 30 and not reveal it to the
user, ask the user to guess the number and then read the guess.
*/
iSecret Generate a random number between 1 and 20 // [1/2 mark]
print "Guess the number (1 to 30) // [ mark for request and read]
read iGuess
/*the algorithm must check the guess against the generated number and if they are the
same it must print a message to the screen saying Congrats, you have won!, and then
stop the game.
*/
[1/2 mark]
[1/2 mark]
DO WHILE (iSecret NOT EQUAL iGuess) AND (guesses >= 0)
if iSecret EQUAL iGuess then // [ 1/2 mark]
print Congrats, you have won // [1/2 mark]
exit program // [1/2 mark]
endif
/*
10
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
Your number is too small. Try again
*/
if iSecret < iGuess || iSecret > iGuess then [ mark]
increment guesses [ mark]
pos [guesses] iGuess [ mark]
decrement points
// keep track of guesses and points [ mark]
print
pos [0] | pos[1]| pos[2]| pos[3]| pos[4]| //etc. for all positions [ mark]
____________________
pos [5] | pos[6]| pos[7]| pos[8]| pos[9]| //etc. for all positions
Page 4 PLEASE TURN OVER
............................................
Second Examiner
Date: 2015/04/....
____________________
|
|
|
|
|
____________________
|
|
|
|
|
____________________
|
|
|
|
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
if iSecret < iGuess then [ MARK] // HANDLE THE SMALL
print Your number is too small. Try again
if iSecret > iGuess then [ MARK] //HANDLE THE LARGE
print Your number is too large. Try again
print "Guess the number (1 to 30) // [ mark for request and read]
read iGuess
End WHILE //(iSecret NOT EQUAL iGuess) AND (guesses < = 25) // while they have not
// guessed correctly [1/2 mark]
// and < 25 tries [1/2 mark]
open fp for writing [ MARK]
if fp EQUAL null [ MARK]
print error [ MARK]
exit program
print here are your points: points to fp [ MARK]
End Algorithm [1/2 MARK]
Begin Algorithm (SubProgram PrintGrid) // define function header [1/2 mark]
//use a loop correctly to do something in the grid pattern
For i = 1 to 5 DO // print the five rows [ mark]
For i = 1 to 5 DO [ MARK]
Print
| // print tab and pipe pattern five times
EndFOR
For i = 1 to 5 DO [ MARK]
Print _ _ _ _ _ // print underline pattern 5 times
EndFOR
EndFOR
End Algorithm (SubProgram PrintGrid)
ii.
The format identifier %lf is also used for _____ data type?
a. char
b. int
[1 mark]
............................................
Second Examiner
Date: 2015/04/....
c. float
d. double
[1 mark]
iii.
1. #include <stdio.h>
2.
3. void main()
4. {
5.
int num1, num2, num3;
6.
7.
printf("Enter the values of num1, num2 and num3 ");
8.
scanf("%d %d %d", &num1, &num2, &num3);
9.
printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
10.
if (num1 > num2)
11.
{
12.
if (num1 > num3)
13.
{
14.
printf("num1 is the greatest among three \n");
15.
}
16.
else
17.
{
18.
printf("num3 is the greatest among three \n");
19.
}
20.
}
21.
else if (num2 > num3)
22.
printf("num2 is the greatest among three \n");
23.
else
24.
printf("num3 is the greatest among three \n");
25.
}
Figure 1
a. num1 = 6
b. num1 = 6
num2 = 8
num3 = 10 num3 is the greatest among three
num2 = 8 num3 = 10
[1 mark]
iv.
a.
b.
c.
d.
#include <stdio.h>
int main()
{
int i = 0;
while (i == 0);
printf("True\n");
printf("False\n");
}
Infinite times
1 time and then False
Never
It depends
Page 6 PLEASE TURN OVER
............................................
Second Examiner
Date: 2015/04/....
[1 mark]
1.
2.
3.
#include <stdio.h>
4.
5.
void main()
6.
7.
8.
9.
10.
scanf("%d", &ival);
11.
remainder = ____________________; // 1
12.
if (remainder == 0)
13.
14.
else
15.
16.
Figure 2
v.
The C-code in Figure 2 above will check whether a given integer is odd or even. What
should be entered in the blanks?
a.
b.
c.
d.
vi.
The keyword used inside a loop to return to the beginning of the loop
a.Exit
c.Continue
b.Goto
d.Return
[1 mark]
vii.
............................................
Second Examiner
Date: 2015/04/....
viii.
What will be the output value if you will compile and execute the following c code?
1. #define x 5+2
2. void main(){
a. int i;
b. i=x*x*x;
c. printf("%d",i);
3. }
[1 mark]
a.
b.
c.
d.
ix.
343
27
133
Compiler
x.
c. ? :
d. %c
[1 mark]
xi.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
a. 11 12 11 12
b. 11 13 11 14
c. 11 12 11 13
d. Compile time error
[1 mark]
xii.
............................................
Second Examiner
Date: 2015/04/....
xiii.
a.
b.
c.
d.
k = = 1 is TRUE
1 = = 1 is TRUE
1 = = 1 is FALSE
K = = 1 is FALSE
[1 mark]
xiv.
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
}
a.
b.
c.
d.
xv.
The sequential model of software development which has no feedback is known as the:
a. Spiral Model
b. Waterfall Model
c. Fountain Model
d. Iterative Model
[1 mark]
............................................
Second Examiner
Date: 2015/04/....
a. Examine the program shown in Figure 3 and list all of the syntax and logical errors that are
present by stating the line number, the error [1/2 mark] and indicating the correct syntax or
logic [1/2 mark]. The program is written to swap the values of num1 and num2 into the other
variable. So at the end of the program, the original value of num1 should be in num2 and the
original value of num2 will be in num1.
Mark and correct each highlighted issue to a max of 5 marks. There are 10 opportunities for
marks.
[5 marks]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
#include <stdio.h>;
void swap(a, b);
{
temp = a;
a = b;
b = temp;
return();
}
int main();
{
num1 = 10, num2 = 20;
printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
swap(int num1, int num2);
printf("After swapping num1 = %d num2 = %d \n", num1, num2);
return();
}
Figure 3
b. Explain the scope of the variables num1 and temp.
[2 marks]
Answer:
They have local scope[1 mark]. A variable that is declared inside a function definition is local. It
is created and destroyed every time the function is executed, and it cannot be accessed by any
code outside the function. [1 mark]
c. Using the code example briefly explain term call-by-value.
[1 mark]
The values of num1 and num2 are copied and passed to a and b. The value of num1 and
num2 do not change.
b. Write the C-program for a time converter that will request and accept the time in Bridgetown
and ask the user for a number representing a second city. The options for the second country
are 1- New York, 2- London (England), 3- Toronto, 4- Hong Kong, 5- Acapulco (Mexico), 6Detroit, and 7- St. George's (Grenada). You must use a switch statement to print the time in
Barbados and the time in the given city during the summer months. The summer time
conversion is as follows:
1234-
............................................
Second Examiner
Date: 2015/04/....
5- Acapulco: -2 hours
If any other city is given as input the program should print a suitable message.
[7 marks]
#include <stdio.h>
main( )
{
int city;
int hours, minutes;
char mode[3];
}
// 1 mark for accommodating for 12 hrs or 24 hrs (if military time used)
if ((hours > 12) && (strcmp(mode, AM))
{hours = 12 hours;
strcpy(PM, mode);}
else if ((hours > 12) && (strcmp(mode, PM))
{hours = 12 hours;
strcpy(AM, mode);}
else if ((hours < 0) && (strcmp(mode, PM))
{hours = 12 + hours;
strcpy(AM, mode);}
else if ((hours > 12) && (strcmp(mode, AM))
{hours = 12 + hours;
strcpy(PM, mode);}
printf(Second city time is %d:%d %s, hours, minutes, mode);
} // end program
............................................
Second Examiner
Date: 2015/04/....
and
char string2 [] = Pastel;
then
string2 will become School
and
string1 will become Pastel
[5 marks]
void swapit(char string1[], charstring2[], int arraysize) // 1 mark
{
const int mysize = arraysize; // 1 mark declarations
char temparray [mysize];
for (int i = 0; i < arraysize; i++) // 1 mark loop to temporarily store
temparray [i] = string1[i];
for (i = 0; i < arraysize; i++) // 1 mark loop 2 to copy one to another
string1 [i] = string2[i];
for (i = 0; i < arraysize; i++) // 1 mark loop 3 copy out of temporary storage
string2 [i] = temparray[i];
return;
}
b. A certain computer stores floating point numbers using a 10-bit mantissa, a 5-bit exponent (in
excess 2 n-1 notation) and 1 bit for the sign. Show how the number 20.375 would be stored in a
16-bit word. (Assume the exponent is represented in Sign and Magnitude form). What is the
error in the representation?
0
1
sign
0 0
mantissa
[5 marks]
0 1 0 1
exponent
............................................
Second Examiner
Date: 2015/04/....
//Point A
//(i)
num_t = 0;
//[1/2 mark]
num_line = 0;
//[1/2 mark]
while (!feof(fpt1))
//[1/2 mark]
{
fscanf( fpt1, %c%, &mychar);
//[1/2 mark]
if (mychar == t)
//[1/2 mark]
num_t++;
//[1/2 mark]
//[1/2 mark]
if ((mychar == .)||(mychar == !)|| (mychar == ?))
num_line++;
//[1/2 mark]
}
//(ii)
fprintf(fptr2, %d, %d, num_t, num_line);//[1 mark]
//...
fclose(fpt1);
fclose(fpt2);
return 0;
}
Write the code that will be inserted at Point A, by declaring any additional variables that
you need and writing code to do the following:
i.
Read in the contents of mockingbird.txt character by character and count all
of the occurrences of the letter t and the number of sentences in the file.
Remember that sentences end with full-stops, question marks and exclamation
marks.
[4 marks]
ii.
Output the number of the occurrences of the letter t and the number of
sentences in the story to the file counter.txt.
[1 mark]
END OF EXAM
............................................
Second Examiner
Date: 2015/04/....