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

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

<p>Homework 5 due 05/03/02</p><p>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); } </p><p>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)?</p><p>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<L[.] – go to the left, x=L[.] go to the middle</p><p>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</p><p>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.</p><p>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;</p><p> if(n>0) { r=n%2; m=n/2; Dec_to_Bin(m); cout << r; } }</p><p>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; } }</p><p>X-tra credit: </p><p>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:</p><p> not p  p 0 1 1 0</p><p> and p q pq 0 0 0 0 1 0 1 0 0 1 1 1</p><p> or p q pq 0 0 0 0 1 1 1 0 1 1 1 1</p><p> if and only if p q p q 0 0 1 0 1 0 1 0 0 1 1 1</p><p> if then p q pq 0 0 1 0 1 1 1 0 0 1 1 1</p><p>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.</p><p>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). </p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us