# C query (high difficulty) - tricky

Hi

I am teaching myself C and have the following question:

Calculate the value of pi from the following series:

pi = 4-4/3+4/5-4/7… etc

Print a table that shows the value of pi. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159? ow many terms

The code that I have developed is as follows and prints an infinite series. However, I am unclear how and when to flag that we have reached 3.141? 3.141

Any suggestions? thx

``````#include <stdio.h>
int main(void)
{
float pi=0, b=4, i=3, x=1.0
int y=0;

pi = b-b/i;
printf("%21s", "pi");
printf("%21s", "i");
printf("%21s", "y");

while (pi!=3.14)
{
for (i=5;; i+=2)
{
if(x==1.0)
{
pi+=b/i;
printf("%21.6lf", pi);
pirntf("%21.6lf", i);
y+=1;
printf("%21d\n);
x*=-1;
} // end if

else
{
pi = pi-b/i;
printf("%21.6lf", pi);
printf("%21.2lf", i);
y+=1;
printf("%21\d", y);
x*= -1.0;
} // end else
} // end fo
} // end while
} // end main
``````

It has been many years ago since I last dabbled in C.

Try pausing the program using getc(stdin);

Output:

``````     pi          i          y
===============================================
if:      3.466666     5.000000     -1.000000

if - pause

else:      2.666667     5.000000          2
if:      3.238095     7.000000     -1.000000

else:      2.793651     9.000000          4
if:      3.593651     5.000000     -1.000000

if - pause
else:      2.793651     5.000000          6
if:      3.365079     7.000000     -1.000000

else:      2.920635     9.000000          8
if:      3.720635     5.000000     -1.000000

if - pause

else:      2.920635     5.000000         10
if:      3.492063     7.000000     -1.000000

``````

Source code: ``` /* Tested on Linux Ubuntu: compile: gcc -o test-2-delete calculate-pi.c run: ./test-2-delete */

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

int main(void)
{
float pi=0, b=4, i=3, x=1.0;
int y=0;
pi = b-b/i;

printf(“\n\t %s \t\t %s \t\t %s”, “pi”, “i”, “y”);
printf(“\n\t ===============================================”);

while (pi !=3.14)
{
for (i=5; i<10; i+=2)
{
if(x==1.0)
{
printf(“\n if: “);
pi += b/i;
y += 1;
printf(”\t %.6lf”, pi);
printf(“\t %.6lf”, i);
printf(“\t %.6lf\n”, x*=-1);

``````    if(pi >= 3.4){
printf("if - pause");
getc(stdin);
break;
}

} else {
printf("\n else: ");
pi  = pi-b/i;
y  += 1;
x  *= -1.0;
printf("\t %.6lf", pi);
printf("\t %.6lf", i);
printf("\t %6d",   y);

if(pi >= 3.142){
printf("\t else = pause");
getc(stdin);
break;
}
}//endif else

}//endfor
``````

}//endwhile

printf(“\n\n Thats’ All Folks \n”);
} // end main

Hi John

Thx so much for your time.

A couple of quick questions, I am only at studio.h why all the other libraries?

Also if I understand it, you are reading the output as characters and not numbers (thus bypassing the difficulty of rounding decimals that are binary numbers) and then flagging when a character is read?

Also are we not able to just round the number in the decimal system and flag it that way?

Ps this seems to bea common college question from my google searches, I am surprised that the answers available are so mixed on this one🚁

Thx!
Karen

C is kind of like riding a horse when you have an automobile. Especially for web development. Good stuff to know though starting out.

The libraries were copied and pasted from an example and may not be required. Try remming the library and see if the script still compiles.

The original script was modified because of misspelling a function name. The executable results flew off the screen and impossible to understand what was happening. The getc(); function paused output and slight changes could be made to see their effects, also to test where and when to break execution once the desired result was established.

I was unsure of the numeric output format required.

Maybe more like a fast, lean, mean racehorse instead of an old bus.

Yes the number of substantive world class c based programs rather contradicts the previous statement. Agreed

I have cracked it.

Pi posed a number of unique problems: from what I can see it only ever approaches - never reaches the number so = is out

Also if you look at the equation the resultant is oscillating above and below our target value which also poses some problems

My solution was to put Pi between a pair of < Pi < signs with the values being to an appropriate figure when rounded to the desired significant figures and then to put a break and print statement (basically a simple if statement under the first two)

It does solve the problem as stated in the question although I am certain it is only one approach among a number of options.

I liked it because it is easy and uses very simple programming tools and statements.

I’ll drop the actual code when I get on a desktop / laptop in he next day or so.

Now I have to figure out a permutation problem - thanks for your help. I hope you like my solution as well

Karen

1 Like

In that analogy the bus is more productive than the race horse. A race horse can only carry at most two people. However, the bus can move 20+ times that at once. Not to mention that bus lacks physical limitations of a living creature.

1 Like
``````#include <stdio.h>
int main(void)
{
double pi=0, b=4.0, x=1.0;
int y=0, i=3;

pi=b-(b/3.0);
printf("%21s", "pi");
printf("%21s", "i");
printf("%21s\n", "y");

for (i=5;;i=i+2)
{
if(x==1.0)
{
pi +=b/i;
printf("%21.2lf", pi);
printf("%21d", i);
y+=1;
printf("%21d\n", y);
x*=-1;
} // end if

else
{
pi -=b/i;
printf("%21.2lf", pi);
printf("%21d", i);
y+=1;
printf("%21d\n", y);
x*= -1.0;
//printf("%s\n", "hello"); // test assistance - remove this for production
} // end else
if (3.135<pi && pi<3.145)
{
printf("The number of iterations is: %d", y);
break;
} //end if
} // end for
} // end main
``````
2 Likes

So, I used the greater than / less than symbols and set my desired number of significant figures to round to.

That is pretty much it.

You can also set the number of iterations of ‘y’ should you so wish.

thanks
Karen

1 Like

Here i my Version: 002

``````/*
#Linux Command line

# GOTO DIRECTORY
cd ~/www/c-files
# COMPILE:
gcc -o test-2-delete  calculate-pi-002.c
# RUN:
./test-2-delete

# ALL IN ONE
clear; cd ~/www/c-files; gcc -o test-2-delete  calculate-pi-002.c; echo; ./test-2-delete; echo ;
*/

#include <stdio.h>
#include <math.h> // value of M_PI

int main(void)
{
int    y=0, i=3;
double pi=0, b=4.0, x=1.0;
double tolerance = 0.05;
char aHeading[]  = "\n\r\t pi \t\t i \t\t y";
char aLine[]     = "\n\r\t =================================";
char aFormat[]   = "\n\r\t %3.3lf \t%10d \t%10d";

pi=b-(b/3.0);
// printf("%21s", "pi");
// printf("%21s", "i");
// printf("%21s\n", "y");
printf("%s", aLine);
printf("%s", aLine);

for (i=5;;i=i+2)
{
if(x==1.0)
{
pi += b/i;
y  += 1;
x  *= -1;
printf(aFormat, pi, i, y);
}else{
pi -= b/i;
y  += 1;
x  *= -1.0;
printf(aFormat, pi, i, y);
// printf("%s\n", "hello"); // test assistance - remove this for production
} // end else

// if (3.135<pi && pi<3.145)
if (pi > M_PI-tolerance  &&  pi < M_PI+tolerance)
{
printf("%s", aLine);
printf("\n\n\t%s \t%1.5f", "Tolerance:",  tolerance);
printf("\n\t%s   \t%1.5f", "Actual PI:",  M_PI);
printf("\n\t%s   \t%1.5f", "Calculated:", pi);
printf("\n\t%s   \t%1.5f", "Difference:", M_PI-pi);
printf("\n\t%s   \t%d",    "Iterations:", y);
printf("\n\n\t%s %s",    "Useful site:", "https://learnxinyminutes.com/docs/c/");
printf("%s", aLine);
break;
}//end if

} // end for

} // end main
``````

Output:

``````	 =================================
pi 		 i 		 y
=================================
3.467 	         5 	         1
2.895 	         7 	         2
3.340 	         9 	         3
2.976 	        11 	         4
3.284 	        13 	         5
3.017 	        15 	         6
3.252 	        17 	         7
3.042 	        19 	         8
3.232 	        21 	         9
3.058 	        23 	        10
3.218 	        25 	        11
3.070 	        27 	        12
3.208 	        29 	        13
3.079 	        31 	        14
3.200 	        33 	        15
3.086 	        35 	        16
3.194 	        37 	        17
3.092 	        39 	        18
=================================

=====================
Tolerance:     0.05000
Actual PI:     3.14159
Calculated:    3.09162
Difference:    0.04997
Iterations:    18

Useful site: https://learnxinyminutes.com/do
=====================

``````

Clever fellow:). I’m only on ch 5 of the Deitel learn C text.

By the way, would you happen to know if an open source video teaching solution mobile and iOS compatible?

1 Like

I do not know any specific mobile video teaching solutions.

Try searching for Youtube mobile “learn C”.

What platform are you using to learn C?

I really like how it is so easy to create a C application using Linux as you can see from the remmed statements at the top of my source code. Just one command line to compile and run the program. Ctrl-C to quit if on the off-chance the program runs amok

Hi sorry i wasn’t clear I’m looking for a mobile app to embed Ina program I am considering on developing. It would be an open source video conferencing program good for teaching (like Big Blue button) but iOS compatible.

I am using Linux Fedora 23. Yes, I love my Linux and have been gradually weaning myself off Microsoft.

OK, looks like your post would be better if it was moved to the Mobile forum.