Vous êtes sur la page 1sur 10

The University of British Columbia

APSC 160: Introduction to Computation in Engineering Design


Midterm Examination, October 23, 2007. Examiner: E.M. Knorr
Time: 75 minutes

Last Name

Total marks: 60

First Name:

Student ID #

(PRINT)

Signature

Lab Section:
(if unknown, state day/time of lab)

The examination has 10 pages, and that includes


this cover sheet. Check that you have a complete paper.

Marks
Page

Max.

10

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.

8-10

13

Total

60

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.

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.

Achieved

{10 marks} Multiple choice questions. Circle the best answer for each question.
1.

The binary number 10101 is converted to which of the following numbers in decimal?
a) 20
b) 16
c) 22
d) 23
e) none of the above

2. Which of the following is the most high-level language in terms of describing a problem?
a) C
b) Assembly language
c) hexadecimal
d) machine language
e) English-like pseudo-code
3. Which of the following numbers is largest (in decimal terms)?
a) 17 in hexadecimal
b) 10000 in binary
c) 32 in decimal
d) 2C in hexadecimal
e) 100101 in binary
4. When a program runs on the CPU, which of the following storage locations is the fastest for the CPU
to access?
a) Level 2 cache
b) a register
c) main memory (also called RAM)
d) a disk drive thats 1 year old, made by IBM
e) a new disk drive that just went on sale yesterday at Staples or FutureShop
5. Which of the following does not typically happen when a return statement is executed in a function?
a) any local/temporary variables for the function are destroyed
b) the return value is passed back to the caller
c) the function returns to resume execution at the calling statement
d) the compiler checks for syntax errors
e) all of the above happen at return time

Page 2

6. {4 marks} Briefly, explain how functions can help achieve the software engineering goals of
modularity and reusability.

7. {0 marks} The BC Lions are in first place. Does the BC Lions football team have what it takes to
win the Grey Cup championship next month in Toronto?
a) Yes.
b) Im not sure, primarily because of the quarterbacking situation.
c)
I
have
my
doubts
______________________________________________________.
d) No.
e) I dont have an opinion (or I dont care, or I dont follow football).

because

8. {4 marks} Evaluate the following boolean expression and show the output. You must show how you
arrived at your final answerfor example, write T/F above the various parts of the expression, to show
how you determined whether or not the expression was true or false overall.
There is no need to construct a truth table.
#define
#define
int
int
int

TRUE
FALSE

1
0

c = FALSE;
d = TRUE;
e = 2;

if (c || (!c && (e * 2 == 4) && (c || d) )


printf(The answer is true.\n);
else
printf(The answer is false.\n);

Page 3

9. {7 marks} Finish the following short program to determine the number of times that an integer x can
be divided by 3, and still remain a positive integer. For example, if x = 27, then it can be divided by
3 three times: x = 27 becomes x = 9 (first time), x=9 becomes x = 3 (second time), and x=3 becomes
x=1 (third time), but then x=1 becomes x=0 (so, we thats as far as we go for this value of x).
Note, however, that x=5 can only be divided by 3 once because: 5 / 3 is 1 (remember integer division?),
and after that 1 / 3 is 0. Note that x=7 can also be divided by 3 only once.
Finally, note that the program terminates only when the user enters -1. Thus, the user can enter many
possible values for x before the program quits.
If you wish you can make changes to my variables below, but this will work well for most of you.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x, save_x, count;
while (1)
{
printf("Enter an integer (-1 to quit):");
scanf("%i", &x);
save_x = x;

printf("We can divide %i by 3, %i times\n", save_x, count);


} /* end of while (1) loop */
system("pause");
return 0;
}

Page 4

10. {8 marks} Trace tables are used to track the values of variables throughout execution. For the
following fragments of code, (a) trace the code for the main function to the right of main, (b) trace the
code for function1 to the right of its code, and (c) show the output of this complete
program in the box at the bottom of this page.
#include <stdio.h>
#include <stdlib.h>
int function1( int first, int second);
int main(void)
{
int A = 2010;
int B = 500;
int C = -1;
C = function1(A, B);
printf("A is %i, B is %i, C is %i\n", A, B, C);
C = function1( (int) (B * 0.01), 1);
printf("A is %i, B is %i, C is %i\n", A, B, C);
system("pause");
return 0;
}
int function1(int num1, int num2)
{
num1 = num1 % num2;
num2 += num1;
if (num1 > num2)
return ++num2;
return --num1;
}

Output from the program:

Reminder: Dont forget to trace the variables!

Page 5

11. {7 marks} Write a function only ... according to the following instructions. Do not write the main
program. Note that the function accepts two parameters: a drug manufacturers standard adult
dosage (in milligrams, an integer) and the patients weight (in pounds, an integer).
Compute the dosage of medicine for the patient according to the patients weight. Dont worry about
what the medicine is called, or what its for.
Here is what you are to do for the function that computes the patients adjusted dosage. If the patients
weight is at least 120 pounds but not more than 190 pounds, then the dosage to be returned to the caller
should be the number of milligrams recommended by the manufacturers standard dosage for an adult.
Patients under 120 pounds should get exactly of the standard dosage, unless the patient is under 60
pounds in which case he/she should get exactly of the standard dosage. If the patient is more than 190
pounds, give exactly 1.5 times the standard dosage.

