# Trying to count the iterations to get certain decimal places of pi.

Page 1 of 1

## 8 Replies - 606 Views - Last Post: 28 October 2013 - 10:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332254&amp;s=dcc9e67c8616b98cd5c5bb2d92f26bbe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DDNPresident

Reputation: 1
• Posts: 23
• Joined: 23-October 13

# Trying to count the iterations to get certain decimal places of pi.

Posted 23 October 2013 - 10:40 AM

I am a beginner in C programming. I am a student at FIU and need a little help. For some reason I can't get this program to stop at the value I need and print it then in the next column show the amount of iterations it took to arrive at that amount.

Example ouput:

3.14 2345 times
3.141 xxxx times
3.1415 xxxx times

Any help would be greatly appreciated. I don't need anyone to actually do the code, just give me a hint at what I am doing wrong. My logic is obviously off. I will get a response, but all of the amount for the counters match exactly on all values.

Here is what I have so far:

```//
//  main.c
//  Homework4.26
//
//  Created by Christopher Mood on 10/13/13.
//

#include <stdio.h>
#include <stdlib.h>

int main()

{
int i1, i2, i3, i4;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
float pi1, pi2, pi3, pi4 = 4.0;

printf(" \t\t\t\t  Calculating the Value of π \n\n");
printf("Number\tIterations\n\n");

for ( i1 = 3; i1 <= 100000; i1 += 2) {
if ( counter1 % 2 == 0 )
pi1 -= 4.0 / (double)i1;

else   {
pi1 += (4.0 / (double)i1);

counter1++;

put ((pi1 = 3.14));
printf("%f\t%d\n\n",pi1,counter1);

for ( i2 = 3; i2 <= 100000; i2 += 2) {
if ( counter2 % 2 == 0 )
pi2 -= 4.0 / (double)i2;

else
pi2 += 4.0 / (double)i2;

counter2++;

for ( i3 = 3; i3 <= 100000; i3 += 2) {
if ( counter3 % 2 == 0 )
pi3 -= 4.0 / (double)i3;

else
pi3 += (4.0 / (double)i3);

counter3++;

for ( i4 = 3; i4 <= 100000; i4 += 2) {
if ( counter4 % 2 == 0 )
pi4 -= 4.0 / (double)i4;

else
pi4 += 4.0 / (double)i4;

counter4++;

if ((pi1 = 3.14)){
printf("%f\t%d\n\n",pi1,counter1);
}
if ((pi2 = 3.141)){
printf("%f\t%d\n\n",pi2,counter2);
}

if ((pi3 = 3.1415)){
printf("%f\t%d\n\n",pi3,counter3);

}
if ((pi4 = 3.14159)){
printf("%f\t%d\n\n",pi4,counter4);
}
}
}
}

}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Trying to count the iterations to get certain decimal places of pi.

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,995
• Joined: 30-May 10

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 23 October 2013 - 12:12 PM

Two things spring to mind.
1. Choose a known function for approximating PI.
http://en.wikipedia....tions_of_%CF%80
It's far from clear what you're actually trying to do. One thing would be to cite some reference to indicate what you're trying to do.

2. Learn to format code.
```//
//  main.c
//  Homework4.26
//
//  Created by Christopher Mood on 10/13/13.
//

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i1, i2, i3, i4;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
float pi1, pi2, pi3, pi4 = 4.0;

printf(" \t\t\t\t  Calculating the Value of π \n\n");
printf("Number\tIterations\n\n");

