Sie sind auf Seite 1von 7

Resit 2012

UNIVERSITY OF GLASGOW

Degrees of MEng, BEng, MSc and BSc in Engineering

INTRODUCTORY PROGRAMMING (ENG4006)


Date August 2012
time
Attempt ALL questions

The numbers in square brackets in the right-hand margin indicate the marks allotted to the
part of the question against which the mark is shown. These marks are for guidance only.

An electronic calculator may be used provided that it does not have a facility for either
textual storage or display, or for graphical display.

Continued Overleaf
Page 1 of 7

Q1

(a)

Describe the types of the variables or functions declared by the following


statements. For example char f4() means f4 is a function returning a
single character.
i)
ii)
iii)
iv)
v)
vi)

(b)

int x;
float levels[10];
double *myPtr;
int *keys[5];
void f(int score[], int size, float *ave);
int *myCalc();

[1]
[1]
[1]
[2]
[3]
[2]

Assuming the following declarations,


int num = 2, denom = 4;
struct {int x; double y;} v = {6, 3.0}

write down the results of the following expressions.


i)
ii)
iii)
iv)
v)
ANS: (a)

Q2

num / denom
num >= v.y || denom != v.x
num*num+num%num-num/num
(v.x == 3*num) && ( num = denom )
num << num

[2]
[2]
[2]
[2]
[2]

i) x is an integer [1] ii) levels is an array of 10 floating point numbers [1] iii)
myPtr is a pointer to a double precision floating point number [1] iv) keys is
an array of five pointers to integers [2] v) f is a function taking an array of
integers score, an integer size and a pointer to a single precision floating
point number as arguments, returning nothing in the function [3] vi) myCalc
is a function with no inputs and returning a pointer to an integer.

(b)

i) 0 [2] ii) 1/true [2] iii) 3 [2] iv) 1/true [2] v) 8 [2]

(a)

Explain the difference between the use of x and x in the C programming


language.
[3]

(b)

The code
putchar('x'-10); printf(" %d\n",'x'-10);

is found to produce the output


n 110

Describe why this result is produced.


(c)

[5]

Write a piece of code, which makes use of for loops, to print the following,
1a
2a
3a
4a
5a
6a
7a

1b
2b
3b
4b
5b
6b
7b

1c
2c
3c
4c
5c
6c
7c

1d
2d
3d
4d
5d
6d
7d

1e
2e
3e
4e
5e
6e
7e

1f
2f
3f
4f
5f
6f
7f

1g
2g
3g
4g
5g
6g
7g

1h
2h
3h
4h
5h
6h
7h

Continued Overleaf
Page 2 of 7

8a

8b

8c

8d

8e

8f

8g

8h

[12]

ANS: (a)

answer should include the words string, character and \0, and mention the
length of the string x.
[3]

(b)

each character is one byte long, with the character x equivalent to the byte
120 in decimal. So a-10 is the character n or the byte 110 in decimal. The
putchar function outputs the byte in character format, and the printf statement
output is the equivalent in decimal.
[5]

(c)

#include <stdio.h>
int main() {
int i,j;
for (i=1;i<=8;i++) {
for (j=0;j<=7;j++) {
putchar(' '); putchar(' ');
putchar('0'+i); putchar('a'+j);
}
putchar('\n');
}
return 0; }

(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
[8]

Q3

[12]

#include <stdio.h>
typedef struct {
int a; double x;
} myStructure;
myStructure f1() {
myStructure s;
s.a = 10;
s.x = 5.0;
return s; }
void f2(myStructure *s) {
s->a = 6;
s->x = 7.5; }
int main () {
myStructure 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

Continued Overleaf
Page 3 of 7

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

raising the integer pointed to by iPtr to the power of b.

[4]

ii)

clearing of bit

[8]

of the integer pointed to by iPtr .

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.
[8]

(b)
i)

