Question

USE C++ Problem 3: Prompt the user to enter a whole number of points to be...

USE C++ Problem 3: Prompt the user to enter a whole number of points to be used in the approximation and robustly confirm that the number of points is positive (remember always ask user to re-enter before any calculations are performed). Then calculate and output the approximation for pi. Run your program at least twice with the entered number of points varying by at least a thousand. Examine your results and add a comment to the end of your source code file stating the effect of using more points on the approximation of pi. The maximum random number in C++ is stored into a predefined constant called RAND_MAX.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
C++ CODE

#include<iostream>

#include<ctime>

#include<cstdlib>

#define seed 35791246

using namespace std;

double estimatePi(long numIter);

double Xpoint;

double Ypoint;

double Zpoint = 0;

int points = 0;

int main()

{

long numIter;

double estimatedPi;

cout<<"Input the number of points: ";

cin>>numIter;

if(numIter < 0)

{

cout<< "Number of points must be a positive integer."<<endl;

return -1;

}

srand(seed); // seeding random number generator

// calling estimatePi function

estimatedPi = estimatePi(numIter);

cout<<"Estimated pi ~= "<< estimatedPi<<endl;

  

}

double estimatePi(long numIter)

{

int i = 0;

double estimatedPi;

while(i < numIter )

{

Xpoint = (double)rand()/RAND_MAX;

Ypoint = (double)rand()/RAND_MAX;

  

Zpoint = (Xpoint * Xpoint) + (Ypoint * Ypoint) ;

if (Zpoint <= 1)

{

points++;

}

i++;

}

estimatedPi = (double)points / numIter * 4 ;

return estimatedPi;

}

OUTPUT

E:\Program Files\Cplusplus workspace\Estimate_pi.exe Input the number of points 1000 stimated pi 3.196 rocess exited after 4.

Add a comment
Know the answer?
Add Answer to:
USE C++ Problem 3: Prompt the user to enter a whole number of points to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program which asks the user to enter a number. While the number is outside...

    Write a program which asks the user to enter a number. While the number is outside the range of 50 to 65 (exclusive). print "This is not valid input." and ask the user to re-enter a number. Remember to include a comment at the start of the program. 7 A- B I - : F F 2

  • Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should...

    Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.ser to input a number, and then using a loop control statement (While, For, Do-While), output to the console the numbers beginning from 1, up to and including the number input by the user.

  • C++ Program Write a do-while loop that continues to prompt a user to enter a number...

    C++ Program Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 #include <iostream> using namespace std; int main() { int userInput; /* Your solution goes here */...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Java programming for ticket price calculator.  It should ask the user if they would like to process...

    Java programming for ticket price calculator.  It should ask the user if they would like to process another ticket order. They should indicate their selection with a Y or N. If they indicate yes, then it should repeat the entire program with the exception of the welcome screen. If they indicate no, it should show the final thank you message and end the program. If they indicate an invalid answer, it should display an error and re-prompt them for a Y...

  • the user should be prompted to enter an integer and then your program will produce another...

    the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd. The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text. Enter an integer: 1234 Number is even, taking every other digit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT