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
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🚁
Pls help me understand your answer a little more fully.
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.
Were you able to adjust your script and achieve your goal?
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
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.
#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
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.