int powerFunc(int *iPtr, int b) {


*iPtr = pow(*iPtr,b);
return 1;
}

[4]

or
int powerFunc(int *iPtr, int b) {
int i, accumulate = 1;
for (i=0; i<b; i++) {
accumulate *= *iPtr
}
*iPtr = accumulate;
return 1;
}

ii)

int bitClear(int *iPtr, int b) {


int mask;
if (b<0 || b>(sizeof(int)/8))
return 0;
mask = ~(1 << b)
*iPtr &= mask;
return 1;
}

[8]

Continued Overleaf
Page 4 of 7

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>
typedef struct {
int orig, xform, len;
} Flip;
Flip transform(int a) {
Flip temp = {0,0,0};
temp.orig = a;
while (a) {
temp.xform = 10*temp.xform + (a%10);
a /= 10;
temp.len++;
}
return temp;
}
int main () {
int x, mxlen = 0; Flip y;
do {
printf("
enter: "); scanf("%d", &x);
if (x) {
y = transform(x);
if (mxlen < y.len) mxlen = y.len;
printf("%d %d %d", mcxlen, y.orig, y.xform);
}
} while (x);
}

Figure Q4
ANS:

The program inverts the order of the characters in the base 10 representation
of input numbers, printing both the original and transformed numbers and
formatting based on the longest number entered so far. The code stops when a
0 is entered.
[5]
The following are all worth two marks up to a maximum of 15
[15]
i) a structure is defined to hold three integers, orig, xform and len, and
typedef'ed to type Flip.
ii) The structure is used to return three variables from the function transform,
avoiding the limitation of a single return variable.
iii) The main program declares an integer variable x, and then repeatedly
loops, until the value of x (which is supplied by the user) becomes 0.
iv) the main loop seeks a value of x from the user, calls the tranform function
with this integer as its input and obtains data from the function in the
variable y, which is a structure with three member variables
v) the output y.len from the function is compared with the largest value of
y.len returned so far, and the largest value of y.len so far is, as a result,
stored in mxlen

Continued Overleaf
Page 5 of 7

vi) the main loop outputs the two structure variables y.orig and y.xform, and
the maximum number of decimal digits used so far
vii) the tranform function first initialises a temporary working variable of type
Flip, with three members. The original input to the function is placed
in the .orig member variable
viii) the function repeatedly loops, the number of loops equal to the number of
decimal digits in the input number to the function (the input number is
divided by 10 on each loop iteration in order to perform this
calculation).
ix) .len is incremented on each iteration in order to store the number of
decimal digits in the input variable
x) on each iteration .xform is left shifted by one decimal place (*10) and the
rightmost new digit is supplied by the rightmost old digit which is left
in a - thus transposing the order of the digits in .orig and .xform

Continued Overleaf
Page 6 of 7

Q5

A C program requires a function called inRange which takes as its


arguments: an array of floating point numbers, an integer indicating the size of
the array, and two double-precision floating point numbers, min and max . The
job of the function is to scan the array and return, in a structure, the number of
members of the array which are in range (i.e. less than or equal to max and
greater than or equal to min ) and the mean of the values which are in range.
Write,
(a)

the structure definition of the structure returned by inRange

(b)

the function inRange

[5]
[15]

ANS: One possible answer is given below.


#include <stdio.h>
typedef struct {
int withinRange;
} RangeCount;

float mean;
[5]

RangeCount inRange(float data[], int size, double min, double max) {


int i;
float total = 0;
RangeCount r = {0, 0.0};
for (i=0; i<size; i++)
if ((data[i] <= max) && (data[i] >= min))
r.withinRange++;
else
total += data[i];
r.mean = total / (float)(size-r.withinRange)
printf(number of elements in range = %d\n, r.withinRange);
printf(average of elements in range = %.2f\n, r.mean);
return r;
}

[15]

End of question paper


Page 7 of 7

Das könnte Ihnen auch gefallen