{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# assn3_soln - MIT OpenCourseWare http/ocw.mit.edu 6.096...

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

MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .

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

View Full Document
Massachusetts Institute of Technology 6.096 Lab 3 Solutions Part 1: 1. (4 x 2 points) a. A function prototype for square should be added before main (or the whole function should be moved earlier). b. Either number should be added as a parameter to square, or number should be made a global variable and removed from the function call to square. The former method is preferable, since the latter entails creating global variables unnecessarily. c. Change the square prototype and definition to take an int&. d. sumOfPositives should return sum. Also, the call to it has too many arguments, which can be corrected either by adding an argument to the prototype and definition or by deleting one of the arguments. 2. (3 x 2 points) a. It prints "This is not the hello message!" . b. It prints the numbers from 0 to 9 (the print statements that actually print these numbers alternate between main and the function). c. It prints the numbers from 0 to 10, repeating each of the numbers in between twice. There are two print statements: the one in mystery gets executed for a given number, and then, since the parameter was passed by reference, the original counter is not updated by mystery, but only by the for loop, so the same number is printed again by the cout statement in main. Part 2: 1. (15 points) double calculatePi( int numDarts ) { int dartsInCircle; for( int i = 0; i < numDarts; i++ ) { double xVal = rand() / static_cast<double>(RAND_MAX), yVal = rand() / static_cast<double>(RAND_MAX); if( sqrt(xVal * xVal + yVal * yVal) <= 1 ) dartsInCircle++; } // pi = circle area / r^2, but r == 1 return static_cast<double>(dartsInCircle) / numDarts; }
2. (6+8 points)

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

View Full Document
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