- StudyBlue
- Michigan
- University of Michigan - Ann Arbor
- Engineering
- Engineering 101
- Ringenberg
- F07 Sample Exam 3 Solutions
F07 Sample Exam 3 Solutions
Engineering 101 with Ringenberg at University of Michigan - Ann Arbor
About this note
By: Peter Nagel
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2009-02-25
File Size: 5 page(s)
Views: 33
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2009-02-25
File Size: 5 page(s)
Views: 33
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“Simply amazing. The flash cards are smooth, there are many different types of studying tools, and there is a great search engine. I praise you on the awesomeness.”
Dennis
Dennis
Sign up (free) to study this.
Exam #3 Engr 101: Intro to Algorithms and Programming Section 400 ? Fall 2007 ? You have 2 hours to complete this exam. ? You are allowed to use the books, your notes, and a calculator. ? You are not allowed to use any electronic devices that allow the passing of messages or data between others. ? You are not allowed to access the internet or use a computer. ? This exam contains three multi-part questions. ? Write your name on the top of each page of the exam. ? You are responsible for the legibility of your work. Any work that is not clear to the grader due to sloppiness or poor handwriting will be marked incorrect. Printed Name: _____________________________________ Lab Section: ______________________________________ Date: ____________________________________________ Sign the Honor Code: I have neither given nor received unauthorized aid on this examination, nor have I concealed any violations of the Honor Code. Signature: __________________________ Q1 Q2 Q3 Total __ / 30 __/ 35 __ / 35 __ / 100 Name: ____________________________________________ Question 1 ? What does the program do? (30 points) Given the following program, write in the space provided what is output to the terminal along with what is contained inside the output file ?out.txt? for the following two inputs from the user. The inputs are read into main() from the getline() call. #include #include #include using namespace std; string yang; void crazyCipher(string & output, ofstream & in) { if (output.size() % 2) output.push_back('x'); for (int i=output.size()-2; i >= 0; i = i-2) { if (output.at(i) == output.at(i+1)) yang.push_back('X'); else { cout << output.substr(i, 2) << endl; yang.append(output.substr(i, 2)); } } in << "---" << output; in << "---" << yang; in << "---" << endl; return; } int main() { string ying; ofstream output("out.txt"); cout << ?Enter something: ?; getline(cin, ying); crazyCipher(ying, output); output.close(); return 0; } User Input #2: silly rabbit Contents of ?out.txt? #2 ---silly rabbit---itXray Xsi--- Terminal Output #2 it ra y si Contents of ?out.txt? #1 ---willow runnerx---rxneun rowXwi--- Terminal Output #1 rx ne un r ow wi User Input #1: willow runner Name: ____________________________________________ Question 2 ? What does the program do (35 points)? Given the following program segment and user input, write the contents of the vector ?tableau? and also the contents of the file ?output.txt? after the program is run in the space provided below. Also in the space provided, write what will be output to the terminal while the program is run. Assume that all #includes and ?using namespace std;? have been declared already. int getIt(int & x, char z, vector > & in) { x = -1; for (int i = 0; i < in.size(); i++) { for (int j = 0; j < in.at(i).size(); j++) { if (in.at(i).at(j) == z) { x = j; in.at(i).at(j) = 'q'; return i; } } } return -1; } int main() { vector > tableau(5); string phrase; string alphas = "thelemers"; int lolz; ofstream outStuff("output.txt"); getline(cin, phrase); for (int i = 0; i < tableau.size(); i++) { for (int j = 0; j < 5; j++) { tableau.at(i).push_back(phrase.at(i*5+j)); } } for (int i = tableau.size() - 1; i >= 0; i--) { for (int j = 4; j >= 0; j--) { outStuff << tableau.at(i).at(j); } outStuff << endl; } for (int i = 0; i < alphas.size(); i++) { cout << getIt(lolz, alphas.at(i), tableau); cout << " " << lolz << endl; } outStuff.close(); return 0; } Terminal Output 0 0 -1 -1 2 3 -1 -1 3 1 -1 -1 -1 -1 0 4 1 3 Contents of ?output.txt? y a n i deg revid sdao r owt Contents of ?tableau? vector qwo q oadq divqr gqd i n a y User Input: two roads diverged in a yellow wood Name: ____________________________________________ Question 3 ? Write some functions (35 points) For this question, write a function called ?generateTable? in the space provided that takes as input a reference to a file stream and outputs a vector of vectors of integers. The structure of the function is as follows: 1. Read in a line from the file. 2. Count the number of letters for each word in the line. 3. Output these numbers into the corresponding rows of the output vector. 4. Repeat for each line in the file. 5. Return the vector of vectors of letter counts for each word. After writing this function, write another function called ?findMaxAvg? with the following structure: 1. Take as an input a vector of vectors of integers like what was output from the generateTable function. 2. Calculate the average of each row of integers in the vector. 3. Output the max of these row averages as a double. Note: You can assume that the input file will consist of at least two lines and that there will be no blank lines. Also, the input file will consist only of alphabet characters, whitespace, and newlines. See below for an example of how these would be used: int main () { vector > table; ifstream inputFile("in.txt"); table = generateTable(inputFile); cout << findMaxAvg(table) << endl; } }} ?in.txt? Averages (per row) 4 3 4 2.75 4 2 5 4 3 1 3 5 4 3 5 3 4 1 3 2 7 3 13 ?table? vector There once was a dog named spot who could not find a lot so settled for a dot findMaxAvg function See next page generateTable function See next page max average 4 Name: ____________________________________________ generateTable function vector > generateTable(ifstream & in) { vector row; vector > table; string inString; int count, i; getline(in, inString); while (!in.fail()) { row.clear(); cout << inString << endl; for (i = 0, count = 0; i < inString.size(); i++) { if (inString.at(i) == ' ') { if (count !=0) row.push_back(count); count = 0; } else count++; } if (count !=0) row.push_back(count); table.push_back(row); getline(in, inString); } return table; } findMaxAvg function double findMaxAvg(vector > table) { double currentMaxAvg = 0; double colCount, rowSum; for (int i=0; i < table.size(); i++) { rowSum = colCount = 0; for(int j=0; j
Back
Next
About this note
By: Peter Nagel
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2009-02-25
File Size: 5 page(s)
Views: 33
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2009-02-25
File Size: 5 page(s)
Views: 33
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“Simply amazing. The flash cards are smooth, there are many different types of studying tools, and there is a great search engine. I praise you on the awesomeness.”
Dennis
Dennis