Thread: Iterations with integers...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Question Iterations with integers...

    I'm having trouble trying to figure out how to put this into code. The main function of this is the user inputs the number of iterations he or she thinks it will take to get close to the sixth decimal point of Pi.


    π = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11

    First off I do this

    #define Pi 3.1415926535

    Then I know a few variables that I need which are

    int Numerator, Denominator;

    My question is how do you put in code that after every other fraction you want a minus and then a plus as shown up top? That's the only thing I need help with.

    On a side note. I'm gonna be using a loop to ask if the user wants to do it again or try again until gets the right answer. Also, I know I'm gonna use Increments.

    I appreciate any input/advice.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What happens to the value of sign as you go through this loop?

    Code:
    int sign = 1;
    
    for( n = 0; n < nMax; ++n )
    {
        sign = -sign;
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    Honestly, beats me. I'm new to C and I'm taking a class on it. I early had trouble repeating the program. such as "Would you like to do this again" y/n

    But I got that part down. Programming in C is soo different without the Forms.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or perhaps, more obvious.

    Code:
    int sign = 1;
    
    for( n = 0; n < nMax; ++n )
    {
        sign = sign * -1;
    }
    Do you know your "sign rules"? i.e, negative * negative = positive, negative * positive = negative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

Tags for this Thread