apsc 160 (ubc)

10
UBC APSC 160 - Fall 2008 Midterm Examination Page 1 of 10 The University of British Columbia APSC 160: Introduction to Computation in Engineering 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.

Upload: jfoxx7777

Post on 15-Jan-2016

108 views

Category:

Documents


2 download

DESCRIPTION

APSC 2008 Midterm Solution

TRANSCRIPT

Page 1: APSC 160 (UBC)

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.

jthillik
Typewritten Text
jthillik
Typewritten Text
SOLUTIONS
jthillik
Typewritten Text
100
Page 2: APSC 160 (UBC)

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

jthillik
Typewritten Text
9001
jthillik
Typewritten Text
9000
jthillik
Typewritten Text
0.0
jthillik
Typewritten Text
True (or 1)
jthillik
Typewritten Text
False (or 0)
jthillik
Typewritten Text
garbage value
jthillik
Typewritten Text
2.2
Page 3: APSC 160 (UBC)

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

jthillik
Typewritten Text
Error (div by zero)
jthillik
Typewritten Text
5
jthillik
Typewritten Text
1
jthillik
Typewritten Text
2
jthillik
Typewritten Text
0
jthillik
Typewritten Text
False (or 0)
Page 4: APSC 160 (UBC)

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.

jthillik
Typewritten Text
#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 sushi: %.1lf \n", jo, sr); return 0; }
Page 5: APSC 160 (UBC)

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 ) {

}

jthillik
Typewritten Text
FILE* pay = fopen("payment.txt", "w"); if(pay == NULL) return; for(;numGs > 0; numGs--) { fprintf(pay, "%c", 'G'); } fclose(pay);
Page 6: APSC 160 (UBC)

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

jthillik
Typewritten Text
void makePayment(int num, char c);
jthillik
Typewritten Text
makePayment(3, 'd');
jthillik
Typewritten Text
double rectArea(double, double);
jthillik
Typewritten Text
double rectArea(double s1, double s2) { return s1 * s2; }
Page 7: APSC 160 (UBC)

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.

jthillik
Typewritten Text
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; }
Page 8: APSC 160 (UBC)

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

jthillik
Typewritten Text
(age >= 25 && age <= 40 && !hasTattoos) || (netWorth > 500000 && goesToChurch)
Page 9: APSC 160 (UBC)

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?)

jthillik
Typewritten Text
jthillik
Typewritten Text
jthillik
Typewritten Text
T T T F F T F F
jthillik
Typewritten Text
!(p||q)
jthillik
Typewritten Text
!p && !q
jthillik
Typewritten Text
jthillik
Typewritten Text
F F F F F F T T
jthillik
Typewritten Text
yes (by the above truth table)
jthillik
Typewritten Text
T T T F F T F F
jthillik
Typewritten Text
!(p&&q)
jthillik
Typewritten Text
!p && !q
jthillik
Typewritten Text
F F T T T T T T
jthillik
Typewritten Text
yes (by the above truth table)
Page 10: APSC 160 (UBC)

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