mid2010_soln

# mid2010_soln - University of Waterloo Midterm Examination...

This preview shows pages 1–5. Sign up to view the full content.

Term: Fall Year: 2010 Student Name: ______________________________________________________________________ UW Student ID Number: ______________________________________________________________ Course Abbreviation and Number GEN E 121 (Mechatronics C++) Course Title Digital Computation Section(s) 001 Instructor C. Hulls Date of Exam Monday, October 18, 2010 Time Period Start time: 9:00am End time: 11:00am Duration of Exam 2 hours Allowed Materials: Writing instruments ONE (8.5x11) formula sheet written on ONE side only Disallowed Materials No electronic devices of ANY kind. No hats or containers such a purses, pencil cases, packsacks Notes: You can assume for any programs that any necessary libraries (eg. #include <iostream> ) have been included. All questions will be marked for credit. If you need to make any assumptions to formulate your solution, state your assumptions clearly. Question Maximum Mark Mark 1. 8 2. 10 3. 17 4. 10 5. 20 6. 20 Total 85 University of Waterloo Midterm Examination

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Fall 2010 GenE 121 (C++) Midterm Page 2 of 10 1. Fill in the Blank [8 Marks Total] a. A variable must be ____defined/declared _____________ before it can be used in a program. b. The ___fmod ________ function returns the remainder from floating point division. c. The * , / , and % operators have the same _______precedence _________. d. The code if ( _______b==0 _______ ) tests whether the integer b is equal to zero. e. A function that does not return anything has a return type of _________void _________. f. For an array of integers called x , the code to set the first element to -1 is x[ _0 _ ] = -1; g. The acos function returns the ___________arccose/inverse cosine _________________ in _____radians _______________ (units).
Fall 2010 GenE 121 (C++) Midterm Page 3 of 10 2. Short Answer [10 Marks Total] What is output by the following code? a. [2 Marks] int x = 2, y = -4, z = 3; cout << 3.2 + x * z / y * 0.5 + x % z; 4.7 b. [1 Mark] int i = 4; int j = i++ / ++i; cout << i << " " << j << endl; 6 1 c. [3 Marks] int num[10] = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; int k = 4; cout << num[k++] << endl; ++num[k]; cout << num[k] << endl; 32 65 d. [4 Marks] int swirl(int & b, int c, int & a) { c += a % b; a = ++b; b += 2; return c; } int main() { int a=6, b=-3, c=4; cout << a << " " << b << " " << c << endl; b = swirl(a, c, c); cout << a << " " << b << " " << c << endl; cin.get(); return 0; } 6 -3 4 9 8 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Fall 2010 GenE 121 (C++) Midterm Page 4 of 10 3. Short Programs and Short Functions [17 Marks Total] a. [4 Marks] Write a program that prompts for an elapsed time in the following format h : m : s and outputs the time in seconds. For example, Enter elapsed time 1 : 5 : 21 Time is 3921 s int main() { cout<<”Enter elapsed time”; char junk; int hour, minute, second; cin>>hour>>junk>>minute>>junk>>second; cout<<”Time is “<<second+minute*60+hour+3600<<” s”<<endl; system("PAUSE"); return 0; } b. [4 Marks] Write a function that receives The number of litres in a car gas tank The fuel efficiency of the car in litres per 100 km The distance to travel in the car trip and returns the number of times you need to fill the tank up with gas.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern