Hand in Typed Or Handwritten C Code Containing Your Solutions of Problems 1-7 (You Do Not

Total Page:16

File Type:pdf, Size:1020Kb

Hand in Typed Or Handwritten C Code Containing Your Solutions of Problems 1-7 (You Do Not

Homework 5 due 05/03/02

Hand in typed or handwritten C++ code containing your solutions of problems 1-7 (you do not need to run the code). 1. How many function calls are performed during computing  Fibonacci(6) by the algorithm int Fibonacci(int n) { if(n==0 || n==1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); }  C(6,2) by the algorithm int C(int n,int k) { if(k==0 || k==n) return 1; else return C(n-1,k-1)+C(n-1,k); }

2. Write a recursive C++ function which computes powers of 2 in the following manner  1 if n  0  2 P(n)   (P(n / 2)) if n  0 and n is even  2 2*(P((n 1) / 2)) if n  0 and n is odd How many function calls are performed during computing P(1025)?

3. Draw the decision tree for the binary search algorithm acting on a sorted list consisting of 12 elements (if there is no middle take the middle right element). Example: For a 6 element list the tree is the following. x>L[.] –go to the right, x

4. Consider the binary search algorithm acting on a sorted list consisting of 14 elements (if there is no middle take the middle left element). Compute the following with the help of the decision tree: (i) The number of possible executions (the number of ways the algorithm may proceed inside the decision tree) (ii) The average number of operations (iii) The number of operations in the worst case

5. Write a program, which reads characters from an input stream, pushes them one by one to a stack and then pops them and prints out in the reverse order.

6. Eliminate recursion from the following function and substitute it by an appropriate use of a stack. void Dec_to_Bin(int n) { int m,r;

if(n>0) { r=n%2; m=n/2; Dec_to_Bin(m); cout << r; } }

7. Eliminate recursion from the following function and substitute it by an appropriate use of a stack. void list_print_reverse(Node* ptr) { if(ptr!=NULL) { list_print_reverse(ptr->link); cout << ptr->data; } }

X-tra credit:

Write a program, which takes logical formulas as input and checks whether they are logical tautologies. Input formulas may contain any number of variables. Logical operators are defined by their truth tables:

not p  p 0 1 1 0

and p q pq 0 0 0 0 1 0 1 0 0 1 1 1

or p q pq 0 0 0 0 1 1 1 0 1 1 1 1

if and only if p q p q 0 0 1 0 1 0 1 0 0 1 1 1

if then p q pq 0 0 1 0 1 1 1 0 0 1 1 1

0 means false and 1 means true. A logical tautology is a logical formula, which is true for all possible logical values of variables involved in it. For example  (pq)(  pq) is a logical tautology. For all possible logical values of p and q the resulting logical value of the above expression is 1. Make sure that your program accepts any logical formula without limiting the number of variables. You may assume for simplicity that a pair of parentheses accompanies any logical operator. Hint: Modify the algorithm for evaluating arithmetic expressions.

Your solution should consist of a tested C++ program submitted in UNIX environment or as a Visual C++ workspace. To get the credit for your solution you will need to make an appointment with me before or on 05/08/02 to demonstrate your solution (you will need to run your programs with various input data as well as discuss your implementation).

Recommended publications