First, copy the lab materials to your account.
cp -r /home/Classes/Cs1/Labs/Lab06 .Everything you need for the lab exercises today is contained in this new directory.
Change directory to Lab06.
Keep paper and pens or pencils handy.
Run the program once more, entering 7. Notice what is happening. For this input, the program loops infinitely. Why?
We can make sure the program terminates
by stopping the loop when the sum equals or exceeds one.
Make this change to the program.
1
Show us your corrected program, and explain the problem with the original.
***** ***** *****Run this program and see what happens. Obviously, you need to correct it. (Hint: there are two things you will need to correct.) To help see what is wrong, hand trace the code, using small sample values for the numbers of rows and columns, such as 1 and 2.
2
Show us your corrected program. Be sure that you can explain
everything that was wrong with the original.
Modify the values assigned to r, g, and b so that the circle you draw will have non-gray colors. We want you to use certain constraints while assigning the values. On each iteration of the loop:
3
Show us your revised code, and how it runs.
Fix this program so that it works correctly.
4Show us your corrected program,
and explain the problem with the original.
Change this code to use a do-while loop having the following pseudocode structure:
do prompt user for a number n if n is not in range 1 to 10, print error message while n is not in range 1 to 10In the original version, there were two input statements but only one test. In this version, there is only one input statement, but there are two tests.
Make sure your do-while version behaves the same as the original version.
5
Show us your modified code.
Finally, convert this program into a loop-and-a-half version that has only one input statement and one test but that behaves the same as the original version.
6
Show us your modified code.
* * *Modify the program so that the output is
* ********** *There are 10 asterisks in the middle row.
Your modifications must obey the following constraints:
7
Show us your completed program.
Consider the unit circle which, in cartesian coordinates, is defined by the equation x*x + y*y = 1. The area of the part of the unit circle that lies in the first quadrant is pi/4, where pi is a well-known constant.
Imagine that you throw darts at random at the unit square (in the first quadrant) so that the darts land randomly on (x,y) coordinates satisfying 0 <= x <= 1 and 0 <= y <= 1. Some of these darts will land inside the unit circle. Since the first quadrant part of the unit circle has area pi/4 and the unit square has area 1, the proportion of darts you throw that land in the unit circle will be approximately pi/4.
Go into the subdirectory EstimatePi and edit the Demo class. Examine this program and observe that it asks the user to enter the number of darts to throw. Since Math.random() produces a double value in the range of 0 to 1, we can use this to generate random (x,y) values representing points in the unit square.
The body of the for loop generates these points and accumulates the number of them that land in the unit circle in the variable inside. When the for loop terminates, the value of inside will be the total number of darts that landed inside the unit circle. The quotient obtained by dividing this value by the total number of darts thrown should approximate the value of pi/4.
The program then outputs this quotient, times 4 (to make it an approximation to pi). Of course, if you throw just a few darts, the approximation will be rather poor. The more darts you throw, the better an approximation you will get.
The program is missing only the test in the body of the for loop that determines whether the dart with coordinates (x, y) lies inside the unit circle.
You are to complete the program and test it with several choices for numbers of darts. At least one of your choices should be 1000000.
For your information, this sort of method for approximating a value is called Monte Carlo, in honor of the famous casino in Monaco, where random numbers are generated for entertainment.
8
Show us your completed program and the results of your tests.