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 427,497 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,456 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: 1339 | Replies: 5
Reply
Join Date: Oct 2006
Posts: 4
Reputation: Zopharr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zopharr Zopharr is offline Offline
Newbie Poster

C++ Homework project, need help (simple problem)

  #1  
Oct 31st, 2006
We were assigned the following problem as a large project in my Freshman intro to C++ programming class:

We have to determine the number of terms needed to calculate Pi to a specific number (3.0, 3.1, 3.14, 3.141, 3.1415, and 3.14159), using the formula:

pi= 4(1-1/3+1/5-1/7+1/9-1/11+1/13...)

I thought I had the code figured out pretty easily, but I have having a problem. I was going to use the following (incomplete) code as a model:

// Date: 10/30/06
// Homework #07
// Student: Jim Hummel
// Course:  CSE 121
// This program calculates the number of terms required for an approximation of pi.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double pif(double q);
int terms=0;
int main()
{double q=3.1;
cout << "Pi Value    Terms\n--------------------------\n";
cout << fixed << setprecision(2);
cout << pif(q)<< "        "<< terms << endl; 
return 0;
}
double pif (double q)
{double n=1.0;
double pi=0;
    while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))
    {
        pi = pow(-1.0,n)/(2.0*n-1.0);
        n++;
            terms++;
    }
    pi*=4.0;
    return pi;
}


In the main function there, I was going to try to have it call the function pif() once for each value q that I set to it, then cout the results, and the number of terms.

I've been toying around with possible options (as seen in the while statement there), but no matter what I do, it returns a value of o terms and 0.00 for pi each time.

What am I doing wrong?

Thanks in advance, this is killing me.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: C++ Homework project, need help (simple problem)

  #2  
Oct 31st, 2006
What is the meaning of q?
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Oct 2006
Posts: 4
Reputation: Zopharr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zopharr Zopharr is offline Offline
Newbie Poster

Re: C++ Homework project, need help (simple problem)

  #3  
Oct 31st, 2006
Sorry, q was to represent the question.

I was going to have it so that I would set q to 3.0 and send it to pif()... then cout the results, and then set q to 3.1... repeat with 3.14, 3.141, all the way to 3.14159, which is as far as I need to go. I need to know the number of terms used for each of those values (how many fractions were necessary to get pi to equal that number.)

Thanks.
Reply With Quote  
Join Date: Oct 2006
Posts: 4
Reputation: Zopharr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zopharr Zopharr is offline Offline
Newbie Poster

Re: C++ Homework project, need help (simple problem)

  #4  
Oct 31st, 2006
Maybe this could help: This is the exact problem:

Problem 2. [The Number p] The number p may be calculated using the following infinite series:
p = 4(1-1/3+1/5-1/7+1/9-1/11+1/3
How many terms of this series you need to use before you get the approximation p of p, where:
a) p = 3.0
b) p = 3.1
c) p = 3.14
d) p = 3.141
e) p = 3.1415
f) p = 3.14159
Write a C++ program to answer this question.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: C++ Homework project, need help (simple problem)

  #5  
Oct 31st, 2006
I don't understand why you are testing how close the value calculated is to 1/q. But anyway, here is a more refined way of writing the source. The algorithm is the same, so any errors are due to that.
Do not use global variables unless absolutely necessary.

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. double pif(double q);
  6. int terms=0;
  7. int main()
  8. {
  9. double q=3.1;
  10. cout << "Pi Value Terms\n--------------------------\n";
  11. cout.setf( ios::fixed );
  12. cout.precision (2);
  13. cout << pif(q)<< " ";
  14. cout << terms << endl; // Since you are changing the value of terms in the above line,
  15. // do not output it in the same line. The result is undefined.
  16. // Use a seperate line to output terms.
  17. // But a better way will be to output the value of n in pif()
  18. return 0;
  19. }
  20. double pif (double approx) // Use meaningful parameter names rather than just p, q, r
  21. {
  22. long n=0;
  23. double pi=0.0;
  24. double error = fabs( pi - approx );
  25. double tolerance = 1 / approx; // Assuming approx is positive, this is also +ve
  26. // while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))// Actually this should be
  27. // while (pi<(q-(1.0/q)) || pi>(q+(1.0/q))).
  28. // It is the same as | pi -q | > ( 1.0 / q )
  29. // |x| = absolute value of x
  30. while ( error > tolerance )
  31. {
  32. n++;
  33. term++;
  34. pi = pi + pow( -1, n + 1)/(2 * n - 1 );
  35. error = fabs( pi - approx);
  36. }
  37. pi*=4.0;
  38. // cout << pi << " "<< n << " " << error << endl;
  39. return pi;
  40. }
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Oct 2006
Posts: 4
Reputation: Zopharr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Zopharr Zopharr is offline Offline
Newbie Poster

Re: C++ Homework project, need help (simple problem)

  #6  
Nov 1st, 2006
Thanks for the reply, although I didn't read it in time before I had to hand it in.

I worked on building an entirely new code without the rediculous while statements... it led to a very long, messy, and unoganized code, but it worked.


I spent the remainder of my time trying to turn it into a while loop, using another variable for the setprecision (as seen below) and the q-(number) statements, but this code here did work and got the job done:

  1. // Date: 10/30/06
  2. // Homework #07
  3. // Student: Jim Hummel
  4. // Course: CSE 121
  5. // This program calculates the number of terms required for an approximation of pi.
  6.  
  7. #include <iostream>
  8. #include <cmath>
  9. #include <iomanip>
  10. using namespace std;
  11. int main()
  12. {double q=3.0;
  13. double n=1;
  14. bool check=false;
  15. double pi = 0;
  16. int terms=0;
  17. while (!check)
  18. {
  19. pi += (double) pow(-1, n+1)/(2*n-1);
  20. cout << fixed << setprecision(1);
  21. if (q-.1<=pi*4 && q+.1>=pi*4)
  22. check=true;
  23. else
  24. terms++;
  25. n++;
  26. }
  27. pi *= 4;
  28. cout << terms << " " << pi << endl;
  29.  
  30.  
  31.  
  32. //Part 2//
  33.  
  34.  
  35.  
  36. q=3.1;
  37. n=1;
  38. check=false;
  39. pi = 0;
  40. terms=0;
  41. while (!check)
  42. {
  43. pi += (double) pow(-1, n+1)/(2*n-1);
  44. cout << fixed << setprecision(1);
  45. if (q-.01<=pi*4 && q+.01>=pi*4)
  46. check=true;
  47. else
  48. terms++;
  49. n++;
  50. }
  51. pi *= 4;
  52. cout << terms << " " << pi << endl;
  53.  
  54.  
  55.  
  56. //Part 3//
  57.  
  58.  
  59.  
  60. q=3.14;
  61. n=1;
  62. check=false;
  63. pi = 0;
  64. terms=0;
  65. while (!check)
  66. {
  67. pi += (double) pow(-1, n+1)/(2*n-1);
  68. cout << fixed << setprecision(2);
  69. if (q-.001<=pi*4 && q+.001>=pi*4)
  70. check=true;
  71. else
  72. terms++;
  73. n++;
  74. }
  75. pi *= 4;
  76. cout << terms << " " << pi << endl;
  77.  
  78.  
  79.  
  80. //Part 4//
  81.  
  82.  
  83.  
  84. q=3.141;
  85. n=1;
  86. check=false;
  87. pi = 0;
  88. terms=0;
  89. while (!check)
  90. {
  91. pi += (double) pow(-1, n+1)/(2*n-1);
  92. cout << fixed << setprecision(3);
  93. if (q-.0001<=pi*4 && q+.0001>=pi*4)
  94. check=true;
  95. else
  96. terms++;
  97. n++;
  98. }
  99. pi *= 4;
  100. cout << terms << " " << pi << endl;
  101.  
  102.  
  103.  
  104. //Part 5//
  105.  
  106.  
  107.  
  108. q=3.1415;
  109. n=1;
  110. check=false;
  111. pi = 0;
  112. terms=0;
  113. while (!check)
  114. {
  115. pi += (double) pow(-1, n+1)/(2*n-1);
  116. cout << fixed << setprecision(4);
  117. if (q-.00001<=pi*4 && q+.00001>=pi*4)
  118. check=true;
  119. else
  120. terms++;
  121. n++;
  122. }
  123. pi *= 4;
  124. cout << terms << " " << pi << endl;
  125.  
  126.  
  127.  
  128. //Part 6//
  129.  
  130.  
  131.  
  132. q=3.14159;
  133. n=1;
  134. check=false;
  135. pi = 0;
  136. terms=0;
  137. while (!check)
  138. {
  139. pi += (double) pow(-1, n+1)/(2*n-1);
  140. cout << fixed << setprecision(5);
  141. if (q-.000001<=pi*4 && q+.000001>=pi*4)
  142. check=true;
  143. else
  144. terms++;
  145. n++;
  146. }
  147. pi *= 4;
  148. cout << terms << " " << pi << endl;
  149. return 0;
  150. }
  151.  
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 7:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC