Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The output should be tabulated to list n, c and the Pi value for each estimate of Pi up to a chosen number of points input by the user at run time. If the user input 500 for example, there should be 500 rows displayed.

What I have tried:

C++
Console.Write("Please enter a Random Number:  ");
string input = Console.ReadLine();
n = double.Parse(input);

if ((n < 1) || (n > 500))
    Console.WriteLine("Error!The number should be between 1 and 500!"
        + "Please try again");

else
    while (n < 500)
    {
        Console.Write("N" + n);
        n++;
    }
{
    int x = 1;
    double y = 1;
    c = x * x + y * y;
    c++;
}
{
    Pi = 4 * c / n;
    Pi++;
    Console.WriteLine("N : {0}", n);
    Console.WriteLine("C: {0}", c);
    Console.WriteLine("Pi: {0} ", Pi);
}
Posted
Updated 19-Mar-16 0:10am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-16 1:07am    
How is it related to Monte Carlo? What makes you thinking that Pi digits can be found using the Monte Carlo method?
—SA
Richard MacCutchan 19-Mar-16 5:51am    
Quite a lot of that code does not seem to make sense. What exactly is the problem?

1 solution

Um.
What do you expect that code to do?
You read a "random number" from the user - which is a strange idea to start with - and if it's outside the limits (that you didn't tell him) you print a message, but continue to use it anyway...
Otherwise, you write all the numbers from that value up to 500 inclusive and continue anyway.
Regardless of what you printed earlier, you set c to three by a convoluted method, and then assume that pi is 0.024, add one to it, and print
C#
N : 500
C : 3
Pi: 1.024
I know this is probably your homework, but you need to throw that away, sit down, and try to work out exactly what you are supposed to be doing here - because that isn't even close to what you teacher asked for! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900