Thread: Math equations not working

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    13

    Math equations not working

    What i need to do is write a program that shows the value of PI based on the following formula:

    PI = 4 - 4/3 +4/5 - 4/7 + 4/9 - 4/11 + ...

    Then show the result as a table that shows the PI value and the number of terms in the formula that were used. For example:
    1 4
    2 2.6667
    3 3.46667
    4 2.895
    ...

    Note that:
    2.6667 = 4 - 4/3
    3.46667 = 4 4/3 + 4/6
    The program should first ask the number of rows in the table.


    This is what i made:

    Code:
    #include "stdio.h"
    
    int rows; //rows user inputs
    int div = 3; // divisor
    int index = 1; //printed index
    int counter = 1; // keeps track of even/odd index
    
    float pi = 4; //pi value
    
    
    void main() {
    
    	printf("Please Enter the number of rows to be generated: \n");
    	scanf("%d", &rows);
    
    	if (rows >= 1) {
    		printf("%d     %f\n",index, pi);
    		div = div+2;
    		printf("div");
    
    
    	}
    
    
    	while(index <= rows) {
    
    		if(counter = 0){	//even
    
    			pi = pi - 4/div;
    			index++;
    			printf("%d     %f\n",index, pi);
    			div = div + 2;
    			counter = counter - 1;
    			
    
    		}
    
    		else {	//odd (counter = 1)
    
    			pi = pi + 4/div;
    			index++;
    			printf("%d     %f\n",index, pi);
    			div = div + 2;
    			counter++;
    
    
    		}
    
    	}
    
    }
    What i get is:

    1 4.000000
    2 4.000000
    3 4.000000
    4 4.000000
    5 4.000000

    and so on. the problem is probably within the "div = div + 2" i dont know how to write that and im pretty sure what i did makes no sense, and ive probably butchered all other mathematical statements aswell.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There are several bugs there:
    1. if(counter = 0) sets counter to 0 in stead of comparing it to 0. Use ==
    2. If counter == 0, you do counter = counter - 1, making it -1. If it is 1, you do counter++, making it 2. So it doesn't switch properly between 0 and 1 as you'd like
    3. 4/div. 4 is an integer, div is an integer, starting with 5. So the result is an integer, which is 4/5 truncated, which is 0. So you add/subtract 0, always. To make it a float, do 4.0/div instead.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    Thank you very much, lot of silly mistakes i made on accident, except your 3rd point which i was not aware of, even after all your correction i found some simple things i misplaced. thanks again =]

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    also note that main should be
    int main()

    read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modular math
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-01-2003, 08:35 AM
  2. Math...
    By TeQno in forum C Programming
    Replies: 4
    Last Post: 07-24-2003, 07:21 AM
  3. Replies: 7
    Last Post: 02-12-2003, 11:43 PM
  4. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 04:38 AM
  5. Library for matrix math/ linear algebra?
    By The V. in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2001, 10:36 PM