Page 6

12. {7 marks} Finish the calls below to perform file reading and writing for the following application.
Look for the lines in bold that say ***** COMPLETE THE ... *****. Nothing else needs to be
codedjust 2 lines.
The goal is to read 3 integers from a file: a hockey players unique player_ID, the number of goals
scored, and the number of assists made. The number of points that a player scored is simply the number
of goals plus the number of assists. We want to create an output file consisting of only 2 items of data
per line in the output file: the players ID number, and the number of points for that player.
Heres a sample nhl_players.txt file:

Heres a sample nhl_points.txt output file:

1001
1002
1003
1004

1001
1002
1003
1004

32
28
1
44

24
66
18
45

56
94
19
89

#include <stdio.h>
#include <stdlib.h>
#define FILENAME "nhl_players.txt"
#define FILENAME2 "nhl_points.txt"
int main(void)
{
int
num_players = 0;
int
playerID, goals, assists, points;
FILE
*nhl, *nhl_output;
/* open files for reading and writing */
nhl = fopen(FILENAME, "r");
nhl_output = fopen(FILENAME2, "w");
if (nhl == NULL || nhl_output == NULL)
printf("Check your file names ... something's wrong!\n");
else
{
/* Read the file in the WHILE condition, until end of file. */
/* ***** COMPLETE THE FOLLOWING WHILE CONDITION ***** */
while (
{
num_players++;
points = goals + assists;
/* ***** COMPLETE THE WRITE TO THE OUTPUT FILE ***** */
}
printf("A total of %i players were
processed.\n",num_players);
}
fclose(nhl);
fclose(nhl_output);
system("pause");
return 0;
}
Page 7

13. {13 marks} For this part of the question you need to do 2 things on the next page: (a) complete the
short main function (with appropriate prototype and call statements), and (b) write a short
function called calculate_oil_in_reservoir (to perform the work described below). To save time
on this question, you dont have to include comments, but you must code a symbolic constant.
Here is some background about the application. It is often helpful to know the engineering application
to motivate the example.
An oil reservoir can be thought of as an underground pool of oil; however, the pool is usually made
up of the oil collected in the pores (spaces) of rock fragments. A simple model of a reservoir is kind of
like a glass of soda pop containing both ice fragments (i.e., the rocks) and Coca-Cola (the oil)
around the spaces between the ice fragments.
Ghawar, for example, with its excellent porosity (i.e., lots of spaces in the rocks) and light, easy-flowing
oil, is the worlds largest oil reservoir (its in Saudi Arabia). For decades, Ghawar has produced over
5,000,000 barrels per day of high quality Arab Light crude oil, or 63% of Saudis total output! It is
doubtful whether another Ghawar will ever be found, anywhereand Ghawar is gradually running out
of oil.
The following formula (taken from Oil and Gas Production in Non-Technical Language by Martin S.
Raymond and William L. Leffler) is sometimes used to compute the amount of oil in a rock
formation:
N = (1 Sw) porosity A h Bo 7,758
where N is the total oil in place (in barrels), Sw is the water saturation percentage, A is the drainage area
(in acres), h is the net pay thickness of the formation, Bo is the oil shrinkage factor (a measure of how
much oil shrinks as it reaches surface temperature and pressure), and 7,758 is a constant to convert acrefeet into barrels.
Example: Suppose: (a) the resistivity from induction logs indicates that water saturation is 15% (0.15);
(b) drilling cores or the density, neutron, or sonic logs suggest that porosity is 25% (0.25); (c) the
drainage area is 250 acres; (d) the gamma ray or SP logs show the net pay thickness to be 27 feet,
and (e) the oil shrinkage factor based on experience with oil common to the area is about 0.6.
Then, N = (1 0.15) 0.25 250 27 0.6 7,758 = 6,676,729 barrels of oil in the reservoir. By
the way, about 30% of the oil in a reservoir is ultimately recoverable by an oil companyyes, most
of the oil remains in the ground, awaiting better technology (or may never be pumped to the
surface). (But ignore this 30% figure in your calculations.) The main program should print the
answer on the screen.

(write your code on the next page)

Page 8

Finish the main program below, and write your function at the bottom of the page. More space is
available on the next page if you need it. Your entire program should be able to compile and run
successfully, so dont leave any parts out. Note that a bunch of code has been written for you.
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double water_sat, rock_porosity, acres, net_pay_thickness, shrinkage;
printf("Enter water_saturation percentage\n");
scanf("%lf", &water_sat);
printf("Enter rock porosity percentage\n");
scanf("%lf", &rock_porosity);
printf("Enter drainage area in acres\n");
scanf("%lf", &acres);
printf("Enter net_pay_thickness\n");
scanf("%lf", &net_pay_thickness);
printf("Enter oil_shrinkage_factor\n");
scanf("%lf", &shrinkage);

system("pause");
return 0;
}

Page 9

<Use this page if you need it for the rest of the previous question.>

THIS IS THE LAST PAGE OF THE EXAM.

Page 10

Vous aimerez peut-être aussi