Sie sind auf Seite 1von 7

Tuesday 15th May 2012

09:30 11:30

SCHOOL OF ENGINEERING

DEGREE OF MASTER OF ENGINEERING (M.Eng.)


DEGREE OF BACHELOR OF ENGINEERING (B.Eng.)
DEGREE OF BACHELOR OF SCIENCE (B.Sc.) IN ENGINEERING

INTRODUCTORY PROGRAMMING EE1 (ENG1032)


Attempt ALL questions

You must indicate the questions you have answered


in the boxes provided on the front cover of your exam script.
You should not attempt more questions than are asked for.
If you do attempt more questions than required you must indicate
clearly on the front of your exam script those questions
you wish to be considered for marking.
Each question is worth 20 marks
Candidates should note that electronic
calculators with a facility for either textual
storage or graphical display are excluded from use
in examinations. If an electronic calculator is
used, intermediate steps in calculations should be indicated

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)

an array of 50 single precision floating point numbers.

[2]

iii)

a function with no arguments returning a pointer to an integer.

[3]

iv)

an array of ten pointers to integers.

[2]

v)

a three dimensional 555 array of double precision floating point


numbers.
[2]

Assuming the following declarations,


int a=4, b=3;
double x = 2.5;

write down the results of the following expressions.

ANS: (a)

Q2

i)

a&&6

[2]

ii)

x*b

[2]

iii)

a*(b+a)%10

[2]

iv)

a += ++a

[4]

i)

int x; [1] ii) float x[50]; [2] iii) int


*pi[10]; [2] v) double array[5][5][5] [2]

*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)

for (i=0; i<10; i++)


printf("%d ", i & 3);

[4]

ANS: (a)

Q3

i)

the scanf function is expecting to receive pointers, so that an


ampersand should be in front of i and s, and the i and s are the wrong
way round in the printf statement
[3]

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)

i) 4 5 6 [3] ii) 4 5 [3] iii) 0 1 2 3 0 1 2 3 0 1 [4]

(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)

Write functions which accept two arguments, a pointer to an integer


and an integer b , and perform the following operations;

iPtr ,

i)

setting bit b of the integer pointed to by iPtr .

[4]

ii)

clearing of bit

[5]

iii)

inverting of bit

of the integer pointed to by iPtr .

of the integer pointed to by iPtr .

[5]

discussion of pass by value and pass by reference, f1 copies the whole


structure from the local function scope back using the return. f2 called by
reference.
[6]

(b)
i)

ii)

iii)

int bit_set(int *arg, int bit) {


if (bit<0 || bit>(sizeof(int)/8))
return 0;
*arg |= (1 << bit);
return 1;
}
int bit_clear(int *arg, int bit) {
int mask;
if (bit<0 || bit>(sizeof(int)/8))
return 0;
mask = ~(1 << bit)
*arg &= mask;
return 1;
}
int bit_invert(int *arg, int bit) {
int mask;

[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)

Write down a type declaration of the structure of a customers account.

[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

ANS: One possible answer is given below.


struct customerRecord {
int id; char *name;
char *address[4];
};

char *reg;
[5]

int printCustomers(struct customerRecord* cra) {


int I, j, count = 0;
for (i=0; i<numCustomers; i++)
if ( strlen(cra[i].reg)>=4 && cra[i].reg[3] == 5) {
count++;
puts(cra[i].name);
for (j=0; j<=3; j++)
puts(cra[i].address[j]);
}
}
return count;
}

[15]

END
Page 7 of 7

Das könnte Ihnen auch gefallen