This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CS2134 Fall 2009 HW 3 Due 11:59 PM, Wed. Oct. 21 Programming Part The following are four programming exercises that ask you to write using recursive functions. The first three functions could be easily written without recursion. But here, in order to practice recursion, you will write them recursively. We are supplying you with a main function that you can use to test your functions (the grader will use it also). The main function is in the file hw3rec.cpp, which you should download. Add your recursive functions to this file. The prototypes for the four functions are included in the file. After writing your four functions, you should compile and run the program in hw3 rec.cpp, and make necessary changes, until your functions work correctly. In your functions, you do not have to check that the arguments satisfy the preconditions. 1. Write a recursive function with the following prototype: //Precondition: v[low..high] is not empty int sum_vec(vector<int> & v, int low, int high) Your function should return the sum of the elements in the range v[low..high]. It should not have any loops in it. It should work roughly as follows: Divide the array in half. Recursively compute the sum of the elements on the left hand side. Recursively compute the sum on the elements on the right hand side. Add together those two sums and return the result. 2. Using the same approach as in part (a), write a recursive function that returns the number of elements in v[low...high] that are even. The prototype should be //Precondition: v[low..high] is not empty int num_even(vector<int> & v, int low, int high) (Note: The number 0 is even.) 3. Write a recursive function that prints out the letter V, in stars. The function takes as input n, the height of V (measured in number of stars), and a number i, which is the number of spaces that the V should be indented. For example, here is a V of height n=4, indented i=1 spaces. * * * * * * * Notice that the V can be broken into two parts, the top row, and the remaining rows. The top row has 2 stars. The remaining rows make a V of height n=3, indented i=2 spaces. Your recursive function should print out the top row of the V, and then make a recursive call to print out the rest of the V. The width of the V should be as shown above. The prototype is as follows: // Precondition: n >= 1, i >= 0 void print_V(int n,int i) If n = 1, print_V(n,i) should print a single star (indented i spaces). 4. Look at the pattern on the next page. It is a pattern that is made up of smaller patterns, which repeat the original pattern on a smaller scale. Examine the pattern carefully. Write a recursive function that prints out the pattern (and larger and smaller versions of it). The function takes as input a number n, which is a power of 2, and an indentation number, i. The longest line of the pattern has n‐1 stars, indented i spaces. [Notice ‐‐‐ thatʹs n‐1 stars, which is an odd number, rather than n stars.] The prototype is as follows: // Precondition: n is a power of 2, n > 0, i >= 0 void fractal(int n, int i) The pattern in fractalpattern.txt should be produced by running fractal(16,0). You donʹt have to check that n satisfies the precondition. Hint: Your function should have two recursive calls. * *** * ******* * *** * *************** * *** * ******* * *** * Written Part a. Consider the insertionSort code discussed in lecture and in the textbook. The vector changes as the function runs. Suppose that we added code to print out the vector at the end of each iteration of the outer for loop. What would be printed out if we ran this modified insertionSort on the vector [ 9, 7, 10, 3, 1 ]? Since the for loop would execute 4 times, you should draw the vector 4 times. (Donʹt worry about the exact format of the output ‐‐ the point is to show how the contents of the vector is or is not changing in each loop iteration.) b. Consider the mergeSort code discussed in lecture and in the textbook. The vector changes as the function runs. Suppose that we added code to print out the vector immediately after the call to ʺmergeʺ. What would be printed out if we ran this modified mergeSort on the vector [ 9, 7, 10, 3, 1 ]? Make sure to show the vector as it would appear after EACH call to merge. (Donʹt worry about the exact format of the output ‐‐ the point is to show how the contents of the vector is or is not changing in each loop iteration.) Submit the answers to the above written questions in a file hw3written.xxx, where xx is an appropriate extension. OPTIONAL Extra Credit: Write a recursive function that takes as input an STL set S of integers, and a number i >= 0, and returns a list containing all subsets of S of size i (where each subset is represented as an STL set). If i is greater than the number of elements of S, return an empty list. You may also write other ʺhelperʺ functions. Donʹt forget that the empty set is a subset of S. For testing, also write a main function that reads the elements of the set S from an input file called inputec3.txt The first line of the input file should contain the number of integers in the set S. The elements of the set S should be given on subsequent lines of the file, separated by spaces. For example: 7 24 32 6 2 80 6 12 Hint: If the first element of set S is 3, then the subsets of S of size i are of two types: subsets of size i containing the element 3, and subsets of size i that do not contain the element 3. The subsets of the first type are made up of a subset of S of size i‐1 Submit your extra‐credit program in a file called hw3ec.cpp ...
View
Full
Document
This note was uploaded on 02/02/2011 for the course CS 2134 taught by Professor Hellerstein during the Fall '07 term at NYU Poly.
 Fall '07
 Hellerstein
 Algorithms, Data Structures, Recursion

Click to edit the document details