for (i1 = 3; i1 <= 100000; i1 += 2) {
if (counter1 % 2 == 0)
pi1 -= 4.0 / (double) i1;
else {
pi1 += (4.0 / (double) i1);

counter1++;

put((pi1 = 3.14));
printf("%f\t%d\n\n", pi1, counter1);

for (i2 = 3; i2 <= 100000; i2 += 2) {
if (counter2 % 2 == 0)
pi2 -= 4.0 / (double) i2;
else
pi2 += 4.0 / (double) i2;

counter2++;

for (i3 = 3; i3 <= 100000; i3 += 2) {
if (counter3 % 2 == 0)
pi3 -= 4.0 / (double) i3;
else
pi3 += (4.0 / (double) i3);

counter3++;

for (i4 = 3; i4 <= 100000; i4 += 2) {
if (counter4 % 2 == 0)
pi4 -= 4.0 / (double) i4;
else
pi4 += 4.0 / (double) i4;

counter4++;

if ((pi1 = 3.14)) {
printf("%f\t%d\n\n", pi1, counter1);
}
if ((pi2 = 3.141)) {
printf("%f\t%d\n\n", pi2, counter2);
}

if ((pi3 = 3.1415)) {
printf("%f\t%d\n\n", pi3, counter3);

}
if ((pi4 = 3.14159)) {
printf("%f\t%d\n\n", pi4, counter4);
}
}
}
}
}
}
}

```

Can you see now how your inner loop is going to run 1E20 times!

If you have that many iterations, you don't need a calculation. Simple trial and error will eventually stumble on a good approximation of PI.

### #3 Peter O

Reputation: 94
• Posts: 229
• Joined: 19-October 13

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 23 October 2013 - 01:06 PM

Salem_c, on 23 October 2013 - 12:12 PM, said:

If you have that many iterations, you don't need a calculation. Simple trial and error will eventually stumble on a good approximation of PI.

And how would you know that it's a good approximation?

### #4 DDNPresident

Reputation: 1
• Posts: 23
• Joined: 23-October 13

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 23 October 2013 - 01:50 PM

Salem_c, on 23 October 2013 - 12:12 PM, said:

Two things spring to mind.
1. Choose a known function for approximating PI.
http://en.wikipedia....tions_of_%CF%80
It's far from clear what you're actually trying to do. One thing would be to cite some reference to indicate what you're trying to do.

2. Learn to format code.

Can you see now how your inner loop is going to run 1E20 times!

If you have that many iterations, you don't need a calculation. Simple trial and error will eventually stumble on a good approximation of PI.

Well i'm using the infinite loop 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...

1.) I was trying to get it to stop at certain values when it lands on them and then prints the number of times you had to run through the loop to arrive at the number. obviously the least amount of times should be at 3.14 and then it would increase as you get into the thousandth, ten-thousandth, and one hundred-thousandth place. My goal is to get the counter without losing it when it checks for the next decimal value of pi. When it gets to the number it simply puts 4999 as each counter value.

2.) My code was all screwed up because I kept trying to revise my program and failed to organize it before I attached the code to this post. Either way, my goal is just some insight on a possible solution. Thank you for your input. I have searched and have yet to find one that just prints the number of times it ran to arrive at the certain decimal values in a simple table.

### #5 DDNPresident

Reputation: 1
• Posts: 23
• Joined: 23-October 13

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 23 October 2013 - 02:47 PM

```#include <stdio.h>
#include <string.h>

int equal( double v, const char *match ) {
char buf[20];
int len = strlen( match );
sprintf( buf, "%.14lf", v );
buf[len] = '\0';
return strcmp( buf, match );

}

int main() {
int i, t0, t1;
double sum= 0.0;
static const char * const match[]= { "3.14", "3.141", "3.1415", "3.14159", NULL };
for ( t0= t1= 0, i= 1; match[t0] != NULL || match[t1] != NULL; i += 2 ) {
double term= (((i-1) % 4) == 0? 4.0 : -4.0) / i;
if ( match[t0] != NULL && equal( sum + term, match[t0] ) == 0 )
printf( "Sum matched %s after adding term %d\n", match[t0++], (i+1) >> 1 );
sum += term;
}
return 0;
}

