GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Oct-2004, 17:08
mccrbr01 mccrbr01 is offline
New Member
 
Join Date: Oct 2004
Posts: 1
mccrbr01 is on a distinguished road

Please help with my C program to find Pi values


This problem involves calculating the value of Pi from the infinite series: 4-4/3+4/5+4/7+4/9-4/11+...
I have successfully created a program that creates the values of Pi for the first eleven terms of the series. I now need to create a program that determines and prints how many terms of the series you have to use before you first get:
a. 3.14?
b. 3.141?
c. 3.1415?
d. 3.14159?
e. 3.14159265358979?

The program should use break and continue statements. My code is given below. Two changes I know it needs (but not sure the best way to do) are:
1. Use a larger array
2. Consider the correct number of decimal positions

CPP / C++ / C Code:
#include <stdio.h> 
#include <math.h> 
int main () 
{ 
int i; /* counter */ 
double pi[11]; 
double n; 
double p; 
int term = 0; 
double plus = 0; 
double minus = 0; 

printf ("%7s%20s\n", "# Of Terms", "Values of Pi"); 

for(i=0; i<11; i++){ 

p=0; 
plus=0; 
for (p=0; p<=i; p+=2){ 
plus = plus + 4 / (1+2*p); 
} /* end for */ 

n=1; 
minus=0; 
for (n=1; n<=i; n+=2){ 
minus = minus - 4 / (1+2*n); 
} /* end for */ 

term = i + 1; 
pi = minus + plus; 

if (pi == 3.14){ 
printf("%7d%20f\n", term, pi); 
continue; 
} /* end if */ 

if (pi == 3.141){ 
printf("%7d%20f\n", term, pi); 
continue; 
} /* end if */ 


if (pi == 3.14159265358979){ 
printf("%7d%20f\n", term, pi); 
break; 
} /* end if */ 

} /* end for */ 

return 0; 
}
Last edited by dsmith : 31-Oct-2004 at 05:27. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 30-Oct-2004, 22:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by mccrbr01
This problem involves calculating the value of Pi from the infinite series: 4-4/3+4/5+4/7+4/9-4/11+...
I have successfully created a program that creates the values of Pi for the first eleven terms of the series. I now need to create a program that determines and prints how many terms of the series you have to use before you first get:
a. 3.14?
b. 3.141?
c. 3.1415?
d. 3.14159?
e. 3.14159265358979?

The program should use break and continue statements. My code is given below. Two changes I know it needs (but not sure the best way to do) are:
1. Use a larger array
2. Consider the correct number of decimal positions

#include <stdio.h>
#include <math.h>
int main ()
{
int i; /* counter */
double pi[11];
double n;
double p;
int term = 0;
double plus = 0;
double minus = 0;

printf ("%7s%20s\n", "# Of Terms", "Values of Pi");

for(i=0; i<11; i++){

p=0;
plus=0;
for (p=0; p<=i; p+=2){
plus = plus + 4 / (1+2*p);
} /* end for */

n=1;
minus=0;
for (n=1; n<=i; n+=2){
minus = minus - 4 / (1+2*n);
} /* end for */

term = i + 1;
pi = minus + plus;

if (pi == 3.14){
printf("%7d%20f\n", term, pi);
continue;
} /* end if */

if (pi == 3.141){
printf("%7d%20f\n", term, pi);
continue;
} /* end if */


if (pi == 3.14159265358979){
printf("%7d%20f\n", term, pi);
break;
} /* end if */

} /* end for */

return 0;
}

Welcome to the forums. You initially have 3 problems
1) you didn't read the stickys about code tags to keep your formatting so your code can be read
2) you didn't use formatting so your code can be read. All your code is left-justified making it nearly impossible to follow. Check out this tutorial for some formatting guidelines.
3) Floating point values are inexact, making the statement
CPP / C++ / C Code:
if (pi == 3.141)
pretty much useless. Caclulating the float value pi will approximate 3.141, but most likely won't be exact. You therefore need to compare a range near the value you wish:
CPP / C++ / C Code:
if (pi > 3.14009 && pi < 3.14101)  // check if pi is about 3.141
This will check values close to your target value.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
Need help with my programs, please help. agentxx04 C Programming Language 1 23-Sep-2004 18:02
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06
Call a C program through Linux shell script nuwandee C Programming Language 3 29-Mar-2004 21:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.