- StudyBlue
- Michigan
- University of Michigan - Ann Arbor
- Engineering
- Engineering 101
- Ringenberg
- Sample exam 3b - F06 answers

Manas T.

Advertisement

3.1 What Does the Program Do? [40 points] Examine the program below and determine what it wil print out to the scren when executed. #include #include using namespace std; string translate(const string & x){ string y; bool newword = true; int j = 0; char first; for(int i = 0; i < x.size(); i++){ if(isalpha(x.at(i))){ if( newword ){ first = x.at(i); newword = false; } else y = y + x.at(i); } else { if( newword ) y = y + x.at(i); else { y = y+ first+ ?ub?+ x.at(i); newword = true; } } } if( not newword) y = y + first + ?ub?; return y; } int main(){ string message(?we love linux?); cout << translate(message) << endl; } NAME ANSWER KEY Output: ewub ovelub inuxlub 3.2 Find the erors. [40 points] The folowing procedure is meant to sort a list of integers. It contains two erors. The first eror can be solved by minor changes on two lines of the program. The second eror can be solved by somewhat more extensive alterations. Identify the two erors, describe what problem they wil cause, and alter the code to fix the problem. Note that you wil only receive ful credit if you make the minimal changes necessary to fix the two erors. Rewriting the entire procedure wil NOT be considered a valid answer to this question. void sort(vector & list){ int i, j, tmp; for(i = 0; i < list.size() - 1; i++) for(j = i + 1; j < list.size(); j++) if(list.at(i) > list.at(j)){ tmp = list.at(i); list.at(i) = list.at(j); list.at(j) = tmp list.at(i); } } NAME ANSWER KEY Eror The sort algorithm counts over pairs apropriate for a list of size list.size()+1. This is because the lops are set up incorectly. Problem Caused The algorithm wil atempt to access data items outside the bounds of the list. Solution (also mark your changes on the code to the left) Change <= to < in both for lops. Eror The lines in the if statement that are suposed to cause the list elements to swap wil not have the desired efect. Problem Caused Data wil be lost as the data is overwriten by elements of lower value. Solution (also mark your changes on the code to the left) Ad a temporary variable that wil hold the data temporarily to facilitate the swap. 3.3 Write a predicate. [2 points] Write a predicate that determines if a given basis set is orthogonal. The basis set is a vector of vectors of integers. Every element of the basis set is a vector of the same length. Two vectors of integers (a 1 , a 2 , a 3 , ?., a N ) and (b 1 , b 2 , b 3 ,?., b N ) are orthogonal if 1 0 N i i b = ! . The basis set is said to be orthogonal if the above condition holds for every posible pair of vectors in the basis set. For example the basis set might be the list of length 4 vectors: (1, 0, 0, 0), (0, 2, 0, 0), (0, 0, 3, 0), (0, 0, 0, -1) In this case the above condition holds for any two vectors in the basis set , so the basis set is orthogonal and the predicate should return true. If the basis set were: (1, 1, 1), (-1, 0, 1), (0, 1, 0) This would not be orthogonal because the condition does not hold for the first and last vectors. So the predicate would return false. You may assume that the above sum is already defined using the function given below. Please use the predicate header given to the right. int Sum( const vector & a, const vector & b){ if(a.size() != b.size()) return 0; else{ int d=0; for(int i=0; i > & basis){ int i, j; for(i=0; i < basis.size()-1; i++){ for(j=i+1; j < basis.size(); j++){ if(Sum(basis.at(i), basis.at(j))!=0) return false; } } return true; } ALTERNATE ANSWER bool orthogonal( const vector > & basis){ int i=0, j; bool ans=true; while (ans and i <= basis.size()-2){ j=i+1; while (ans and j<=basis.size()-1){ if(Sum(basis.at(i), basis.at(j))!=0) ans = false; j++; } } return ans; } Michael Falk Microsoft Word - SampleExam3b-solutions.doc

Advertisement

Want to see the other 3 page(s) in Sample exam 3b - F06 answers?
JOIN TODAY FOR FREE!

"The semester I found StudyBlue, I went from a 2.8 to a 3.8, and graduated with honors!"

Jennifer Colorado School of Mines
StudyBlue is not sponsored or endorsed by any college, university, or instructor.

© 2014 StudyBlue Inc. All rights reserved.

© 2014 StudyBlue Inc. All rights reserved.