{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

h6-sol

# h6-sol - Solutions of Homework 6 Programming Project The...

This preview shows pages 1–3. Sign up to view the full content.

Solutions of Homework 6 : Programming Project The following is the complete C program that implements the given function with the required modifications. #include <math.h> #include <stdio.h> #include <stdlib.h> int count; double fun(double n){ count++; if (n<=2) return (1); else return (5+fun(pow(n,1.0/3.0))); } int main(){ int i; printf("n\t\tCount\t\tReturned Value\n"); printf("=====\t\t=====\t\t==============\n"); for (i=0; i<6; i++){ count = 0; printf("2^%3.0f\t\t%d\t\t%5.2f\n",pow(3,i),count,fun(pow(2.0,pow(3.0,i)))); } } The output of the above program is: n Count Returned Value ===== ===== ============== 2^ 1 1 1.00 2^ 3 2 6.00 2^ 9 3 11.00 2^ 27 4 16.00 2^ 81 5 21.00 2^243 6 26.00 Note that running the program at n = 2 27 was not required in the project handout. 1

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

View Full Document
To analytically find the Count , we establish the recurrence relation that describes the number of times, T ( n ), the function fun gets called and then solve it as follows: T ( n ) = 1 + T ( 3 n ) = 1 + T ( n 1 3 ) , T (2) = 1 = 1 + parenleftBig 1 + T (( n 1 3 ) 1 3 ) parenrightBig = 2 + T ( n 1 3 2 ) = 2 + parenleftBig 1 + T (( n 1 3 2 ) 1 3 ) parenrightBig = 3 + T ( n 1 3 3 ) = 3 + parenleftBig 1 + T (( n 1 3 3 ) 1 3 ) parenrightBig = 4 + T ( n 1 3 4 ) .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}