









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










This is a closed-book exam. There are 5 problems on 15 pages. Read the entire exam through before you begin working. Work on those problems you find easiest first. Read each question carefully, and note all that is required of you. Keep your an- swers clear and concise, and state all of your assumptions carefully. Each question has a box for your answer; put your answers in the boxes provided. Write your uniqname on the line provided at the top of each page. You are to abide by the University of Michigan/Engineering honor code. Please sign below to signify that you have kept the honor code pledge. Honor code pledge : I have neither given nor received aid on this exam, nor have I concealed any violations of the Honor Code.
In the following three parts to this question, you are given a specification of a function. For each function, you must supply the requested number of simple test cases and the requested number of boundary- condition test cases. For each test case, you must provide: a description of the test case, the expected behavior for a correct implementation of the function, and why the case is a boundary case (if applicable). Give English descrip- tions of your test cases and provide sufficient detail so that your test can be repeated fully. For full credit, your cases should collectively provide maximum coverage of behavior. Here is an example: int gcd(int x, int y); // EFFECTS: returns the greatest common divisor of x,y Supply one simple test case for the gcd function:
list_t reverse(list_t list); // EFFECTS: returns the reverse of list Supply one simple test and one boundary case for the reverse function. int tree_sum(tree_t tree); // EFFECTS: returns the sum of all elements in the tree, // zero if the tree is empty Supply one simple test case and one boundary case for the tree_sum function
const int MAX_DAYS = 100; struct daily_temperature_t { unsigned int day; // day in the month int high; // high temperature for the day int low; // low temperature for the day }; struct weather_t { daily_temperature_t temps[MAX_DAYS]; double average_high; double average_low; }; void average_highs_lows(monthly_weather_t &w, int num_days); // REQUIRES: num_days > 0 // MODIFIES: w // EFFECTS: determines the average of high and low temperatures // over the first ‘num_days’ days // and puts them in w Supply one simple test case and one boundary condition for the average_highs_lows function:
Consider the following function specification bool repeat_char(char *s, int n); // REQUIRES: s is a C-string of at least n non-NUL characters // n > 0 // EFFECTS: returns true if the first n characters are // fully repeated throughout the string s, // false otherwise. // // EXAMPLES: repeat_char(“123123”, 3) - > true // repeat_char(“abcdeabcdeabcde”, 5) - > true // repeat_char(“123123qu2”, 3) - > false // repeat_char(“abcdeabcdeabc”, 5) - > false Provide an implementation of this function on the next page. You need not copy the RME clause, but should copy the function prototype. You must adhere to the following:
Recall insert_tree() from Project 2, where we created a new tree from an existing one, adding a new leaf along the way. The following question asks you to write a function to create a new tree from an existing one, modifying elements that meet a certain condition, and return the resulting tree. tree_t transform(tree_t t, bool (cond)(int), int(xform)(int)); // EFFECTS: Returns a new tree in which all elements that meet // the condition “cond” are transformed by the function // “xform”. For example, if transform() were called on the tree 0 /
3 5 / \ /
1 2 1 8 with cond() being a function that returns true if its argument is less than 4, and xform() be- ing a function that returns the argument times 2, then transform() should return the tree 0 /
6 5 / \ /
2 4 2 8 Your function must be recursive, but need not be tail-recursive. You may not use any helper functions. You have the following tree functions available to you. You may not use any others. bool tree_isEmpty(tree_t tree); tree_t tree_make(); tree_t tree_make(int elt, tree_t left, tree_t right); int tree_elt(tree_t tree); tree_t tree_left(tree_t tree); tree_t tree_right(tree_t tree);
Consider the following function specification bool no_uniq(list_t l) // EFFECTS: returns true if l has no elements that appear // only once in l; returns false otherwise. // // EXAMPLES: // (1, 5, 1, 5, 1) - > true. 5 appears twice, and // 1 appears three times. // // (1, 5, 1, 1) - > false. 5 appears only once. // // () - > true no singletons Provide a tail-recursive implementation of this function. Note that the iterative solution to a sim- ilar problem over arrays would have nested loops. So, your solution will probably require two helper functions: one for the “outer loop” and one for the “inner loop”. Also, notice that an ele- ment is not unique if an equivalent element is either before or after it in the overall list. So, your “outer loop” might need to keep track of two lists—a prefix and a suffix. For reference, here are the functions you may use without writing yourself. You may not assume that any other functions are available to you. bool list_isEmpty(list_t list); list_t list_make(); list_t list_make(int elt, list_t list); int list_first(list_t list); list_t list_rest(list_t list);