Thread: Can someone please check my code, I'm having a difficult time trying to get pi.

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    24

    Can someone please check my code, I'm having a difficult time trying to get pi.

    We must use a dartboard method to approximate pi to two decimal places. We must use only the three libraries listed. We must ask a user to feed a seed into the random number generator .
    Input: Enter a seed value: 320
    Output: PI is approximately 3.14
    Input: Enter a seed value: 780
    Output: PI is approximately 3.14

    Code:
    #include <stdio.h> /* standard input and output */
    #include <math.h> 
    #include <stdlib.h> /* srand() */ 
    #define SEED 10000000 /* number of throws at dartboard */
    int main (){
        int darts; /* number of darts thrown */
        double x, y, z, pi; /* x and y are the first quadrant points, z is the square of x and y, pi is used approximate pi */
        int p, count=0; /* p stands for number of hits inside the quadrant */
        printf("Enter a seed value: \n"); /* user inputs a seed value */
        scanf("%d", &darts); /* gets seed value from user and feeds it into number darts thrown */
        srand(SEED); /* random number generator */
        count=0; /* sets initial counting to 0 */
        for (p=0; p<darts; p++){ 
            x=(double)rand()/RAND_MAX;
            y=(double)rand()/RAND_MAX;
            z=((x*x)+(y*y)); /* square of distance to origin */
            if (z<=1) count++;}
            pi=(double)count/darts*4;
            printf("PI is approximately %f \n", pi);
            return 0;
        }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    It looks like you got pi to 2 digits, just as requested.

    However, you're doing one thing wrong, according to your description. You say that you are instructed to feed the user seed input to the random number generator. You are not doing that. You are seeding the random number generator with a constant value (SEED).
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    so i should feed the random number generator with just seed?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Based on your description, you should be feeding it with the value that you get from the user. In your case, the variable is called darts.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    thank you! but I'm still not getting the output value of 3.14, i get like 3.15 if seed value is 450 and 3.18 if seed value is 890

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bellagomes21 View Post
    thank you! but I'm still not getting the output value of 3.14, i get like 3.15 if seed value is 450 and 3.18 if seed value is 890
    I suggest trying it with very large numbers. In my tests, it seems to converge on pi, the larger the input.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A few things.

    1. Code:
      #define SEED 10000000 /* number of throws at dartboard */
      If the comment is to be believed, then maybe this is enough throws of darts?
      Code:
      for (p = 0; p < SEED; p++) ...
    2. Seeding with darts as suggested earlier, too:
      Code:
      srand(darts);
    3. Again if we are to believe this comment:
      Code:
      /* x and y are the first quadrant points, z is the square of x and y, pi is used approximate pi */
      Just calling rand() twice does not mean you get a first quadrant point. You may need to make your x returned by rand() positive, or your y positive, in order to get a point in quadrant I (x, y).
      Code:
      x = rand(), y = rand();
      if (x < 0) x = -x;
      if (y < 0) y = -y;
    Last edited by whiteflags; 01-29-2016 at 04:29 PM. Reason: I am bad at math

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    Code:
    #include <stdio.h> /* standard input and output */
    #include <math.h> 
    #include <stdlib.h> /* srand() */ 
    #define SEED 10000000 /* number of throws at dartboard */
    int main (){
        int darts; /* number of darts thrown */
        double x, y, z, pi; /* x and y are the first quadrant points, z is the square of x and y, pi is used approximate pi */
        int p, count=0; /* p stands for number of hits inside the quadrant */
        printf("Enter a seed value: \n"); /* user inputs a seed value */
        scanf("%d", &darts); /* gets seed value from user and feeds it into number darts thrown */
        srand(darts); /* random number generator */
        count=0; /* sets initial counting to 0 */
        for (p=0; p<SEED; p++){ 
            x=rand(), y=rand();
            if (x<0) x=-x;
            if(y<0) y=-y;
            z=((x*x)+(y*y)); /* square of distance to origin */
            }
            if (z<=1) count++;{
            pi=(double)count/darts*4;
            printf("PI is approximately %.2f \n", pi);
            return 0;
        }
    }
    This is what i have but the output is now: PI is approximately 0.00.
    How would i get the output to be PI is approximately 3.14 for any seed value?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I've been meaning to tell you, but the indentation in your program is so bad that it is hiding problems. This is your program when it's legibly indented:
    Code:
    #include <stdio.h> /* standard input and output */
    #include <math.h> 
    #include <stdlib.h> /* srand() */ 
    #define SEED 10000000 /* number of throws at dartboard */
    int main ()
    {
        int darts; /* number of darts thrown */
        double x, y, z, pi; /* x and y are the first quadrant points, z is the square of x and y, pi is used approximate pi */
        int p, count=0; /* p stands for number of hits inside the quadrant */
        
        printf("Enter a seed value: \n"); /* user inputs a seed value */
        scanf("%d", &darts); /* gets seed value from user and feeds it into number darts thrown */
        srand(darts); /* random number generator */
        
        count = 0; /* sets initial counting to 0 */
        for (p=0; p<SEED; p++)
        { 
            x = rand(), y = rand();
            if (x < 0) x = -x;
            if (y < 0) y = -y;
            z=((x*x)+(y*y)); /* square of distance to origin */
        }
        if (z <= 1) 
            count++;
        
            
        {
            pi = (double)count / darts * 4;
            printf("PI is approximately %.2f \n", pi);
            return 0;
        }
    }
    Now the question is, is this how you intended the program to work? I don't think z is ever less than or equal to 1, so 0/darts * 4 is 0.

  10. #10
    Registered User
    Join Date
    Jan 2016
    Posts
    24
    sorry about the indentation, still a newbie here

    i was trying to set count as the number of hits. where pi is approximately equal to 4x/n and n is the darts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-01-2013, 09:21 AM
  2. A Difficult Time
    By SMurf in forum General Discussions
    Replies: 3
    Last Post: 10-19-2011, 08:16 AM
  3. Run-Time Check Failure #2
    By mjskolly in forum C Programming
    Replies: 6
    Last Post: 08-23-2011, 07:58 AM
  4. Difficult time understanding generating terrain (from file)
    By indigo0086 in forum Game Programming
    Replies: 3
    Last Post: 06-07-2007, 11:36 PM
  5. very difficult code - program
    By gaurav_behl in forum C Programming
    Replies: 2
    Last Post: 06-15-2004, 11:33 PM

Tags for this Thread