1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C+ Programming Homework Help

  1. Nov 4, 2008 #1
    Here is what I have coded so far:

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>

    int randomNumber(void); // Function prototype for random number generator
    void srand(unsigned int seed); // Function prototype for seeding the RNG

    int main()
    {
    // Declaration of double variables

    double x; // coordinate of dart
    double y; // coordinate of dart
    double v; // coordinate of dart
    double estimatedPi;
    double Pi;

    // Declaration of long variables

    long startValue;
    long seed;
    long impact;
    long target;

    // Declaration of initialization values

    startValue = 100;
    seed = time(NULL);
    impact = 0;
    target = 0;
    Pi = 3.14159265358979;

    srand(seed);

    printf("\t\t\tPi Dart Simulator v1.2 \n\n");
    printf("True Pi to 8 decimal places: 3.14159265358979 \n");
    printf("Random number seed: 1225850161 \n\n");

    printf(" Throws Hits Esimated Pi %%Error \n");
    printf("====================================================\n\n");

    while (impact < startValue && impact < 10000000)
    {
    x = (double)rand()/RAND_MAX;
    y = (double)rand()/RAND_MAX;
    impact += 1;

    if ((sqrt(x*x + y*y)) <= 1.00)
    {
    target += 1;
    estimatedPi = ((double)target / (double)impact)*4;
    }

    while (impact >= startValue)
    {
    printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
    (((estimatedPi) - Pi) / Pi)*100);
    startValue *= 10;
    impact = target = 0;
    }
    }

    system("pause");
    return 0;
    }

    unsigned long random = 1;

    void srand(unsigned int seed)
    {
    random = seed;
    }

    int randomNumber()
    {
    random = random * 1103515245 + 12345;
    return (unsigned int)(random / 65536) % 327768;
    }


    What I cannot figure out is why my values for the estimation of Pi and percent error is coming out wrong - PLEASE HELP
     
  2. jcsd
  3. Nov 5, 2008 #2

    zyh

    User Avatar

    I tested your code, and there are many errors. I'm very curious whether it could compile on your system. Which compiler did you use? 
    from the
    Code (Text):
    system("pause");
    , I guess that you are using Microsoft windows.:rofl:, and I'm not sure a it is a legal statement in gcc under linux.

    Code (Text):

    int randomNumber(void); // Function prototype for random number generator
     
    What does this function use for?

    Code (Text):

    void srand(unsigned int seed); // Function prototype for seeding the RNG
     
    srand() is a function supplied by standard c library. So, you needn't implement it yourself.

    Code (Text):

    printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
    (((estimatedPi) - Pi) / Pi)*100);
     
    You have Five "%", but you have only four associated parameters. And there is a mismatch of types. Because " estimatedPi " is declared as a double format variable, you should use "%lf" instead. So as the expression "(((estimatedPi) - Pi) / Pi)*100)".

    After modifying the whole bugs, it works.
    Here is the result in my system
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C+ Programming Homework Help
  1. C program help (Replies: 5)

  2. C++ program help! (Replies: 1)

  3. C program help needed (Replies: 3)

Loading...