You are on page 1of 10

The University of British Columbia

APSC 160: Introduction to Computation in


Engineering Design
Fall 2008. Midterm Examination. Friday, October 17th, 2008.
Examiner: Jeremy T. Hilliker
Last Name:
Signature:

First Name:

Student #:

SOLUTIONS

Lab Section:
(give date/time if unknown)

Duration: 60 minutes.
Length: 10 pages (including cover page and reference page).
Complete the above fields in ink (pen).
NO AIDS! No papers, notes, books, texts, or any type of electronic equipment is allowed including cell
phones, calculators, or sound/image players/transmitters/receivers (see rules #4a).
No questions except for errors or ambiguities (see rules #2).
Use the backs of facing or same page if you run out of space.
Clearly indicate where the solution is if you use backs of pages.
All questions are independent except where explicitly noted.
Always use good programing practices to get full marks (you may omit comments).

Rules governing formal examinations:


1. Each candidate must be prepared to produce, upon request, a
UBCcard for identification.
2. Candidates are not permitted to ask questions of the
invigilators, except in cases of supposed errors or ambiguities
in examination questions.
3. No candidate shall be permitted to enter the examination room
after the expiration of one-half hour from the scheduled starting
time, or to leave during the first half hour of the examination.
4. Candidates suspected of any of the following, or similar,
dishonest practices shall be immediately dismissed from the
examination and shall be liable to disciplinary action:
a. having at the place of writing any books, papers or
memoranda, calculators, computers, sound or image
players/recorders/transmitters (including telephones), or
other memory aid devices, other than those authorized by
the examiners;
b. speaking or communicating with other candidates; and
c. purposely exposing written papers to the view of other
candidates or imaging devices. The plea of accident or
forgetfulness shall not be received.
5. Candidates must not destroy or mutilate any examination
material; must hand in all examination papers; and must not
take any examination material from the examination room
without permission of the invigilator.
6. Candidates must follow any additional examination rules or
directions communicated by the instructor or invigilator.

UBC APSC 160 - Fall 2008

Midterm Examination

Q#

Awarded

Of

12

25

18

14

14

10

Total

100

Marker

99

Page 1 of 10

Q1 (12 points) Expressions


State the value of the variables after each of the independent code fragments.
If the value cannot be determined, state whether it is indeterminate or if it causes an error.
A) (2 points)
int a = 9000;
int b = a++;
a

9001

9000

B) (1 points)
double a = 1 / 2;
a

0.0

C) (2 points)
int a, b;
// note: this is not an error in the question
a = a || !a;
b = b && !b;
a

True (or 1)

False (or 0)

D) (1 point)
int a;
// note: this is not an error in the question
int b = 1 + a;
b

garbage value

E) (1 point)
double a = 1.2, b = 1.8;
double c = a + (int) b;
c

2.2

UBC APSC 160 - Fall 2008

Midterm Examination

Page 2 of 10

F) (1 point)
int a = 0, b = 1;
double c = b / a;
c

Error (div by zero)

G) (1 point)
int pi[] = { 3, 1, 4, 1, 5, 9 };
int a = pi[4];
a

H) (2 points)
int a = 1, b = 1;
if(a > 0) {
int a = 2;
b = 2;
}
// what are the values here?
a

I)
(1 point)
int inc(int a) {
return a++;
}
...
int a = 0;
inc(a);
// what is the value here?
a

J)
(1 points) [bonus question]
int a = 5;
a = !(--a);
a

False (or 0)

UBC APSC 160 - Fall 2008

Midterm Examination

Page 3 of 10

Q2 (25 points) Programming: Implement a Complete Program


Your Nanna (grandmother) is having a party. She has asked you to help by preparing a Jell-O
salad and sushi rolls. Nanna is very precise about how much of each you should prepare:
1.4 servings of Jell-O salad per guest (with a minimum of 8 servings total)
5.0 sushi rolls per guest (to a maximum of 60 sushi rolls total)
You dont want to offend Nanna by getting the amounts wrong, so you decide to write a program
to calculate them.
Write a complete program to:
1. Prompt the user for the number of guests.
2. Compute the correct amount for each item.
3. Print the correct amount for each item to 1 decimal place.
#include <stdio.h>
#define JO_PER 1.4
#define SR_PER 5
#define JO_MIN 8
#define SR_MAX 60
int main(void) {
printf("Enter # of guests: ");
int guests;
scanf("%d", &guests);
double jp = guests * JO_PER;
if(jo < JO_MIN)
jo = JO_MIN;
double sr = guests * SR_PER;
if(sr > SR_MAX)
sr = SR_MAX;
printf("jello: %.1lf
return 0;

sushi: %.1lf \n", jo, sr);

UBC APSC 160 - Fall 2008

Midterm Examination

Page 4 of 10

Q3 (18 points) Programming: File Output


You are planning to buy a new car before Nannas party. You are considering three options. The
salesman has told you that the new, boring car costs 28 Gs, a used, boring car costs 18 Gs,
and an [insert term for your preferred gender] magnet costs 46 Gs. You know that you can use
the advanced programming skills that you learned in APSC 160 to generate those amounts of
letters.
Implement the following function that outputs a specified number of the letter G to a file named
payment.txt (you must open this file yourself).
Example: if you are given the number 3 as the parameter, your text file should contain GGG
void makePayment( int numGs ) {
FILE* pay = fopen("payment.txt", "w");
if(pay == NULL)
return;
for(;numGs > 0; numGs--) {
fprintf(pay, "%c", 'G');
}
fclose(pay);

}
UBC APSC 160 - Fall 2008

Midterm Examination

Page 5 of 10

Q4 (14 points) Function Basics


A) (7 points)
You returned to the car dealership with your file containing the correct number of Gs. Much to
your disappointment, the salesman now says that the car that you chose is priced in Ks. You
know that you can change your program to output Ks instead of Gs, but your best friend has
told you that they need 4 Cs to pay this months rent.
i) If you change the function makePayment (from the previous question) to output a specified
number of any (single) specified letter (passed as arguments), what would its new prototype
be?
void makePayment(int num, char c);
ii) How would you call your new function to output three (3) occurrences of the letter d to the
file payment.txt?
makePayment(3, 'd');

