•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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•
•
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation:
Rep Power: 2
Solved Threads: 13
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.
•
•
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation:
Rep Power: 2
Solved Threads: 13
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.
•
•
Join Date: Mar 2007
Location: Brisbane, Australia
Posts: 227
Reputation:
Rep Power: 2
Solved Threads: 13
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:
You will probably want to put you system pause back in, but that should do what you want.
. 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 mainYou will probably want to put you system pause back in, but that should do what you want.
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- C++ Fibonacci program help (C++)
- MySQL date formatting (PHP)
- for rookie C++ (template) programmers (C++)
- Data Control Seems To Be Loading Slowly. Very Strange (Visual Basic 4 / 5 / 6)
- Create proxy class from WSDL (.net & java) (XML, XSLT and XPATH)
- Mssql (ASP.NET)
- Source Code that don't work? (Java)
- need help for c++ calculator (C++)
- Incorrect Links to My Site (Site Layout and Usability)
Other Threads in the C++ Forum
- Previous Thread: Problemes with MS C++ 6.0
- Next Thread: can somebody please help me with my homework


Linear Mode