Help with this code.

This is a discussion on Help with this code. within the C Programming forums, part of the General Programming Boards category; [code] THe value for PI can be determined by the series equation PI= 4x (1-1/3+1/5-1/7+1/9-1/11+1/13-...) write a program to approximate ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Help with this code.

    [code]

    THe value for PI can be determined by the series equation

    PI= 4x (1-1/3+1/5-1/7+1/9-1/11+1/13-...)

    write a program to approximate the value of PI using the formula given including terms up through 1/99.

    I wrote this code but not sure what I am doing wrong. please some one help.

    thanks


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int
    main(void)
    {
    double x, y, z, sum, pi, i, n;
    
    sum = 1.0;
    for (i = 0, y = 3.0; i < n; ++i, y += 2.0)
    {
         z = 1.0 / y;
         if ((i & 1) == 0)
           sum -= z;
         else
           sum += z;
         }
    
         pi = 4.0 * sum;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use integer for counter value (i,n).
    Don't forget to initialize the values before use!

    Code:
         pi = 4.0 * sum;
    Isn't this line supposed to be outside loop?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Stop declaring all your variables on one line and give them all initial values.

    I think you forgot to assign n something. So the loop is out of control. Who knows what else.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Quote Originally Posted by Bayint Naung View Post
    Code:
         pi = 4.0 * sum;
    Isn't this line supposed to be outside loop?
    Isn't it already?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    OOps , It's either my eye problem or indentation problem

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    No return.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Looks to me more like the user got the snippet from here. He's just trying to fill it out, because he has no clue how to write a program that's not complete and on the internet for him to copy.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yeah, that post is from September 2008. Is this what passes for "I wrote this code" these days?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Mmm, cryptic...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21