csc180f_2001_exam - H it University of Toronto Faculty of...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: H it University of Toronto Faculty of Applied Science and Engineering Final Examination, December 2001 CSC 180H1F: Introduction to Computer Programming Duration: 25/: hours Exam Type: D Aids allowed: One 856x11" aid sheet (both sides). No calculators. Examiner: Alan Rosenthal La5t name: First name(s): Student number: Make sure you have all seven pages (including this page). Note: Pay attention to whether the question asks for a complete program (with #include. for example. although a prologue comment is not required for this exam) or asks only for a function. Note: Answers not in the correct space will not be graded unless a note in the correct space says “see page and the answer on that page is clearly labelled with the question number, Be careful not to get Stuck on some questions to the complete exclusion of others. The amount of marks allotted does not necessarily indicate how long it will take you to complete the quesuon, nor does the size of the answer-space indicate the size of the correct answers Leave this table blank. ! ——z-n ——-ln- —=— --:_ l. N La.) 3.x CSC 180. December 2001. page 1 of? ll 1. [7 marks] (the “philosophical” question) Here is a code fragment which calls pnntf. Printf is strange because it can take different numbers of arguments. if (x z: 0) printft‘No glops were found.\n‘); else printf('%d glops were found.\n', X): The following code fragment calls a hypothetical void funcuon “p”: if (x == ) 9(5); else PlS, X); Why is [his PfOblemaliC? That is. why is the “p” example an impossible situation even though the “prtntf" example is workable? 2. [10 marks] Write a program fragment (doesn‘t even need to be in a function) which computes the lowest number n greater than 1000 such that n = 31:2 for some integer k. CSC 180. December 2001. page 2 of 7 1| 4| 3. [16 marks] Consider the following declarations and initializations. The array initialization you see sets a[01 to be 12. a[l] to be 21. and a[2] to be 31. int n = 13; int m = 6: double x = 10; char 5(8) = “abode”; int a[3] = ( 12, 21, 31 }: For each of the following expressions. give both the type and the value of the expression. (Use the most simplified form for each value; eg. do not write “3+5” when you mean 8.) An example is given. Note that the ASCII value of ‘a’ is 97, ‘b’ is 98, ‘c' is 99. and so on. Expression TEE Value aEO] + m int 18 SE21 sl2] + x x + a[1] / a[2] n + m / x 4. [8 marm] What does this program fragment output? Again, the letters are in order in the ASCII character code; for example, 'a'+3 is 'd'. int i. j; for (i = 0; i < 5: j++l ( j = i ' 3 + 1: if (j > 10) putcharl‘a‘ + (j - 10)); CSC [80. December 200i, page 3 of 7 ll I) {I 5. [10 marks] Write a recursive function which takes two parameters, a string and a character, and outputs the longest final string (a substring which goes up to the end of the stn’ng)‘f which Starts with that character. For example, final("hello", 'l') will return the string “110" (a pointer to the first ‘1',i.e. its argument plus two, by pointer arithmetic), and final("ababdef", ‘b') will return the string “babdef”. Since this function's return type wrll be a pointer (char *). we will return NULL if the character does not actually appear in the string. So final("hello". 'g') will return NULL. The recursive call can pass s+1 (where s is the string parameter) to pass in a string lacking the first character. using pointer arithmetic. (This is only valid if sis nm the empty string, Le. if s[0] l: '\0' — we may not look at the array past the '\0'.) The function header is provided. The function must be written recursively (no explicit loops). char *finaltchar ‘5. char c) { I“ n *- For example, the complete set of final strings of uhello" is: “hello”. “ello”. “110", “10' . o . and CSC 180, December 2001. page 4 of 7 {l l) 6. {10 marks] Recall the infinite series we discussed which sums to Tt: 44444 = —-—-4-—u-—.+._- 1 3 5 7 9 Write a function named “pi” which takes an integer parameter which is the number of terms of this senes to add together, and returns a double value which is the sum. For example. pi(1) returns 4. The function header is provided. double pilint n) { 7. [9 marks] Here is a recursive factorial luncuon: int factorialiint n) { if (n < 2) return 1: else return n * Eactorialln - 1); } Heie is a function which recurses infinitely if its parameter is 5. otherwise it is the identity function: int funnyidentitylint n) i if in == 5) return funnyidentityln); else return n; Can you make a recursive function which recurses infinitely if its parameter is 5, Otherwise is a recursive factorial function? CSC 180. December 2001, page 5 of 7 h 8. [15 marks] [n this problem. everyone has one or zero best friends. People are represented by non-negative integers. There is a “friend” library, with apprOpriate declarations in a sySLem include file <friend.h>. Among other useful functions for manipulating friends. it contains a function “bestfriend” which takes one integer argument and returns that person's one best friend. or —1 if that person doesn’t have a best fnend. a) What is the ‘extem' declaration of bestfriend which will appear in the sySIem include file friendh? b) There is an implicit chain of best friends. If 3's best friend is 5, and 5's best friend is 8. and 8‘s beSt friend is 6, then we could say that 6 is an “eventual best friend" of 3. Since everyone has only one best friend (or none). if you keep going down this chain, you eventually find all of the starting person’s “eventual best friends”. (Assume that there are no loops. e.g. if 3's best friend is S. then 5’s best friend is not going to be 3.) Write a complete C program which uses the bestfriend funcu'on to determine whether or not 9 is 2's eventual best friend. (Your program takes no input.) You do not write the “bestfriend” function; it is in the “friend” library. CSC 180. December 2001, page 6 of 7 I! I? {I 9. [l5 marks] Write a complete program which repeatedly prompts the user for a file name, then attempts to open the file. and if it succeeds it reads and displays the first character of the file ( read it with getc(fp)). If it fails, it must call perrorO. It does this repeatedly in a loop until the user types “quit”. (Since this is in a loop, you must fcloseO the open file before opening the next one (or your program will Stop working after a set number of files which is the limit on how many files you can have open at once).) Helpful snippet (use as you like below. or not (you can say “above stuff goes here" or cop)r it)): if (fgets(buf, sizeof buf, stdin) == NULL) return 0: if (strcmp(buf. 'quit\n'} == 0} return 0; CSC 180, December 2001. page 7 of 7 H 1| 1! 4| Extra space if needed (you must write “see page 8" in the usual answer space for the question) ...
View Full Document

This note was uploaded on 02/13/2010 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 8

csc180f_2001_exam - H it University of Toronto Faculty of...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online