Docsity

5 Solved Problems on Progress and Data Structures - Midterm Exam | EECS 280, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS280'Fall'2010'Midterm'Exam'Page'1'
!
EECS$280:!!Midterm!Exam!
Fall!2010!
!
!
• 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!answers!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!sheet!of!paper.!
!
• 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.!
! !
!Signature:!_________________________________________!
! !
!Name:!_________________________________________!
!
!Uniqname:! ____________________________________________________!
! !
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download 5 Solved Problems on Progress and Data Structures - Midterm Exam | EECS 280 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

EECS 280: Midterm Exam

Fall 2010

  • 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 answers 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 sheet of paper.
  • 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.

Signature: _________________________________________

Name: _________________________________________

Uniqname: ____________________________________________________

Problem 1 -­‐ Correctness

In the following problems, you are given:

  • An English description of a function (or functions)
  • A potentially erroneous implementation of that function (or functions)

For each implementation, state whether or not it is correct in provided box. If it is not correct, list the specific

bugs (there may be more than one). You do not need to correct the bugs; only list them!

1a) The function alphaTally() counts the number of times each lowercase letter in the alphabet appears in an

input C-­‐string and stores the results into an array where each element in the array corresponds to a letter in the

alphabet.

void alphaTally(int occs[], char * str) { // REQUIRES: occs is an array of 26 integers where each is initialized to zero // str is a non-empty C-string composed exclusively of lowercase letters // EFFECTS: counts the number of times each lowercase letter in the alphabet // appears in str and stores the counts in the occs array where, for i = 1..26, // index (i-1) in the array corresponds to the ith^ letter in the alphabet. For // example, occs[0] corresponds to 'a', occs[1] to 'b', ..., and occs[25] to 'z'. char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; char * current = str; for (int i = 0; i < 26; i++) { while (*current) { if (alphabet[i] == *current) occs[i]++; current++; } current = str; } return; }

Uniqname:________________________________________________________________________________

1b) The function isReversed() checks if one string is contained in another string, but in reverse order.

bool isReversed(char s1, char s2){ // REQUIRES: s1 and s2 are non-empty C-strings. // EFFECTS: returns true if s2 is composed of the string in s // except in reverse order, false otherwise. char t = s2; while (t) { t++; } while(s1 && (t!=s2)) { if(s1=*t){ s1++; t-1; } else return false; } return true; }

Uniqname:________________________________________________________________________________

1d) The function foo() calls the function addition() where addition() adds its inputs together and

returns a pointer to the result which can be used in foo() to print out the result of the addition.

int * addition(int a, int b) { // EFFECTS: Calculates a + b and returns a pointer to the result int result = a + b; return &result; } int foo() { int * result1 = addition(13, 37); int * result2 = addition(73, 31); cout << *result1 << " " << *result2 << endl; return 0; }

Problem 2 -­‐ Tail Recursion, Lists, and Function Pointers

Given the subset of list functions from project 2 below where all lists are composed of zero or more integers,

implement the following function tail-­‐recursively in the box provided on the next page:

list_t modItUp(list_t in_list, int (fn1)(int), int (fn2)(int)) { // EFFECTS: If in_list is empty, returns an empty list. // Otherwise returns a list, out_list, where: // out_list[i] = fn1(in_list[i]) when i % 2 = 0 // out_list[i] = fn2(in_list[i]) when i % 2 = 1 // // For example, if fn1 returned its input squared and fn2 returned its // input multiplied by 2, then: // // modItUp((1, 2, 3, 4, - 5), fn1, fn2) yields the list (1, 4, 9, 8, 25) }

Available list methods (you may assume that all use constant space with respect to their inputs):

bool list_isEmpty(list_t list); // EFFECTS: returns true if list is empty, false otherwise list_t list_make(); // EFFECTS: returns an empty list. list_t list_make(int elt, list_t list); // EFFECTS: given the list (list) make a new list consisting of // the new element followed by the elements of the original list. int list_first(list_t list); // REQUIRES: list is not empty // EFFECTS: returns the first element of list list_t list_rest(list_t list); // REQUIRES: list is not empty // EFFECTS: returns the list containing all but the first element // of list list_t reverse(list_t list); // EFFECTS: returns the reverse of list list_t rotate(list_t list, unsigned int n); // EFFECTS: returns a list equal to the original list with the // first element moved to the end of the list n times.

Notes :

  • The definitions of any other list or helper functions that you use must be included in our solution.
  • Your solution must be tail-­‐recursive and may not use iteration in any way.
  • You do not need to copy over the RME clauses, but you should copy over the function's type-­‐signature.

Problem 3 -­‐ Recursion and Trees

Given the subset of tree functions from project 2 where all trees are composed of zero or more integers and also

the definition of a perfectly balanced tree below, implement the following function recursively in the box

provided on the next page:

bool tree_isBalanced(tree_t tree); // EFFECTS: returns true if tree is balanced, false otherwise

Available tree methods:

bool tree_isEmpty(tree_t tree); // EFFECTS: returns true if tree is empty, false otherwise tree_t tree_make(); // EFFECTS: creates an empty tree. tree_t tree_make(int elt, tree_t left, tree_t right); // EFFECTS: creates a new tree, with elt as it's element, left as // its left subtree, and right as its right subtree int tree_elt(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the element at the top of tree. tree_t tree_left(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the left subtree of tree tree_t tree_right(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the right subtree of tree

A tree is perfectly balanced if it's right subtree and left subtree are the same height for every node in the tree.

The height of a tree is defined as the number of nodes that exist in a path from the root of the tree to the

deepest node in the tree. A tree with only one node has a height of one and an empty tree has a height of zero.

Therefore, empty trees are trivially considered perfectly balanced. The following trees are perfectly balanced:

4 / \

/ \ Height = 2 4 8 Height = 3 2 5 / \ /
3 5 7 9

These trees are not perfectly balanced:

4 / \

/ Height = 2 5 8 Height = 3 2 / /
4 7 9

Notes :

  • The definitions of any other list or helper functions that you use must be included in our solution.
  • Your solution must be recursive and may not use iteration in any way.
  • You do not need to copy over the RME clauses, but you should copy over the function's type-­‐signature.
  • Consider writing a helper function to calculate the height of a tree.

Uniqname:________________________________________________________________________________

Uniqname:________________________________________________________________________________

Problem 5 -­‐ Structs and Exceptions

For this problem, you must write a function that can enter the results of past and upcoming Olympics into a

global array of Olympic structs. The data structures have already been defined below and all that remains is for

you to write a function to enter the results.

const int MAX_EVENTS = 100; struct Athlete { // this struct stores information about a specific athlete char name[50]; // the athlete's name stored as a C-string char country[50]; // the athlete's country stored as a C-string int score; // the athlete's score } struct Event { // this struct stores information about a specific event char name[50]; // the name of this event stored as a C-string Athlete medalist[3]; // the list athletes who won medals for this event where: // medalist[0] = gold medal, medalist[1] = silver medal, // medalist[2] = bronze medal }; struct Olympics { // this struct stores information about a specific Olympics Event events[MAX_EVENTS]; // the events in this Olympics int year; // the year of this Olympics int numEvents; // number of Olympic events in this Olympics } Olympics history[ 30 ]; // global array that can hold results for 30 Olympics

You must write the following function using iteration in the box on the next page.

void addResult(int year, char * eventName, char * medal, Athlete winner); // REQUIRES: "winner" is a properly formatted and initialized Athlete struct // MODIFIES: global "history" array // EFFECTS: If no Olympic struct exists in the history array whose year element // is equal to the input "year", throws a - 1. For the Olympic struct in the // history array (if it exists) whose year element is equal to "year", if no // Event struct exists in the Olympic's events array whose name element is // equal to the input "eventName", throws a - 2. If the input "medal" is not // one of the strings "gold", "silver", or "bronze", throws a - 3. If none of // the first three inputs causes an exception to be thrown, this function // copies "winner" into the appropriate medalist array location, depending on // "medal", for the corresponding Olympic year and event as described above.

You may use only the following two library functions and any that you write yourself:

void strcpy(char *dest, const char *src) // REQUIRES: src is a NULL-terminated string, dest can hold a copy of src // MODIFIES: dest // EFFECTS: place a copy of src in dest bool strcmp(const char *dest, const char *src) // REQUIRES: src and dest are NULL-terminated strings // EFFECTS: returns FALSE if dest is equal to src, TRUE otherwise

Notes :

  • Your solution must be iterative.
  • You do not need to copy over the RME clauses, but you should copy over the function's type-­‐signature.
  • Assume that each Olympics has a unique year and each event has a unique name per Olympics.