- StudyBlue
- Michigan
- University of Michigan - Ann Arbor
- Engineering
- Engineering 101
- Ringenberg
- Practice Exam 1 W/o Answers
Practice Exam 1 W/o Answers
Engineering 101 with Ringenberg at University of Michigan - Ann Arbor
About this note
By: Muneet Parhar
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2009-03-07
File Size: 7 page(s)
Views: 144
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2009-03-07
File Size: 7 page(s)
Views: 144
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.
“I have used this website for three exams, and I see a huge difference in my test results.”
Naj
Naj
Sign up (free) to study this.
Page 1 of 7 Exam 1 Fall 2008 Semester Engr101: Introduction to Algorithms and Programming Section 300 ? You are allowed to use the books, your notes, and a calculator. ? You are not allowed to use devices that allow the passing of information between others. ? You are not allowed to access the internet or use a computer. ? You are not allowed to use any device that can interpret, compile, or run program code in any way. ? This exam contains four multi?part questions. ? Write your uniqname on the top of each page of the exam (excluding the first page). ? 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. ? You must read and sign the Honor Code below. Printed Uniqname: ___________________________ Printed Name: ____________________________ Lab Section: _____________________________ Number I II III IV Total Grade I have neither given nor received aid on this examination, nor have I concealed a violation of the Honor Code. Signature:_________________ Uniqname: ____________________________ Page 2 of 7 Question 1 ? Find the Errors (20 points) The program on the next page is supposed to prompt the user for two inputs and in the process output a lot of ?useful? information. It is also supposed to open a file, write a value to it, close it, re?open it for reading, read the value that was written to it, and finally close it. Unfortunately, there are 7 syntax errors in the program. Use the Linux compiler output seen below to identify the 7 errors. Once you have found the errors, circle them in the program and write a number next to each. Then in the numbered spaces below, briefly explain how to fix each of your numbered errors. Compiler Output silly.cpp: In function 'void myDiv(double&, int&)': silly.cpp:8: error: expected `;' before 'cout' silly.cpp:9: error: return-statement with a value, in function returning 'void' silly.cpp: In function 'int main()': silly.cpp:27: error: expected `;' before 'result' silly.cpp:28: error: 'result' was not declared in this scope silly.cpp:34: error: statement cannot resolve address of overloaded function silly.cpp:37: error: no match for 'operator<<' in 'myIfile << dval' silly.cpp:41: error: return-statement with no value, in function returning 'int' Error 1: _______________________________________________________________ Error 2: _______________________________________________________________ Error 3: _______________________________________________________________ Error 4: _______________________________________________________________ Error 5: _______________________________________________________________ Error 6: _______________________________________________________________ Error 7: _______________________________________________________________ Uniqname: ____________________________ Page 3 of 7 Remember: circle the errors in the program and write a number next to each. Then in the numbered spaces on the previous page, briefly explain how to fix each of your numbered errors. silly.cpp 1 #include 2 #include 3 4 using namespace std; 5 6 void myDiv(double & in1, int & in2) { 7 in1 = in1 / in2 8 cout << "Functions Rock! "; 9 return in1; 10 } 11 12 13 int main() { 14 int ival; 15 double dval; 16 ifstream myIfile; 17 ofstream myOfile("Zfile.txt"); 18 19 cout << "Time for some fun!\n"; 20 21 cout << "Enter integer: "; 22 cin >> ival; 23 cout << "Enter double: "; 24 cin >> dval; 25 26 cout << "ZOMG look at this: " 27 result = dval; 28 myDiv(result, ival); 29 cout << result << endl; 30 31 cout << "LOL, files is better! "; 32 myOfile << ival / dval << endl; 33 34 myOfile.close; 35 myIfile.open("Zfile.txt"); 36 37 myIfile << dval; 38 cout << "See: " << dval << endl; 39 myIfile.close(); 40 41 return; 42 } Uniqname: ____________________________ Page 4 of 7 Question 2 ? What are the outputs? (25 points) In the box provided below each of the small programs, write only one of the following in order to indicate how the program performs: ? The phrase ?Does not compile? indicating the program never compiles. ? The phrase ?Infinite loop? indicating the program enters an infinite loop and never finishes. ? The screen output of the program using the underscore to denote any spaces (i.e. 26_12_7_). Assume that #include and using namespace std; are included at the beginning of each program. Answer: int main() { int foo = 5; bool test = false; while ((foo == 5) and test) { foo--; cout << foo << " "; } cout << foo; return 0; } Answer: void test(int & in) { while ((in % 3) == 1) { cout << in << " "; in = in - 1; } return; } int main() { int goo = 9; while (goo>0 and goo<10) { test(goo); goo = goo - 1; } return 0; } Answer: int main() { int moo = 5; while (moo != 0) { moo = moo - 1; cout << moo << " "; moo = moo + 1; } return 0; } Answer: void rest(int & in1, int in2) { while(in1 <= 9) { cout << in2 << in1 << " "; in1 = in1 + 3; } return; } int main() { int too=0, zoo; while(too < 2) { zoo = 3; rest(zoo, too); too = too + 1; } return 0; } Answer: int main() { int doo=0, roo; while(roo >= 0) { cout << doo << roo; roo--; doo++; return 0; } Uniqname: ____________________________ Page 5 of 7 Question 3 ? What is the output? (30 points) Analyze the program below. In the boxes provided, write what the program outputs to the screen and also the contents of the file ?out.txt? after the program is run. The contents of the input file, ?in.txt?, are provided below. Denote any spaces by an underscore (for example, 23_21). #include #include using namespace std; void res(double in1, int in2, ofstream & out) { while ((in2 % 2) == 0) { out << in2 / in1 << endl; return; } out << in1 / in2 << endl; cout << "F:" << in2 / in1 << endl; return; } int main() { int x = 6, y; double z; ifstream infile("in.txt"); ofstream outfile("out.txt"); while (x > 0) { infile >> y >> z; res(z, y, outfile); x = x - 1; } outfile.close(); infile.close(); infile.open("out.txt"); // re-opening a // file starts // reading it from // the beginning while (x < 6) { infile >> z; cout << "X:" << z << endl; x = x + 1; } return 0; } 10 2.5 9 90.0 12 6.0 2 0.25 3 12.0 13 130.0 8 2.0 9 4.5 1 1.0 3 3.0 Screen Output Contents of ?out.txt? Contents of ?in.txt? Uniqname: ____________________________ Page 6 of 7 Question 4 ? Write some code (25 points) For this question, you will be completing a program that calculates how far in the vertical direction (after traveling a given distance in the horizontal direction) a beam of light refracts after it crosses into, and then travels inside of, a new medium. To do this, you must write two functions, one procedure, and fill in the ten blanks in the main() function on the next page. The following diagram and equations describe the situation where the light is emitted in the first medium (air) at point A, crosses into the new medium (water) at point B, and then finishes its traversal in the new medium at point C: After it is completed, the program will do the following: 1. Prompt the user to input a value for x in centimeters and then input x as a double. 2. Prompt the user to input a value for y1 in centimeters and then input y1 as a double. 3. Use the functions and procedure (detailed below) to calculate y2 given the above inputs and equations. 4. Output y2 to the screen. The first function, calc1, takes two input arguments as doubles where the first input is x and the second input is y1. It then computes i as a double using one of the above equations and returns i. The procedure, calc2, has two arguments represented as doubles. The first argument, i, is the calculated value output by calc1. The procedure uses i and one of the above equations to calculate, and then write to, the value of the second argument, R. The second function, calc3, takes two input arguments as doubles where the first input is the calculated value written by calc2, R, and the second input is x. It then computes y2 as a double using one of the above equations and returns y2. Note: Make sure to write the proper function definitions with the correct names and correct number of inputs in the correct order on the next page. C A B i R y1 x x y2 Air Water Equations: i = arctan(x / y1) R = arcsin(sin(i)*0.7504) y2 = x / tan(R) Notes: ? The cmath function sin(w) returns the value of the sine of w. ? The cmath function atan(x) returns the value of the arctangent of x. ? The cmath function asin(y) returns the value of the arcsine of y. ? The cmath function tan(z) returns the value of the tangent of z. Uniqname: ____________________________ Page 7 of 7 Procedure ? calc2( ) Function ? calc1( ) Function ? calc3( ) #include #include using namespace std; // calc1 definition would go here // calc2 definition would go here // calc3 definition would go here int main() { double x,y1,i,R,y2; cout << "Enter x (in cm): "; cin >> ___________; cout << "Enter y1 (in cm): "; cin >> ___________; _____ = calc1(_____, _____); calc2(_____, _____); _____ = calc3(_____, _____); cout << "y2 = " << y2 << endl; return 0; } Administrator Microsoft Word - Exam 1.docx
Back
Next
About this note
By: Muneet Parhar
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2009-03-07
File Size: 7 page(s)
Views: 144
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2009-03-07
File Size: 7 page(s)
Views: 144
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.
“I have used this website for three exams, and I see a huge difference in my test results.”
Naj
Naj