apsc 160 (ubc)
DESCRIPTION
APSC 2008 Midterm SolutionTRANSCRIPT
UBC APSC 160 - Fall 2008 Midterm Examination Page 1 of 10
The University of British Columbia
APSC 160: Introduction to Computation inEngineering Design
Fall 2008. Midterm Examination. Friday, October 17 , 2008.th
Examiner: Jeremy T. Hilliker
Last Name: First Name: Student #:
Signature: Lab Section:
(give date/time if unknown)
Q# Awarded Of Marker
1 12
2 25
3 18
4 14
5 14
6 6
7 10
Total 99
• 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 Page 2 of 10
Q1 (12 points) ExpressionsState 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 b
B) (1 points)double a = 1 / 2;
a
C) (2 points)int a, b; // note: this is not an error in the questiona = a || !a;b = b && !b;
a b
D) (1 point)int a; // note: this is not an error in the questionint b = 1 + a;
b
E) (1 point)double a = 1.2, b = 1.8;double c = a + (int) b;
c
UBC APSC 160 - Fall 2008 Midterm Examination Page 3 of 10
F) (1 point)int a = 0, b = 1;double c = b / a;
c
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 b
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
UBC APSC 160 - Fall 2008 Midterm Examination Page 4 of 10
Q2 (25 points) Programming: Implement a Complete ProgramYour Nanna (grandmother) is having a party. She has asked you to help by preparing a Jell-Osalad 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 don’t want to offend Nanna by getting the amounts wrong, so you decide to write a programto 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.
UBC APSC 160 - Fall 2008 Midterm Examination Page 5 of 10
Q3 (18 points) Programming: File OutputYou are planning to buy a new car before Nanna’s party. You are considering three options. Thesalesman has told you that the new, boring car costs 28 “G”s, a used, boring car costs 18 “G”s,and an [insert term for your preferred gender] magnet costs 46 “G”s. You know that you can usethe advanced programming skills that you learned in APSC 160 to generate those amounts ofletters.
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 ) {
}
UBC APSC 160 - Fall 2008 Midterm Examination Page 6 of 10
Q4 (14 points) Function BasicsA) (7 points)You returned to the car dealership with your file containing the correct number of “G”s. Much toyour disappointment, the salesman now says that the car that you chose is priced in “K”s. Youknow that you can change your program to output “K”s instead of “G”s, but your best friend hastold you that they need 4 “C”s to pay this month’s rent.
i) If you change the function makePayment (from the previous question) to output a specifiednumber of any (single) specified letter (passed as arguments), what would its new prototypebe?
ii) How would you call your new function to output three (3) occurrences of the letter “d” to thefile “payment.txt”?
B) (7 points)Nanna is concerned that her serving tray is not big enough for all of the sushi rolls which youprepared. Nanna needs to know the surface area of her rectangular tray, but she only knows thelength of its sides. Nanna trusts that you, her favorite and most gifted grandchild, can help her bywriting a function to compute the area of a rectangle given the length of its sides.
What is the prototype for this function?
Implement the function based on the following formula:
Area = side1 * side2
UBC APSC 160 - Fall 2008 Midterm Examination Page 7 of 10
Q5 (14 Points) Functions, Conditional Statements
Nanna’s guests were so impressed by you at her party that you received multiple job offers! Youwere given several before-tax (gross) salary figures, but you want to know what they will be inafter-tax (net) dollars.
After-tax (net) income is computed as: net = gross - taxPayableIncome 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.
UBC APSC 160 - Fall 2008 Midterm Examination Page 8 of 10
Q6 (6 points) Boolean ExpressionsNanna noticed your new car (and your new friend who liked your new car) at the party. Nannawanted to remind you that if you wanted to stay in her will, that you must marry someone whomeets her high standards.
Nanna suggests that you only date people who are between the ages of 25 and 40 (inclusive), andhave 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 Nanna’s wishes. Your expression should evaluateto true if (and only if) Nanna’s conditions are met.
Assume that your date’s attributes are already assigned to the following variables:
int age, netWorth; // age in years, netWorth in dollarsdouble height, weight; // height in meters, weight in lbsint hasTattoos, goesToChurch; // boolean
UBC APSC 160 - Fall 2008 Midterm Examination Page 9 of 10
Q7 (10 points) More Boolean Expressions, Truth TablesNanna 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
p q
Conclusion: (are they equivalent?)
UBC APSC 160 - Fall 2008 Midterm Examination Page 10 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
e Scientific notation
f Decimal floating point
s String