Thread: Help with simple program

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Question Help with simple program

    Can someone please check if this program is correct.... i'm trying to write a PI approximation....but i can't get the output that i have to obtain....... suggestions?????

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
          double pi=0, y=1;
          int num, i;
          char ch;
    
          pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;
    
           pi = 0;
          y = 1;
    
    
          do
            {
              cout<<"How many terms do you want to use?\n";
              cin>>num;
    
    
    	for (i = 1; i<=num; i++)
    
    		if (i%2 == 0)
    			
    		pi = pi - 4/y;
    
    		else 
    		pi = pi + 4/y;
    		cout<<pi,;
    
    		y = y + 2;
    			
    	
    cout<<"Do you want another approximation? please enter Y or N\n";
    cin>>ch;
    
    }while (ch!= 'n' && ch!= 'N');
    
    return 0;
    
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    should put you in right direction

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
          double pi, y=1;
          int num, i;
          char ch;
          pi = 4.0 - 4.0/3.0 + 4.0/5.0 - 4.0/7.0 + 4.0/9.0 - 4.0/11.0 + 4.0/13.0 - 4.0/15.0;
          y = 17.0;
    
          do
            {
              cout<<"How many terms do you want to use?\n";
              cin>>num;
    
    	for (i = 1; i<=num; i++)
    	{
    		if (i%2 == 0)
    			
    		pi = pi - 4/y;
    
    		else 
    		pi = pi + 4/y;
    		y = y + 2;
    	}			
    	cout<<pi;
    	cout<<"Do you want another approximation? please enter Y or N\n";
    	cin>>ch;
    
    }while (ch!= 'n' && ch!= 'N');
    
    return 0;
    
    }

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You have to watch your whitespace. You only need to print the number after you are out of the loop. The number of terms needs to be very high for accurate results.

    Code:
    	do
    	{
    		cout<<"How many terms do you want to use?\n";
    		cin>>num;
    
    
    		for (i = 1; i<=num; i++)
    		{
    			if (i%2 == 0)
    			{
    				pi = pi - 4/y;
    			}
    			
    			else 
    			{
    				pi = pi + 4/y;
    			}
    			y = y + 2;
    		}
    			
    		cout<<pi<<endl;
    
    		cout<<"Do you want another approximation? please enter Y or N\n";
    		cin>>ch;
    
    		pi = 0;
    
    	} while (ch!= 'n' && ch!= 'N');

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    thank you so much for your help.....i can see now where i made my mistakes....

    thanks!!!!!!

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    You also set pi back to 0 after assigning it.

    Code:
          pi = 4 - 4/3 + 4/5 - 4/ 7 + 4/ 9 - 4/11 + 4/13 - 4/15;
    
          pi = 0;
          y = 1;

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Tonto
    You have to watch your whitespace.
    IMAO, so do you. TABs indent too far. Use 3-4 spaces instead
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM