473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Help with apppproximate value of pi = sqrt(6* (1/(1)2 + 1/(2)2 + 1/(3)2+ 1/(4)2 + 1/(

18
Hello
I am working on one of my lab for this week, which calculates the approximate value of pi. Listed below is the actual problem, which I am posting here, so that you can see the different requirements for the program.

The program is simple. I used the ‘for loop’ to define the number of increments, then set up a formula to calculate the pi. My codes compile with no problem, but the result is incorrect. One of the test data I used was 100 (terms), which should result in 3.13592904 (or something close to this). My codes return a result of 19.7060531358098.

I would appreciate any help from you guys. Thanks.



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.

Your problem for this lab is to write a program (using a for loop) that approximates pi using the above series. Allow the user to specify the number, n, of terms to be used. Try your program with n = 100, then 1,000, then 10,000.

Note how the approximation becomes better and better as you increase the number of terms. For comparison purposes, the correct value of pi to 15 decimal places is 3.141592653589793. You won’t get that kind of accuracy with your program, but to get the best accuracy possible make your integer datatypes long and your floating point datatypes long double.

Important: In order to run correctly in the Automatic Grading System, you must set your output precision to 15 and use the correct datatypes for all your variables.





Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     long double terms;     
  9.     double pi;
  10.     long int count;
  11.  
  12.     // Prompt the user to enter the number of terms
  13.        cout << "Specify the number of terms to use:   " << endl;
  14.        cin >> terms; 
  15.  
  16.     // Set up increments
  17.  
  18.        for (count =1; count <= terms; count++)
  19.  
  20.     // formula that will approximate pi based on the user input
  21.  
  22.        {
  23.            pi = pi + (1.0 / (2.0 * count - 1));
  24.        }   
  25.             pi = pi * 6;
  26.  
  27.      cout << setprecision (15) << "The approximate value of pi is:   " << pi << endl;
  28.  
  29. system ("pause");
  30.  
  31. return 0;
  32. } // end main
Oct 11 '07 #1
17 3170
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. pi = pi + (1.0 / (2.0 * count - 1));
I believe this line is giving you trouble. Specifically, then denominator of the fraction. You write 2.0 * count - 1, which will perform (2.0 * count) - 1, though you might have meant 2.0 * (count - 1). However, from your problem specification, I don't think you have to have the -1 - that is, I think it should just be 2.0 * count.
Oct 11 '07 #2
pyramid
18
Expand|Select|Wrap|Line Numbers
  1. pi = pi + (1.0 / (2.0 * count - 1));
I believe this line is giving you trouble. Specifically, then denominator of the fraction. You write 2.0 * count - 1, which will perform (2.0 * count) - 1, though you might have meant 2.0 * (count - 1). However, from your problem specification, I don't think you have to have the -1 - that is, I think it should just be 2.0 * count.


Thanks.

Expand|Select|Wrap|Line Numbers
  1.      pi = pi + (1.0 / (2.0 * count));
.

I changed the code and removed the "-1". The result is 15.5621325529189 (using 100 as terms). It is still incorrect, but it's closer to the correct answer. I worked on this all day yesterday and I just can't figure out why it's not working.

Also, I apologize that I didn't put the codes in block. I just learned that today.

Thanks.
Oct 11 '07 #3
Ganon11
3,652 Expert 2GB
Don't worry too much about the code tags - just go ahead and read our Posting Guidelines when you get the chance.

Anyway, I think I found your problem. In the assignment specification, it says to take the square root of your final answer, which I don't see you doing.

However, if your answer is ~15, then your final result will probably be closer to 4 than 3...It will be yet closer to your answer, though.
Oct 11 '07 #4
pyramid
18
Don't worry too much about the code tags - just go ahead and read our Posting Guidelines when you get the chance.

Anyway, I think I found your problem. In the assignment specification, it says to take the square root of your final answer, which I don't see you doing.

However, if your answer is ~15, then your final result will probably be closer to 4 than 3...It will be yet closer to your answer, though.

I added the sqrt * pi, and the result is as you said, around 4...... I still cannot submit because it's not running the program completely (with correct results). I don't understand why it's not working.

I appreciate your input. Please let me know if you can think of anything else that may be causing the problem. I will continue to work on the codes. Thanks.
Oct 11 '07 #5
Ganon11
3,652 Expert 2GB
I got it!

I think you are reading your problem incorrectly, and when you copied it to the forums, it was also copied incorrectly. We have been reading the denominator as 2(1), then 2(2), 2(3),...,2(n). But I think it's actually (1)^2, (2)^2, (3)^2,...,n^2. Making this change and using terms = 100, I got 3.13207653180911 as desired from my own sample program.

As an aside, make sure you make pi a long double as requested in your problem specification.
Oct 11 '07 #6
Banfa
9,065 Expert Mod 8TB
I think you are reading your problem incorrectly, and when you copied it to the forums, it was also copied incorrectly. We have been reading the denominator as 2(1), then 2(2), 2(3),...,2(n). But I think it's actually (1)^2, (2)^2, (3)^2,...,n^2. Making this change and using terms = 100, I got 3.13207653180911 as desired from my own sample program.
I think you are right the original series SUM 1..N ( 1 / 2N ) does not converge, it diverges as can be proved with the Integral test for convergence.

Since it diverges sqrt( 6 * SUM) also diverges and therefore reaches no limiting value and N -> infinity.
Oct 11 '07 #7
pyramid
18
Thank you guys. I worked on this more last night, and finally gave up around 10 p.m. -

I'm really excited about the results you got when you made the change. I will change the codes using the guidelines you provided. I'll post here again once I get my change completed.

Thanks again.
Oct 12 '07 #8
pyramid
18
The description of the problem is actually the same as what the instructor gave us ( i copied and pasted here). I am so frustrated because the program should be simple, but yet, I can't seem to generate the correct codes. Granted that I'm new to this class, I should at least understand the instructions.

I changed my codes from the original codes using (1)^2, (2)^2, (3)^2,...,n^2.,. I tested using 100 as 'n' of terms, and the result = 3.69166666666667 which is incorrect (but very close...... )

Expand|Select|Wrap|Line Numbers
  1.        for (count =1; count <= terms; count++)
  2.        {
  3.            pi = 6 * 1.0/2 + 1.0/4 + 1.0/6 + 1.0/8 + 1.0/10 + 1.0/ (terms * 2.0);
  4.        }
  5.         pi = pi;
long terms;
long double pi;
long int count;


Thanks for all your help.
Oct 12 '07 #9
Ganon11
3,652 Expert 2GB
What are you doing inside that for...loop? It looks like you're trying to write the entire series in one line, which would ruin the value of using a for...loop in the first place. You should only be adding one term to pi each execution of the loop.

You were very close before with your program. As Banfa and I have determined, the series is not (1 / 2N), it is (1 / N^2).

I was able to get the program working correctly by taking your sample code and changing one line.
Oct 12 '07 #10
pyramid
18
I think I have been working on this problem too long. I still cannot get the codes to work. I changed the codes to:

Expand|Select|Wrap|Line Numbers
  1. for (count =1; count <= terms; count++)
  2.  
  3.         {  
  4.          pi = (1.0 /(terms * terms)*2);
  5.          }
  6.         pi = sqrt * pi; 
  7.         pi= pi * 6;
Using terms = 100, the result I got was 2.01232066038189e-310 which is incorrect.

I appreciate all the help.

Thank you
Oct 15 '07 #11
Ganon11
3,652 Expert 2GB
You shouldn't be multiplying the denominator by 2 at all. Ignore what your assignment said, it did not mean to multiply by two, it meant to square the term. Get that *2 out of there and see if it works.
Oct 15 '07 #12
pyramid
18
This is the revised codes. I am still unable to get the correct results.

Expand|Select|Wrap|Line Numbers
  1. for (count =1; count <= terms; count++);
  2.        {          
  3.        pi = (1.0 /(terms * terms));                                   
  4.        }
  5.        pi = sqrt * pi;
  6.        pi = pi * 6;
  7.  
  8.  
I set all the datatypes as outlined on my lab:
long int terms
long int count;
long double pi;
double sqrt;

using terms=100, the result I'm getting is 4.64600985669798e+265.

Thanks.
Oct 15 '07 #13
RRick
463 Expert 256MB
You got a couple of problems.

First it is not pi that's being summed up in the loop, but an intermediate value based only on the inverse of the squared counter. You need to maintain a separate value for that value and have the loop modify it.

Second, got your sqrt and times 6 reversed.
Oct 16 '07 #14
pyramid
18
Got it.

A big thanks to all of you.

I modified the codes based on your input and it worked!. Thanks again.
Oct 16 '07 #15
I have been fooling around with this little app and cannot get it to work. I've tried debugging in everyway, and my algorithm seems to be working, but I am getting the wrong results. I am talking, way off. This should be so simple for me, but I am a little rusty on my C++.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <conio.h>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. int main(){
  8.     long double terms;
  9.     float denom = 0, numer = 0, pi = 0;
  10.     long int count;
  11.  
  12.     // Get user input for n terms
  13.     cout << "Please enter number of terms: ";
  14.     cin >> terms;
  15.  
  16.     // Setup for...loop to count
  17.     for ( count = 1; count <= terms; count ++ )
  18.         denom += ( pow( count, 2 ) );
  19.  
  20.     cout << denom << endl;
  21.  
  22.     numer = 1 / denom;
  23.  
  24.     cout << numer << endl;
  25.  
  26.     pi = ( numer * 6 );
  27.  
  28.     cout << pi << endl;
  29.  
  30.     pi = sqrt( pi );
  31.  
  32.     // Output value of pi
  33.     cout << "The value of pi is " << pi;
  34.  
  35.     getch();
  36.  
  37.     return 0;
  38. } // end main
Any ideas, please help me! This has been driving me crazy all day at work.

/me sounds lame...
Oct 16 '07 #16
Ganon11
3,652 Expert 2GB
You are incorrectly assuming that 1/a + 1/b + 1/c + ...+ 1/n is 1/(a + b + c + ... + n), so you can't calculate the sum in separate steps like this. Instead of having denom and numer. calculate the fraction at each step of the for loop and add it to a running sum.
Oct 16 '07 #17
JosAH
11,448 Expert 8TB
Don't they teach basic calculus and algebra anymore nowadays?

kind regards,

Jos
Oct 16 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Eddie | last post by:
I am having a strange issue with MySQL. I just installed it on my Zaurus and loaded a few of my databases in it. All very small. When I do a count withoout a where, it hangs. It does this on all...
1
by: ritskits | last post by:
I have the dropdownlist that is populated when the form is loaded. I have the button that opens search child window using the modalwindowdialog and it returns the value that I want to add to the...
5
by: Matt Williamson | last post by:
I have a table that gets generated every 5 minutes containing the drive space on my servers. The table is sortable via an external javascript file by clicking the column headers (using sortable.js...
2
by: yo_mismo | last post by:
Hi, I send a parameter from a Form (Form1) to an other form (Form2): Form2 frm2 = new Form2(); frm2.number_frm2 = number_frm1; frm2.Show(); The problem i got is that the variable...
3
by: JHNielson | last post by:
I AM AT THE WIRE AND DESPERATELY NEED SOME HELP! I have a query that checks that when the user submits data to me, it checks that their total is correct. The SQL looks like this: UPDATE...
4
jeffbroodwar
by: jeffbroodwar | last post by:
Hello, i have a problem about assigning a char value to a byte... please check the code below : ======================================================== Scenario # 1 : This code doesn't work : ...
0
by: Spam Catcher | last post by:
Hi all, I'm trying to set the DataGridViewCell.Value of an unbound column. I can set the value in code, but the value isn't being displayed. I tried to RefreshEdit/EndEdit/Invalidate the...
1
by: v2naveen | last post by:
Hi, I have table called CUSTOMER with the following fields and values Interval_date rec_value ------------- ---------- 10/1/2007 5:30:00 12.0 10/1/2007 6:00:00 17.0 10/1/2007 7:00:00 15.0 .
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.