Beruflich Dokumente
Kultur Dokumente
09:30 11:30
SCHOOL OF ENGINEERING
OVER
Page 1 of 7
Q1
(a)
(b)
Write variable declarations for the following types. For example if the
question says array of three double precision floats write double x[3] ;
i)
an integer.
[1]
ii)
[2]
iii)
[3]
iv)
[2]
v)
ANS: (a)
Q2
i)
a&&6
[2]
ii)
x*b
[2]
iii)
a*(b+a)%10
[2]
iv)
a += ++a
[4]
i)
*fx();
[3] iv)
int
(b)
i) 1/true (true and true = true) [2] ii) 7.5 [2] iii) 8 = 28%10 [2] iv) 10 (a is
pre-incremented to become 5, then a=5+5, partial marks for spotting the
preincrement or the meaning of += without getting the whole thing) [4]
(a)
A programmer has written the following code segments which are not
performing as expected. Explain and correct the reported problem and any
other errors that you can identify in the code segments.
i)
Problem
Code
No compilation errors,
but when examined in
the debugger, both
variables contain
int i;
char s[20];
printf("Enter search word and
starting line: ");
scanf("%s %d, i, s);
[3]
OVER
Page 2 of 7
ii)
iii)
(b)
nonsense.
No compilation errors,
but no output produced.
Program appears stuck
at start of while loop.
Compilation error,
parse error before ;
token, line 7.
int i=0;
while (i < 10); {
printf(%d\t, i);
--i;
}
#include <stdio.h>
#define PI 2.1415926535;
int main() {
float area;
int r = 5;
area = r * r * PI
printf(Pi=%f, PI);
printf(Area=%f, area);
}
[3]
[4]
For each of the code fragments shown below, state what is printed on the
console when they execute. Assume all variables are integers.
i)
x = y = 3;
while (x--)
printf("%d ", ++y);
[3]
ii)
x = y = 3;
while (--x)
printf("%d ", y++);
[3]
iii)
[4]
ANS: (a)
Q3
i)
ii)
there is a semi-colon after the while condition, so that the while loop
thinks that it is acting on a null statement and never sees i change.
Remove the semi-colon, and i should be incremented, not
decremented.
[3]
iii)
marks for spotting that semi-colons are the problem, and that a semicolon in line 6 is important. Extra marks for spotting that the #define
adds a semi-colon and therefore the error is not in line 6 but in line 7
where an additional semi-colon causes problems.
[4]
(b)
(a)
For the source code of figure Q3, describe in detail how data is passed to the
functions f1 and f2 , and returned again to the main program
[6]
#include <stdio.h>
OVER
Page 3 of 7
typedef struct {
int a; double x;
} TestStructure;
TestStructure f1() {
TestStructure s;
s.a = 10;
s.x = 5.0;
return s; }
void f2(TestStructure *s) {
s->a = 6;
s->x = 7.5; }
int main () {
TestStructure s1, s2;
s1 = f1();
f2(&s2);
printf("Test 1: %d, %g\n", s1.a, s1.x);
printf("Test 2: %d, %g\n", s2.a, s2.x);
return 0;
}
Figure Q3
(b)
ANS: (a)
iPtr ,
i)
[4]
ii)
clearing of bit
[5]
iii)
inverting of bit
[5]
(b)
i)
ii)
iii)
[4]
[5]
[5]
OVER
Page 4 of 7
if (bit<0 || bit>(sizeof(int)/8))
return 0;
mask = 1 << bit;
*arg ^= mask;
return 1;
}
Q4
Describe in your own words the purpose of the code of Figure Q4, and explain
in detail how the code fulfills this purpose.
[20]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i, answer, guess, guesshist[3] = {99,99,99};
int best = 99;
srand( time(NULL) );
do {
answer = 1+(int)(100.0*rand()/(RAND_MAX+1.0));
for (i=1; i<=2; i++)
guesshist[i-1]=guesshist[i];
guesshist[2] = 0;
do {
guesshist[2] += scanf("%d", &guess);
if (guess > answer) puts("Too high.\n");
if (guess < answer) puts("Too low.\n");
} while (guess != answer);
printf("took %i guesses.\n", guesshist[2]);
if (best > guesshist[2]) best = guesshist[2];
} while (!(guesshist[0]<=guesshist[2] &&
guesshist[1]<=guesshist[2]));
printf("No improvement past %i.\n\n\n", best);
return 0;
}
Figure Q4
ANS:
The program implements the traditional high/low game with the used trying
to work out the computer has guessed. The user types in guesses and the
computer responds with too high or too low if the guess is incorrect. If the
guess is correct, the number of guesses taken to come to the correct answer is
printed. The computer keeps a record of the best (smallest) number of guesses,
and the last three numbers of guesses. If the user is not, on the whole,
improving, the program stops after printing their best (smallest) number of
guesses so far.
[6]
Two marks for each correct, non trivial, detailing of the code, up to a
maximum of 14.
[14]
OVER
Page 5 of 7
i)
i
(loop variable), answer (number to be guessed), guess (present user
guess) are defined as integers.
ii)
b
est (best number of guesses) and guesshist (last three numbers of
guesses) initialized to 99 so user is not brought to the end of a run of
games prematurely.
iii)
P
seudo-random number generator initialized using the time function to
avoid the program always using the same random numbers.
iv)
a
nswer (number to be guessed) set to an integer between 1 and 100
inclusive.
v)
C
ore body consists of a do loop which repeats until the user gets a
correct answer, surrounded by another do loop which maintains the
history of the number of guesses and repeats until the present game is
worse than both of the previous games.
vi)
A
decimal number is input for each user guess, and if the number if read
successfully the number of guesses is incremented.
vii)
T
he guess is compared with the answer and the appropriate string
printed on the console.
viii)
I
nner loop terminates when guess=answer
ix)
O
uter loop terminates when number of guesses in this game is worse
than both of the previous games.
x)
A
for loop and statement in the outer loop moves the number of guesses
for the previous two games back in a three element array leaving the
third [2] element free to hold the number of guesses for this game.
Q5
Suppose that you run a car dealership and want to print out a list of addresses
of your customers whose cars were registered in 2005. Such cars have a 5 as
the 4th character of their registration. You are provided with an array of
structures describing the customers accounts. There are numCustomers
entries in the array. The fields in the structures are the customer ID (an
integer), the customers name (a string), the registration of the vehicle (a
string), and an array of four strings that represent the customers address.
(a)
[5]
(b)
Write a function which, when passed an array of structures, prints out the
names and addresses of those with cars registered in 2005. The function
returns the number of records it has printed.
[15]
OVER
Page 6 of 7
char *reg;
[5]
[15]
END
Page 7 of 7