Vous êtes sur la page 1sur 8

Engineering Programming 100, Semester 1, 2008

Question 1 [20 marks] Fundamentals

1(a) The following C program contains three (3) syntax errors that will cause the
compiler to generate error messages. Identify the three errors [2 marks each, 6 marks in
total].

#include <stdio.h>
#include <math.h>

int main(void)
should be a brace here */
double val, result; should be value or value changed to val
const double factor = 1.0/3.0;

printf("Program determines the cube root of the inputted value\n");

printf("Input value to find the cube root of: ");


scanf("%lf", &value);
result = pow(value, factor);
print("result is %lf\n", result); should be printf

return 0;
}

1(b) The following C program contains three (3) errors that will not cause errors on
compilation but will not produce the right answer. Identify the three errors [3 marks
each, 9 marks in total].

#include <stdio.h>

int main(void)
{
int i;
double d[3];
double volume;

printf("This program computes the volume of a cube\n");


printf("Input dimensions of the cube: ");
scanf("%lf %lf %lf", &d[0], &d[1], &d[1]); should be &d[2

volume = d[0];
for(i = 1; i < 3; i--) should be i++
volume = volume * d[i];

printf("Volume of the cube is: %d\n", volume); should be %lf

return 0;
}

1(c) State whether each of the following variable statements is legal or illegal in C [0.5
marks each, 5 marks in total].

(a) int _next; right (f) char x1, x2; right


(b) float int; wrong (g) float radius_1, radius_2;
right
(c) float diam; right (h) char my name[30]; wrong
(d) double #bins; wrong (i) int My_Age_In_Years = 50;
right
(e) char y[12.0]; wrong (j) float matrix[3,3]; wrong

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

Question 2 [20 marks] Control Flow, Looping, Arrays

2(a) Write the C code segment to use a case statement to output text as defined in the
following table given the input values [5 marks].

Input value Output text


10 Excellent
9 Excellent
8 Very Good
7 Very Good
6 Good
5 Fair
4 Poor
3 Very Poor
2 Very Poor
1 Bad
0 Bad
#include <stdio.h>

int main(void)
{
int score;

printf("Input score (0-10): ");


scanf("%d", &score);

switch (score){
case 10: case 9:
printf("Excellent\n");
break;
case 8: case 7:
printf("Very Good\n");
break;
case 6:
printf("Good\n");
break;
case 5:
printf("Fair\n");
break;
case 4:
printf("Poor\n");
break;
case 3: case 2:
printf("Very Poor\n");
break;
case 1: case 0:
printf("Bad\n");
break;
}
}

Page 3 of 5
Engineering Programming 100, Semester 1, 2008

2(b) State the C code segment required to declare a two dimensional array called
results of double precision floating point values of dimensions 50 rows by 25
columns [3 marks].

double results[50][25];

2(c) Write the code segment to prompt the user for a floating point value that must be
between 0.0 and 100.0. Check the inputted value and keep prompting the user to re-
enter the value if it is not in the range 0.0 to 100.0 [6 marks].

/* q2 part c - the best solution */

#include <stdio.h>

int main(void)
{
float score;

do
{
printf("Input score between 0.0 and 100.0: ");
scanf("%f", &score);
if((score < 0.0) || (score > 100))
printf("error - score must be between 0.0 and 100.0\n");
}while((score < 0.0) || (score > 100.0));

printf("Correct score is %f\n",score);

/* q2 part c - alternative solution */

#include <stdio.h>

int main(void)
{
float score;

score = -1.0; /* initialise to a wrong value */

while((score < 0.0) || (score > 100.0))


{
printf("Input score between 0.0 and 100.0: ");
scanf("%f", &score);
if((score < 0.0) || (score > 100.0))
printf("error - score must be between 0.0 and 100.0\n");
}

printf("Correct score is %f\n",score);

2(d) Given the following structure, write the C code segment to create a variable of the
type defined by the structure and then set all the entries to 5.0. Make use of all the
variables in the structure definition to perform this task [6 marks]

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

struct matrix
{
int width = 10;
int height = 10;
float matrix[10][10];
}

/* q2 part d */

#include <stdio.h>

struct matrix
{
int width = 10;
int height = 10;
float matrix[10][10];
};

int main(void)
{
struct matrix m1;
int i,j;

for(j=0; j<m1.height;j++)
for(i=0;i<m1.width;i++)
m1.matrix[j][i] = 5.0;
}

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

Question 3 [20 marks] Functions, multiple files and I/O

3(a) Write the C code for the declaration and description of a function called biggest
to take in two floating point variables x and y and return the largest of the values i.e.
given the code z=biggest(x,y) and x=5.0, y= 5.5, z would be set to 5.5. [2 marks
for the declaration, 4 marks for the description, total of 6 marks]
#include <stdio.h>

float biggest (float, float);

float biggest_2 (float, float);

int main(void)
{
float x, y, z;

x = 5.0;
y = 10.0;

z = biggest (x,y);
printf("biggest(1) is %f\n",z);

z = biggest_2 (x,y);
printf("biggest(2) is %f\n",z);

return 0;
}

/* best solution */
float biggest (float x, float y)
{
float temp;
if (x >= y)
temp = x;
else
temp = y;

return temp;
}

/* a poorer solution - lose a mark for this */


float biggest_2 (float x, float y)
{
if (x >= y)
return x;
else
return y;
}

3(b) Write a C program to read data from a file consisting of the x and y values for a
graph in which x is an integer and y is a floating point value. A typical file looks like:

10
1 10.0
2 15.1
3 20.4
5 50.2
10 25.9

Page 6 of 5
Engineering Programming 100, Semester 1, 2008

12 26.9
15 27.0
20 37.2
25 26.5
30 27.1

The first row of the file contains the number of pairs of (x,y) points i.e. number of
lines to read (in this case 10). Assume the maximum number of points that could be in
the file is 100 and create appropriate arrays in which to hold the values. Use appropriate
error checking to deal with the file not existing[14 marks].

#include <stdio.h>

int main(void)
{
FILE *fp_in;
int i, no_samples;

int x[100];
float y[100];

fp_in = fopen("q3_b.txt","r");
if(fp_in != NULL)
{
fscanf(fp_in,"%d", &no_samples);
for(i= 0; i< no_samples; i++)
fscanf(fp_in, "%d %f", &x[i], &y[i]);
fclose(fp_in);

/* check to see if read values okay */


for(i=0; i<no_samples; i++)
printf("x: %d y: %f\n",x[i], y[i]);
}
else
{
printf("file %s not found - aborting\n");
}

return 0;
}

Page 7 of 5
Engineering Programming 100, Semester 1, 2008

Question 4 [20 marks] Program development

4(a) Given the following description of a problem, identify the stages needed and
generate the C code for the program.

As an engineer, you have been asked to determine the mass in kilograms of a number of
steel disks that your company is going to manufacture. The steel disks will be t=1.5cms
in thickness and the radius of the disks varies from rmin=10 cms to rmax=100 cms in i=2
cm increments. The parameters i, rmin, rmax and t need to be inputted by the user. The
density of the steel d=8030 kgs/m3. The format of the output should be as follows. Each
row of the output must contain the details of a disk in the following format:
r, t, density, mass
including the commas (the intention is for the data to be read into a spreadsheet
program. The first line must contain the results for the smallest radius and the last line
contain the results for the largest radius. The output of the program is to be saved in a
file “disks.csv”. Use appropriate error checking to make sure you can write to the file.
[4 marks for the program stages, 4 marks for appropriate variables and constants, 6
marks for the calculations, 2 marks for variable inputting and 4 marks for correct
outputting to file, 20 marks in total].
/* question 4 */

/*
Program stages:
1/ Declare the variables
2/ Prompt and get from the user the values of the variables
needed
3/ Open the file "data.txt" for output
3/ Repeat for the range of radius values
3.1/ Perform the calculation
3.2/ Output the results to a file
4/ Close the file
*/

/*
note the need to consider some form of conversion to get the right
units – cms to metres. Can occur in numerous places. One solution
shown below.
*/

#include <stdio.h>
#include <math.h>

#define PI 3.14159265359 /* could be a const declaration, also


could use M_PI from math.h */

int main(void)
{
FILE *fp_out;

float thickness; /* could be doubles */


float min_radius;
float max_radius;
float inc_radius;
float mass;
float const density = 8030.0; /* this could be a #define */

Page 8 of 5
Engineering Programming 100, Semester 1, 2008

float radius;

/* read in parameters */

printf("input minumum radius: ");


scanf("%f", &min_radius);
printf("input maximum radius: ");
scanf("%f", &max_radius);
printf("input radius increments: ");
scanf("%f", &inc_radius);
printf("input thickness: ");
scanf("%f", &thickness);

fp_out = fopen("disks.csv","w");
if(fp_out != NULL)
{
/* file opened okay - do calculations */
/* use of a for loop */
/* ================= */
for(radius = min_radius; radius <= max_radius; radius +=
inc_radius)
{
mass = thickness/100.0 * PI * pow(radius/1000,2.0) * density;
fprintf(fp_out,"%f, %f, %f, %f\n", radius, thickness,
density, mass);
}
/* use of a while loop */
/* ==================== */
radius = min_radius;
while (radius <= max_radius)
{
mass = thickness/100.0 * PI * pow(radius/100.0,2.0) *
density;
fprintf(fp_out,"%f, %f, %f, %f\n", radius, thickness,
density, mass);
radius += inc_radius;
}
}
else
{
printf("can't open the file to write to\n");
}

return 0;
}

END OF EXAMINATION

Page 9 of 5

Vous aimerez peut-être aussi