Final-Spring04-Solutions - University of Waterloo...

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

View Full Document Right Arrow Icon
SE240 Final Exam, Spring 2004 Page 1 of 16 University of Waterloo Department of Electrical and Computer Engineering SE 240 Algorithms and Data Structures Spring 2004 Final Examination Instructor: Ladan Tahvildari Date: Monday, August 9, 2004, 9:00 a.m. to 12:00 noon. Duration: 3 hours Type: Special Materials Instructions: There are 9 questions. Answer all 9 questions. Standard calculator allowed but no additional materials allowed. The number in brackets denotes the relative weight of the question (out of 100). If information appears to be missing from a question, make a reasonable assumption, state it and proceed. Write your answers directly on the sheets. If the space to answer a question is not sufficient, use the last (overflow) page. When presenting programs, you may use any mixture of pseudocode/C++/Java constructs as long as the meaning is clear. Name Student ID Question Mark Max Marker 1 15 2 5 3 10 4 5 5 15 6 10 7 15 8 15 9 10 Total 100
Background image of page 1

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

View Full DocumentRight Arrow Icon
SE240 Final Exam, Spring 2004 Page 2 of 16 Name: Student ID: Question 1: Algorithm Analysis [15] Consider the program given below. Be sure to reduce all answers to their simplest closed form (i.e. your answer should not have contain any summation). 1 #include <stdio.h> 2 3 int bar(int x, int n) 4 { 5 int i, sum; 6 sum = 0; 7 for (i=1; i<=n; i++) 8 sum = sum + x; 9 return (sum); 10 } 11 12 int foo(int x, int n) 13 { 14 int sum; 15 if (n==1) 16 sum = bar(x,1); 17 else 18 sum = bar(x,n) + foo(x,n-1); 19 return(sum); 20 } 21 22 int main(int argc, char *argv[]) 23 { 24 int n,x; 25 scanf(“%d”, &n); 26 scanf(“%d”,&x); 27 printf(“%d\n”, foo(x,n)); 28 } Part A. bar Operation [2] As a function of x and n , what value does bar(x,n) return? Show all your work. This function sums x for n times. Thus, nx x n x bar n i = = = 1 ) , ( .
Background image of page 2
SE240 Final Exam, Spring 2004 Page 3 of 16 Name: Student ID: Part B. Running Time Complexity for bar [1] Give a simple and tight Big-Oh upper bound for the running time for bar as a function of the input parameters n and x . Show all your work. The value of x clearly does not influence the running time of bar. The statement inside the loop takes O(1) time. Since this loop is executed n times, the loop is O(n). Then the bar function is O(n). Part C. foo Operation [5] As a function of x and n , what value does foo(x,n) return? Show all your work. Base case: n = 1 foo(x, 1) = bar(x, 1) = x Induction: n > 1 foo(x, n) = bar(x, n) + foo(x, n – 1) Writing out a few terms in recursion foo(x, 2) = bar(x, 2) + foo(x, 1) = 2x + x foo(x, 3) = bar(x, 3) + foo(x, 2) = 3x + 2x + x In general: 2 ) 1 ( ) , ( 1 1 + = = = = = n n x i x ix n x foo n i n i . Part D. Running Time Complexity for
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Page1 / 16

Final-Spring04-Solutions - University of Waterloo...

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

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