Sie sind auf Seite 1von 3

Engineering Programming 100, Semester 1, 2008

1/ Choose the best representation for the following real life variables [1 mark each, 5
marks in total].

a. Country string is best, char not so good


b. Weight in kilograms float or double
c. Age in years char, short or int (could be float if 3.5 years say)
d. Last name string is best, char not so good
e. Number of DVDs char, short or int

2/ State which of the following variable declaration statements is legal or illegal [1


mark each, 5 marks in total].

float a, b = 3; legal
double SIZE; legal
long int next2you; legal
int second-life; illegal
unsigned short x!; illegal

3/ State the result of the following operations where the variable types and their initial
values are stated below [2 marks each, 6 marks in total].

int x = 24;
int y = 20;
int j;
float p;

a. ++x; 25
b. j = x/3 + 9; 17
c. p = (x * 2 + 40) / (y * 3 – 16); 2.0
Note doesn’t matter whether the value of x from a.
is used (25) instead of 24 – same results.

4/ Write down the C statement to declare an array to contain the height in metres for
50 people [2 marks].

float height[50]; Needs to be floating point, int not so good

5/ Write down the C statement to declare an array to represent an image or 320 by 240
values where each value varies from 0 to 255 in increments of 1 [2 marks].

unsigned char image[240][320];

Penalise lack of unsigned, use of int or having the 240 and 320 swapped over.

6/ Give the statement(s) to output to the screen, a double precision variable called
speed, followed by an integer called count, followed a character called found [3
marks].

printf(“%lf %d %c\n”, speed, count, found);

Page 1 of 4
Engineering Programming 100, Semester 1, 2008

7/ Give the statement(s) to read from the keyboard, an integer called house_number,
followed by a string called street_name, followed by a long integer called
phone_number [3 marks].

scanf(“%i %s %ld”, &house_number, street_name,


&phone_number);

8/ What is printed by the following code? [4 marks].

int i;

for(i=40; i>=20; i-=5)


{
printf(“%d\n”, i + 2);
}

42
37
32
27
22 (one number per line of course).

9/ What is printed by the following code? [4 marks].

int i;
float r = 0.0;

do
{
r += 0.3;
i = r;
printf(“%d\n”, i);
}
while (r < 1.8);

0.3
0.6
0.9
1.2
1.5
1.8

10/ Explain why it is a bad idea to use the following if statement when the variable
mass is a floating point number [2 marks].

if(speed == 0.0)

Answer should mention that you can’t represent floating point numbers accurately so
shouldn’t do direct comparisons in an if clause. Should use a tolerance value e.g.
if((speed <= tolerance) && (speed >= -tolerance)).

Page 2 of 4
Engineering Programming 100, Semester 1, 2008

11/ Write an if then else statement to output the grade ‘F’ when the integer variable
mark is 0, 1, 2, 3 or 4, and output the grade ‘P’ when the value of mark is 5, 6, 7, 8,
9 or 10. [4 marks].

if((mark >= 0) && (mark <= 4))


printf(“F\n”);
else if ((mark >= 5) && (mark <= 10))
printf(“P\n”);
else
printf(“Mark is unrecognised\n”);

Other solutions are possible and reward if they are reasonable (switch statement, one
if statement per mark etc.).

12/ Write a complete C program to read in two floating point numbers from the
keyboard and print them out in descending order. That is, if the values are 12.5 and
11.7, the output would be 12.5 followed by 11.7 [10 marks]

#include <stdio.h>

int main(void) // no use of command line args – not


{ // in the unit.
float x,y;

printf(“input two floating point numbers: “)


scanf(“%f %f”, &x, &y);

if(x <= y
printf(“%f %f\n”, y, x);
else
printf(“%f %f\n”, x, y);
}

END OF TEST

Page 3 of 4