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

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

View Full Document Right Arrow Icon
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 .
Background image of page 1

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 2
2. (6+8 points) int randomInRange( int lowerBound, int upperBound ) { int range = upperBound – lowerBound + 1; return rand() % range + lowerBound; } double randomInRange( double lowerBound, double upperBound ) { double range = upperBound - lowerBound; double randomDecimal = rand() / static_cast<double>(RAND_MAX);
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course DDDD h0322 taught by Professor Whoknow during the Spring '11 term at Bina Nusantara University.

Page1 / 10

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online