You are on page 1of 9

The University of British Columbia

APSC 160: Introduction to Computation in Engineering Design


Midterm Examination, February 27, 2006. Examiner: E.M. Knorr
Time: 75 minutes

Last Name

Total marks: 60

First Name:

Student ID #

(PRINT)

Signature

Lab Section:

The examination has 9 pages, and that includes


this cover sheet. Check that you have a complete paper.
Fill in your name, ID, signature, and lab section above.
To save time, you dont have to write your name on each
page since the pages have serial numbers on them.
This is a closed book examination. No books, help
sheets, calculators, cell phones, or other materials may
be used.
Work quickly and do the easy questions first. Part marks
will be awarded for most questions.
The marks for each question are given in braces. Use this to
manage your time.
Please write down any reasonable assumptions
that you are making, if you believe that a question is
ambiguous. To minimize disruptions during the exam,
please avoid asking the invigilators for hints or for more
information. Please ask only if you truly believe that a
question is in error.

Marks
Page

Max.

10

6-8

20

Total

60

Achieved

{10 marks} Multiple choice questions. Circle the best answer for each question.
1. The decimal number 56 is converted to which of the following numbers in binary?
a) 101110
b) 111000
c) 110100
d) 110110
e) none of the above
2. How many times is the following loop executed?
for (k=2; k >= -10; k--)
{
/* statements */
}

a)
b)
c)
d)
e)

0
2
12
13
an infinite number of times (because its an infinite loop)

3. Which of the following if statements has its boolean expression evaluate to true only if the mass of
an object is between 4.0 and 16.0 grams, inclusive? (mass is a double-precision variable)
a) if (4.0 <= mass && 16.0 <= mass)
b) if (mass >= 4.0 || mass <= 16.0)
c) if ( (mass == 4.0 || mass > 4.0) && (mass == 16.0 || mass > 16.0) )
d) if (4.0 <= mass <= 16.0)
e) none of the above
4. Which of the following is not an advantage of using user-defined functions to construct a small
software application? Choose the best answer.
a) the program is usually shorter
b) the program is usually more readable
c) the program usually executes faster
d) the program is usually easier to test
e) the program is usually easier to debug
5. What is the purpose of the break statement?
a) it lets the CPU take a break, by pausing until the user presses any key to continue
b) it causes the program to end immediately
c) it exits from the current loop and executes the next statement after the loop
d) it returns from the function that the user is currently in
e) all of the above are true, depending on where the break statement is located

Page 2

6. {0 marks} The womens Canadian Olympic hockey team did really well this year, winning a gold
medal. The mens hockey team, however, didnt finish very well, and got no medal. What was your
opinion of the performance of the mens Canadian Olympic hockey team?
a)
b)
c)
d)
e)

Im disappointed.
Im downright angry.
Im not at all surprised because _________________________________________________.
Im OK with it.
I dont have an opinion (or I dont care, or I dont follow hockey).

7. {4 marks} Translate the following mathematical expression into C. It finds the speed of a satellite if
that satellite is to remain in an orbit with a fixed radius (around the Earth). Assume that the variable
called speed has already been declared as a double-precision floating point number. We want to
compute:
v = [ G ME / r ] 0.5
Note that an exponent of 0.5 means square root. You can use the sqrt() function in the math.h
library to compute it. G stands for the universal gravitational constant, ME is the mass of the earth, and r
is the distance from the centre of the earth to the satellite. Assume that the following variables and
constants have been already been defined and initialized for you: UNIV_GRAV, MASS_EARTH, radius
(and also the math.h library). Just code the formula that computes speed, which is v in the formula.

8. {3 marks} Write a function prototype (a function header line showing the parameters)but do not
write the function)to return the density of a substance, given its mass and volume.

Page 3

9. {7 marks} The following code doesnt seem to work. Fix it for the programmer, so that the program
compiles and runs properly. You can indicate the errors with arrows, or by circling them ... but also be
sure to show the correction(s) you make. If anything is missing from the program (other than
comments), write it down. (Note: The number of marks for this question does not necessarily equal the
number of mistakes. There is no need to show the output.)
#include <stdio.h>
#define FILENAME sensor3.txt

int main(void)
{
int

num_data_pts = 1;

double

seconds, sum=0.0;

FILE

*sensor;

/* open file for reading */


sensor = fopen(FILENAME, r);
if (sensor == NULL)
printf(Error opening input file.\n);
else
{
/* while not at end of file, sum the time in seconds /
while ( (fscanf(%lf, seconds)) == 1 )
{
num_data_points++;
sum += seconds;
}
if (num_data_points = 0)

/* avoid division by zero */

printf(No data points were read from the file.\n);


else
average = num_data_points / sum;
printf(Average of the times is %7.2f seconds\n, average);
fclose(sensor);
} /* end of first ELSE statement */
system(PAUSE);
return 0;
}

Page 4

10. {8 marks} For the following fragment of code, do two things: (1) Create a trace table to show how
the variables change throughout the code, and (2) show the output of this code. A box is provided for
the output, but create your own trace table to the right of the code.
int

a, b, c;

a = 10;
b = 5;
for (c=0; c <= a; c++)
{
if (c < b)
printf(c is %i\n, c);
else
c++;
if (c < b &&
{
a = a - 2;
b--;
}

c < a)

}
printf(a=%i, b=%i, c=%i\n, a, b, c);
printf(c / a = %i, c/a);
calc(c);
/* the function calc appears here */
void calc(int a)
{
if (a % 5 != 0)
printf(not a multiple of 5\n);
else
printf(a multiple of 5\n);
}

Output from the program:

Reminder: Dont forget to create your own trace table to the right of the code!

Page 5

11. {20 marks} For this part of the question you need to do two things: (a) write a short main
program (with appropriate prompts, and appropriate output), and (b) write a short function (to
perform a calculation, given two input parameters). To save time on this question, you dont have to
include comments.
Here is some background about the application:
Canadas oil sands are the worlds largest known hydrocarbon resource. Bitumen is a thick, sticky form
of crude oil found in Albertas Athabasca oil sands, and it flows very poorly. It can be mined at the
surface (using diggers and dump trucksand later the oil is separated from the sand). Alternatively,
a process called SAGD (steam-assisted gravity drainage) can be used to inject steam underground,
and then the resulting heavy oil can be more easily pumped to the surface. But, SAGD needs to use
energy to do the heatingand that energy typically comes from natural gas.
Furthermore, the resulting heavy oil may have a hydrogen/carbon ratio of 1.5 to 1, but most refineries
need crude oil with a ratio of 1.7 or higher. Such heavy oil needs to be upgraded by either removing
carbon atoms, or by adding hydrogen atoms. The latter is preferred; this process is called hydrotreating,
and again, natural gas is used in this process.
An estimate for the amount of gas needed for both SAGD and upgrading (combined) is 2200 cubic feet
of natural gas per barrel of oil. (For your information, a barrel is just a unit of measureits 42 US
gallons.) Using natural gas was not a bad idea when the cost of natural gas was only $1.50 (per 1000
cubic feet); but recently, natural gas hit $15.00, and is now around $7.00.
You job is to write a main program that reads (from the keyboard) the number of barrels of SAGD heavy
oil that needs to be extracted and upgraded, and the current price (for 1000 cubic feet) of natural gas.
Define (as a constant) the amount of money that the company budgets for 1000 cubic feet of natural
gas this yearwe will use 6.00 dollars as our budget estimate for this year. (Of course, this value
may change from year to year, but since its a constant in our program, it can easily be changed.)
Your function needs to return the total amount of money above or below budget for extracting and
upgrading the given number of SAGD-produced barrels of heavy oil, given the current price of
natural gas. Then, after being returned by the function, the amount is printed by your main program,
using the wording shown below.
Example 1: Suppose the company assumes a yearly budget of $6.00 for (1000 cubit feet of) natural gas.
For a given project, suppose the user inputs 10000.0 barrels of SAGD-produced heavy oil, and a
current price of $7.00 for natural gas. For this amount of heavy oil, the company would be over
budget by this dollar amount: 10000.0 * 2200/1000 * (7.00-6.00) = 22,000.00because it costs the
company more for the gas than expected. So, your function should return 22000.0.
Example 2: If the current price of natural gas is only $5.50, then for an input of 10000.0 barrels, the
function would return 11000.0, which comes from: 10000.0 * 2200/1000 * (5.50-6.00). By
returning 11000.0, you are indicating that the cost of extracting and upgrading this amount of heavy
oil is $11000 under budget.
Output: For Example 1 above, your main program would print the following sentence on the screen:

Page 6

This project will be 22000.00 dollars over budget.

And, for Example 2 above, your program would print:


This project will be 11000.00 dollars under budget.

Note the words above and under. Your code should choose the correct word! Also note that there
is no negative sign in the output, and that the amounts are printed to 2 decimal places.

Write your complete main program here. Then, write your function. There is more space on the next
page.

<more space is on the next page>


Page 7

Page 8

12. {8 marks} Write a fragment of C code to do the following using a while loop. Print out the sum of
the positive integers between 1 and limit, inclusive, for all positive integers in that range that are not
evenly divisible by 2 or 3. For example, if limit = 17, then we compute: 1 + 5 + 7 + 11 + 13 + 17 = 64.
Be sure to declare and initialize any variables that you use. (You dont have to read in the limit from the
keyboard, and you dont have to write the whole program.) Your printed output for this case would be:
The sum is 64.

Page 9

You might also like