```

I believe this is what I am looking for... I found this online. But I am not sure how this is even done at this point in my knowledge of C. It seems correct though. Is there something similar that can be done using code like what I have originally used?
Output:

Sum matched 3.14 after adding term 119
Sum matched 3.141 after adding term 1688
Sum matched 3.1415 after adding term 10794
Sum matched 3.14159 after adding term 136121

### #6 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,995
• Joined: 30-May 10

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 24 October 2013 - 12:07 PM

> Well i'm using the infinite loop 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...
I suggest you throw away what you have, and concentrate on just writing a loop which evaluates each successive term.

> My code was all screwed up because I kept trying to revise my program and failed to organize it before I attached the code to this post.
Well if you continue to post crap, people will ignore you / give you low grades / fire you from your job.

Besides, when you start digging yourself into a hole, the best thing to do is STOP and think about what you're actually trying to do.

Random hacking won't solve your problem if you don't understand the problem to begin with.

> I believe this is what I am looking for... I found this online.
Sure, but it doesn't implement the series you planned to implement.

Also (if you were planning to hand it in), if you found it using google, then so can your tutors'.
If they have any academic integrity, you'd be kicked out of college.

### #7 DDNPresident

Reputation: 1
• Posts: 23
• Joined: 23-October 13

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 25 October 2013 - 09:27 AM

Salem_c, on 24 October 2013 - 12:07 PM, said:

> Well i'm using the infinite loop 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)+...
I suggest you throw away what you have, and concentrate on just writing a loop which evaluates each successive term.

> My code was all screwed up because I kept trying to revise my program and failed to organize it before I attached the code to this post.
Well if you continue to post crap, people will ignore you / give you low grades / fire you from your job.

Besides, when you start digging yourself into a hole, the best thing to do is STOP and think about what you're actually trying to do.

Random hacking won't solve your problem if you don't understand the problem to begin with.

> I believe this is what I am looking for... I found this online.
Sure, but it doesn't implement the series you planned to implement.

Also (if you were planning to hand it in), if you found it using google, then so can your tutors'.
If they have any academic integrity, you'd be kicked out of college.

Extra Credit points to an assignment. Not that serious.

Also, I know how to run through the loop using the series. I just needed a way to get it to output each time it hit that value of the approx. of pi.

To address your other concern of copying things from online... Did you see this used as my own code? Did I claim it to be mine? I don't know what happens in your brain when I referenced where I received it from. It was just an output example of the same CONCEPT! You're reading skills are lacking or you simply enjoy posting something rather rude. Either way, even though you did not point me in the right direction or even address the concern I appreciate your response either way. Decided not to do the extra credit portion and just stick with the A in the original assignment minus the bonus points. If there is anyone who can point me in the right direction just for understanding it would be appreciated. Thank you.

### #8 DDNPresident

Reputation: 1
• Posts: 23
• Joined: 23-October 13

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 28 October 2013 - 07:42 PM

I revised my code getting the approximation of pi using the infinite series as follows...

```
#include <stdio.h>

int main( void )
{
long double pi = 0.0;
long double number = 4.0;
long double dnom = 1.0;
long int counter;
long int accuracy = 500000;

printf( "term #\t\t pi\n" );

for ( counter = 1; counter <= accuracy; counter++ ) {

if ( counter % 2 != 0 ){
pi += number / dnom;
}

else {
pi -= number / dnom;
}
printf( " %ld\t\t %Lf\n", counter, pi );

dnom += 2.0;

}

}

```

Any help would still be appreciated, its not for a grade any longer, I just cant figure out how to do it! I have tried to do an if (pi == 3.14) printf counter ,pi

### #9 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,995
• Joined: 30-May 10

## Re: Trying to count the iterations to get certain decimal places of pi.

Posted 28 October 2013 - 10:14 PM

First, lets declare a couple of extra variables.
```long double previousPi = 0.0;
long double errorTerm = 1.0;
```

In the loop, you would do something like
```if ( fabs(pi-previousPi) < errorTerm ) {
// print pi, and how many iterations it took
errorTerm /= 10.0;  // now wait for the next digit to stabilise
}

```

I'll leave you to decide where to assign previousPi