B) (7 points)
Nanna is concerned that her serving tray is not big enough for all of the sushi rolls which you
prepared. Nanna needs to know the surface area of her rectangular tray, but she only knows the
length of its sides. Nanna trusts that you, her favorite and most gifted grandchild, can help her by
writing a function to compute the area of a rectangle given the length of its sides.
What is the prototype for this function?
double rectArea(double, double);

Implement the function based on the following formula:

Area = side1 * side2


double rectArea(double s1, double s2) {
return s1 * s2;
}

UBC APSC 160 - Fall 2008

Midterm Examination

Page 6 of 10

Q5 (14 Points) Functions, Conditional Statements


Nannas guests were so impressed by you at her party that you received multiple job offers! You
were given several before-tax (gross) salary figures, but you want to know what they will be in
after-tax (net) dollars.
After-tax (net) income is computed as: net = gross - taxPayable
Income tax payable is determined based on before tax earnings as follows:
gross income is 37,178 or less
15% taxes payable
gross income is more than 37,178, but not more than 74,357
5,577 plus 22% of the amount in excess of 37,178
gross income is more than 74,357
13,756 plus 26% of the amount in excess of 74,357
Complete a function which computes the after tax earnings.
double afterTaxIncome(double gross) {
double tax;
if(gross <= 37178)
tax = gross * .15;
else if(gross <= 74357)
tax = 5577 + (gross - 37178) * .22;
else
tax = 13756 + (gross - 74357) * .26;
return gross - tax;
}

UBC APSC 160 - Fall 2008

Midterm Examination

Page 7 of 10

Q6 (6 points) Boolean Expressions


Nanna noticed your new car (and your new friend who liked your new car) at the party. Nanna
wanted to remind you that if you wanted to stay in her will, that you must marry someone who
meets her high standards.
Nanna suggests that you only date people who are between the ages of 25 and 40 (inclusive), and
have no tattoos. Nanna adds that none of that will matter if the person has a net worth over
$500,000, and they go to church in which case they are okay.
Write a single boolean expression to represent Nannas wishes. Your expression should evaluate
to true if (and only if) Nannas conditions are met.
Assume that your dates attributes are already assigned to the following variables:

int age, netWorth;


// age in years, netWorth in dollars
double height, weight;
// height in meters, weight in lbs
int hasTattoos, goesToChurch; // boolean

(age >= 25 && age <= 40 && !hasTattoos)


|| (netWorth > 500000 && goesToChurch)

UBC APSC 160 - Fall 2008

Midterm Examination

Page 8 of 10

Q7 (10 points) More Boolean Expressions, Truth Tables


Nanna likes to tell you what she thinks a lot. She also likes to say things in different ways.
Sometimes, you are not sure if she is saying the same thing, or something different.
Given the following boolean expressions, use a truth table to determine if they are equivalent.
A)

!(p || q)

compared to

!p && !q

!(p||q)

Conclusion: (are they equivalent?)

B)

!(p && q)

compared to

!p && !q

yes (by the above truth table)


!p || !q

Conclusion: (are they equivalent?)

UBC APSC 160 - Fall 2008

!(p&&q)

!p && !q

yes (by the above truth table)

Midterm Examination

Page 9 of 10

Reference Sheet
<stdio.h>
int fclose(FILE *);
int feof(FILE *);
FILE* fopen(const char *, const char *);
int fprintf(FILE *, const char *, ...);
int fscanf(FILE *, const char *, ...);
int printf(const char *, ...);
int scanf(const char *, ...);
int sprintf(char *, const char *, ...);
<math.h>
double cos(double);
double exp(double);
double fabs(double);
double log(double);
double log10(double);
double pow(double, double);
double sin(double);
double sqrt(double);
double tan(double);
double hypot(double, double);
printf specifiers
c

Character

d or i

Signed decimal integer

Scientific notation

Decimal floating point

String

UBC APSC 160 - Fall 2008

Midterm Examination

Page 10 of 10

You might also like