User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,547 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,353 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1368 | Replies: 5 | Solved
Reply
Join Date: Oct 2007
Posts: 7
Reputation: pyramid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pyramid pyramid is offline Offline
Newbie Poster

Question Approximate Value of pi, incorrect results

  #1  
Oct 15th, 2007
I have been working on this lab for a couple of days. I need to write a program (using a 'for loop') that approximates pi using the following series:

pi = sqrt(6* (1/(1)2 + 1/(2)2 + 1/(3)2+ 1/(4)2 + 1/(5)2 +...... + 1/(n)2)) where n is the number of terms. Make the interger datatypes = long, and the floating point datatypes to long double.

This is the code I have written so far. I don't know what i'm missing but the results I'm getting is not correct.

#include <iostream>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
int main()
{
    long int terms, count;     
    long double pi;
    double sqrt; 
           
    
      cout << "Enter the number of terms to use:   " << endl;
       cin >> terms; 
   
    
       for (count =1; count <= terms; count++);
       {          
       pi = (1.0 /(terms * terms));                                   
       }
       pi = sqrt * pi;
       pi = pi * 6;
       
                   
     cout <<setprecision (15)<< "The approximate value of pi is:   " << pi << endl;
   
system ("pause");

return 0;
} // end main
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Posting Whiz in Training

Re: Approximate Value of pi, incorrect results

  #2  
Oct 15th, 2007
for (count =1; count <= terms; count++);

This pretty makes your loop useless, you don't want that semi-colon.

You also need to check the logic behind the steps you take to calculate pi. E.g. I don't see any square root function used.
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: pyramid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pyramid pyramid is offline Offline
Newbie Poster

Re: Approximate Value of pi, incorrect results

  #3  
Oct 15th, 2007
thank you. The codes I have has the sqrt * pi listed.
My understanding of this problem is that:
using the series given, it would be (1 / N^2), which I wrote as pi = (1.0/(terms * terms);
then sqrt * pi; and eventually, pi * 6.


Thanks.
Reply With Quote  
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Posting Whiz in Training

Re: Approximate Value of pi, incorrect results

  #4  
Oct 15th, 2007
pi = sqrt(6* (1/(1)2 + 1/(2)2 + 1/(3)2+ 1/(4)2 + 1/(5)2 +...... + 1/(n)2))

Ok, so looking at this equation you have the series of summing 1/(n)2 . This is not equivalent to the sum of 1/(n^2).

I did notice you are using sqrt in your code, but 2 points. 1, surely sqrt means square root and not multiplication (the sqrt is also undefined in your code). 2, the sqrt is located outside of the parenthesis indicating it should be the last operation (you currently have *6 as the final operation).

Hope this helps.
Reply With Quote  
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Posting Whiz in Training

Re: Approximate Value of pi, incorrect results

  #5  
Oct 15th, 2007
I appologise about the ^2, you are correct here. I assume those "2"s are supposed to be in superscript . Anyway I felt nice so:

#include <iostream>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
int main() {
    long int terms, count;     
    long double pi = 0;
    
    cout << "Enter the number of terms to use:   " << endl;
    cin >> terms;
    
    for (count=1; count<=terms; count++) {
        pi += (1.0 / (count * count));
    }
    pi = pi * 6;
    pi = sqrt(pi);

    cout << "The approximate value of pi is:   " << pi << endl;

    return 0;
} // end main

You will probably want to put you system pause back in, but that should do what you want.
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: pyramid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pyramid pyramid is offline Offline
Newbie Poster

Re: Approximate Value of pi, incorrect results

  #6  
Oct 15th, 2007
A BIG thank you.

I tried the codes so many different ways. I had several people who were nice enough to help, but to no avail.... You are the first one that pointed out about the "sqrt" being outside the parenthesis meant that it should be the last operation.

I am very knew to C++, (on my 9th day of class); and I really appreciate the fact that you didn't make me feel "stupid" for not knowing what should be done. This kind of attitude makes a huge difference to those of us that really wants to learn.....

Thanks again.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC