The University of British ColumbiaAPSC 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. 2 10 3 8 This is a closed book examination. No books, help sheets, calculators, cell phones, or other materials may be used. 4 7 5 8 Work quickly and do the easy questions first. Part marks will be awarded for most questions. 6 7 7 7 8-10 13 Total 60 Fill in your name, ID, signature, and lab section above. To save time, you don’t 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 When a program runs on the CPU. made by IBM e) a new disk drive that just went on sale yesterday at Staples or FutureShop 5. 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.{10 marks} Multiple choice questions. Circle the best answer for each question. 1. 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 . 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 that’s 1 year old. 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 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. e) I don’t have an opinion (or I don’t care. because 8. {4 marks} Evaluate the following boolean expression and show the output.\n”). There is no need to construct a truth table. explain how functions can help achieve the software engineering goals of modularity and reusability. if (c || (!c && (e * 2 == 4) && (c || d) ) printf(“The answer is true.6. c) I have my doubts ______________________________________________________. You must show how you arrived at your final answer—for example. b) I’m not sure. or I don’t follow football). e = 2. d = TRUE. {4 marks} Briefly. #define #define int int int TRUE FALSE 1 0 c = FALSE. to show how you determined whether or not the expression was true or false overall. 7. primarily because of the quarterbacking situation. Page 3 .\n”). Does the BC Lions football team have what it takes to win the Grey Cup championship next month in Toronto? a) Yes. else printf(“The answer is false. {0 marks} The BC Lions are in first place. write T/F above the various parts of the expression. d) No. the user can enter many possible values for x before the program quits. x=9 becomes x = 3 (second time). If you wish you can make changes to my variables below. For example. while (1) { printf("Enter an integer (-1 to quit):"). that x=5 can only be divided by 3 once because: 5 / 3 is 1 (remember integer division?). save_x = x. } Page 4 . note that the program terminates only when the user enters -1. {7 marks} Finish the following short program to determine the number of times that an integer x can be divided by 3. save_x. return 0. but this will work well for most of you. Finally.h> int main(void) { int x. &x). count). } /* end of while (1) loop */ system("pause"). scanf("%i". however. and x=3 becomes x=1 (third time). we that’s as far as we go for this value of x). then it can be divided by 3 three times: x = 27 becomes x = 9 (first time). count.h> #include <stdlib. but then x=1 becomes x=0 (so. #include <stdio.9. and still remain a positive integer. if x = 27. Note. and after that 1 / 3 is 0. save_x. printf("We can divide %i by 3. Thus. %i times\n". Note that x=7 can also be divided by 3 only once. C). C). A. B is %i. (b) trace the code for function1 to the right of its code. int second). (a) trace the code for the main function to the right of main. {8 marks} Trace tables are used to track the values of variables throughout execution. C = function1(A. printf("A is %i. printf("A is %i.10. 1). int B = 500. A. B). num2 += num1.h> int function1( int first. return 0. int C = -1. C is %i\n". B is %i.h> #include <stdlib. int main(void) { int A = 2010. and (c) show the output of this complete program in the box at the bottom of this page. #include <stdio.01). system("pause"). if (num1 > num2) return ++num2. C = function1( (int) (B * 0. int num2) { num1 = num1 % num2. return --num1. } int function1(int num1. B. B. } Output from the program: Reminder: Don’t forget to trace the variables! Page 5 . For the following fragments of code. C is %i\n". give exactly 1. then the dosage to be returned to the caller should be the number of milligrams recommended by the manufacturer’s standard dosage for an adult. Do not write the main program. Patients under 120 pounds should get exactly ½ of the standard dosage. Compute the dosage of medicine for the patient according to the patient’s weight. unless the patient is under 60 pounds in which case he/she should get exactly ¼ of the standard dosage.5 times the standard dosage. If the patient is more than 190 pounds. Page 6 . or what it’s for. according to the following instructions.11. {7 marks} Write a function only . Here is what you are to do for the function that computes the patient’s adjusted dosage. an integer) and the patient’s weight (in pounds. an integer). Note that the function accepts two parameters: a drug manufacturer’s standard adult dosage (in milligrams.. Don’t worry about what the medicine is called.. If the patient’s weight is at least 120 pounds but not more than 190 pounds. nhl_output = fopen(FILENAME2. } Page 7 . assists. } fclose(nhl). The goal is to read 3 integers from a file: a hockey player’s unique player_ID. the number of goals scored. goals. We want to create an output file consisting of only 2 items of data per line in the output file: the player’s ID number. until end of file. "w"). points = goals + assists. return 0.12.. else { /* Read the file in the WHILE condition.num_players). /* open files for reading and writing */ nhl = fopen(FILENAME. Look for the lines in bold that say ***** COMPLETE THE . {7 marks} Finish the calls below to perform file reading and writing for the following application.txt" int main(void) { int num_players = 0. points. /* ***** COMPLETE THE WRITE TO THE OUTPUT FILE ***** */ } printf("A total of %i players were processed. and the number of assists made. *nhl_output. "r"). int playerID. */ /* ***** COMPLETE THE FOLLOWING WHILE CONDITION ***** */ while ( { num_players++.\n". Here’s a sample nhl_players.txt file: Here’s a sample nhl_points. something's wrong!\n").txt" #define FILENAME2 "nhl_points. if (nhl == NULL || nhl_output == NULL) printf("Check your file names . and the number of points for that player. fclose(nhl_output).. system("pause").. Nothing else needs to be coded—just 2 lines.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. The number of points that a player scored is simply the number of goals plus the number of assists. FILE *nhl. {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). lots of spaces in the rocks) and light.) The main program should print the answer on the screen.25). (write your code on the next page) Page 8 . (b) drilling cores or the density.758 where N is the total oil in place (in barrels). most of the oil remains in the ground. or 63% of Saudi’s total output! It is doubtful whether another Ghawar will ever be found. and (e) the oil shrinkage factor based on experience with oil common to the area is about 0. A is the drainage area (in acres). about 30% of the oil in a reservoir is ultimately recoverable by an oil company—yes. Then.. Ghawar. N = (1 – 0. however.729 barrels of oil in the reservoir. For decades. 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. To save time on this question.000 barrels per day of high quality Arab Light crude oil.e.15) × 0. the “rocks”) and Coca-Cola (the “oil”) around the spaces between the ice fragments.13. By the way. awaiting better technology (or may never be pumped to the surface).758 is a constant to convert acrefeet into barrels.e. (c) the drainage area is 250 acres. easy-flowing oil. and 7. (d) the gamma ray or SP logs show the net pay thickness to be 27 feet. Ghawar has produced over 5.25 × 250 × 27 × 0. neutron. Bo is the oil shrinkage factor (a measure of how much oil shrinks as it reaches surface temperature and pressure). The following formula (taken from “Oil and Gas Production in Non-Technical Language” by Martin S. Example: Suppose: (a) the resistivity from induction logs indicates that water saturation is 15% (0. h is the “net pay thickness” of the formation. anywhere—and Ghawar is gradually running out of oil.758 = 6.000.676. you don’t have to include comments. but you must code a symbolic constant.15). It is often helpful to know the engineering application to motivate the example. Here is some background about the application. An oil reservoir can be thought of as an underground “pool” of oil. with its excellent porosity (i. is the world’s largest oil reservoir (it’s in Saudi Arabia). (But ignore this 30% figure in your calculations. Sw is the water saturation percentage.6 × 7.6. A simple model of a reservoir is kind of like a glass of soda pop containing both ice fragments (i. for example. and (b) write a short function called “calculate_oil_in_reservoir” (to perform the work described below). the pool is usually made up of the oil collected in the pores (spaces) of rock fragments. or sonic logs suggest that porosity is 25% (0. &water_sat). scanf("%lf". printf("Enter water_saturation percentage\n"). scanf("%lf". return 0. &acres).h> int main(void) { double water_sat. Note that a bunch of code has been written for you. net_pay_thickness. so don’t leave any parts out. shrinkage. &net_pay_thickness).Finish the main program below. acres. &shrinkage). scanf("%lf". printf("Enter net_pay_thickness\n"). Your entire program should be able to compile and run successfully. printf("Enter drainage area in acres\n"). and write your function at the bottom of the page. system("pause"). scanf("%lf". #include <stdio. printf("Enter oil_shrinkage_factor\n"). } Page 9 . More space is available on the next page if you need it. printf("Enter rock porosity percentage\n").h> #include <stdlib. rock_porosity. &rock_porosity). scanf("%lf". <Use this page if you need it for the rest of the previous question.> THIS IS THE LAST PAGE OF THE EXAM. Page 10 .