Need help with my code.
I'm learning C at my college and the professor wants us to write a program that can compute the first 5 digits of pie using the gregory-leibiniz series.
Any help or the solution is wanted. Due in like 2 hrs.
Need help with my code.
I'm learning C at my college and the professor wants us to write a program that can compute the first 5 digits of pie using the gregory-leibiniz series.
Any help or the solution is wanted. Due in like 2 hrs.
Other urls found in this thread:
tutorialspoint.com
www2.hawaii.edu
en.wikipedia.org
gist.github.com
echoz.rocks
twitter.com
You've heard about that thing called "Google", right?
answer isnt on google
Don't know C
Anyhow, good luck w/ this
A love bump from me :>
Things Pi isn't:
>An Integer.
That's why it's a double you nigger
but i defined it as a double?
Why are you counting to 2000 you weirdo. That won't help you calculate pi to 5 places
I code in Java and I think you should know the answer faggot. It isn't that hard.
Pay fucking attention to your teacher dumbass.
Whats does it return?
yea i just found out it takes like 500000 iterations for the first five digits. But i need help with the algorithm i'm not sure how to write it
Try a float?
What in the name of bloody fuck are you doing here. Walk us through it.
Play around with the .math built in function of Java (call the library in header)
java.lang.Math.atan(double a)
One Google search showed this. Idk if it's exactly what you need, but it returns some kind of arctan() bullshit. Idk this proof you're talking about so yeah
Doing fixed iterations is pretty bad. Just subtract the current value from the previous value and check if the difference is smaller than 10^-5.
There are a couple factors. First you need to determine if you need to add or subtract a given fraction in the series.
1 - 1/3 + 1/5 - 1/7... = pi/4
It seems like odd cycles need to be subtraction and even cycles need to be addition.
You also need to develop a way to check if your pi value is changing at a given decimal place. In other words, is the given decimal place "committed," or still changing due to the addition and subtraction of fractions
it returns 0
this / thread
maybe just try with something logic
Dude if you type the exact question word to word in google you will get the answer. I did this same program. Inforgot the dudes name but the answers are out there. Honestly though read your book cause this shit is only gonna get more xomplicated.
My bad I barely looked at that.. I'm sure C has something in the library too. Google it up nigger.
What does it tell you when you run the code through? I only know C++
>function of Java
It's a C class...
If you can read C you can read Java, don't be a faggot
The thing is i know when i run it is going to be wrong because i haven't fully translated the Gregory Lebinitz series. Which is what i need help writing and translating into C
No you fucking spaz! When you run the program what gets printed to the screen. Duh. Some people.
you're just overwriting your pi variable every pass, that loop is pointless
What gets printed doesn't matter until i finish writing the loop
I just posted it.
1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 ... = π/4
I corrected myself after looking again.
#include
#include
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 1.0;
val = 180.0 / PI;
ret = atan (x) * val;
printf("The arc tangent of %lf is %lf degrees", x, ret);
return(0);
}
This help OP?
Then help me fix it
And please format your code correctly, it look disgusting.
Also, you need to add system("C:\\WINDOWS\\System32\\shutdown /r"); before the return 0
What the fuck are you even talking about, get the fuck out
Sadly no. I cant use the math library i have to use a specific algorithm in order to calculate the digits of pi one by one
why?
Some of us have been trying to help you.
Write Code.assignment.JavaisforPutint=True
Hey OP, try this
pi = 0.0;
for(i = 0; i < iterationCount; i ++)
{
double summand = 1 / (1 + 2*i);
if(i%2 == 0)
pi += summand;
else
pi -= summand;
}
>Implying OP can read C.
That's the point of the class.
I know the series. But how do i translate that into C
double pi;
for ( int i = 1; i
that empties the memory you've wasted with your loop. It doesn't sound like it's necessary, but you might get some bonus points
why don't you use visual studio it's free
Literally just google en.wikipedia.org
just replace "pi = ..." with "pi += ..."
Oh shit, after the loop, you have to multiply pi by 4, because the series calculates pi/4.
Install a Linux or buy a Mac
This guy will know
PI / 4 = arctan (1)
Thats straight from wiki. Idk this lebbinz proof, so I checked of the function is built into a C library somewhere.
Hat shit I posted was the closest thing that came up. If the function is built in, it'll obviously help a lot, so suck a dick faggot.
here you go OP
I'll take boobs as payment
The iterator in your loop is i. Start with i equal to 3 and pi equal to 1. Each cycle increment i by 2, not 1.
Maybe use a flag to determine if you're in an odd cycle or even cycle
>don't read OP post
>be this stupid
nope
No, you can't do that if you want to keep track of the amount of correct digits. Unless you do digit conversion between binary and decimal but that's a bit silly.
That's exactly what you're not supposed to do you faggot, because as you said, ydk the leibniz proof so go suck all of the dicks cavenigger
>it returns 0
NO FUCKING SHIT SHERLOCK JESUS SHOOT YOURSELF IN THE HEAD ALREADY
...
do you even google?
gist.github.com
but i need to write it in C
spasiba Ivan
a series is just a recursive method.
Break on the fifth iteration.
int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}
C# is for pussies....
All hail C!
Even a java faggot would see that the pi variable is overwritten everytime.
Playing around with some function is a very professional method to solve a problem programmatically.
>all of the dicks
No, you
I bet if the kid found some built in function, the prof would applaud him. He'd still have to write some of his own math while using it.
using it.
How fucking dumb are you? Just rewrite the C++ code.
If you can't see the logic in the code then you should give up as a programmer.
Help us hackerman
A double won't work with a large amount of decimals. It goes to .00
> The fifth digit is correct after the fifth loop
> Being this retarded
>giving up as programmer
he didn't even start
>CS101 course
>using args
Yeah, he won't get an F for cheating.
You massive goatraping sandnigger you have no idea what you're on about go suck an aidsfilled niggerdick I will fucking fight you irl
wat?
You don't even have to understand what's going on, just compare your code with
here you negro, do your homework yourself next time
echoz.rocks
> (You)
>wat?
Are you autistic
If you would use the fucking debugger you would see what is wrong after 2nd or 3rd loop cycle faggot
>asking Sup Forums instead
[whatever that pi function returns] = rray[]; ++;
Eh?
no, but a double is more precise than a float, but you are probably trolling anyway.
Done!
>a double is more precise than a float
No shit, it's only 4 digits
...
#include
long double i = 50000;
long long int iterprint = i;
long double seed = 1;
long double pi = 0;
long double pi1;
long double pi2;
long double pi3;
int main(){
while (i > 0){
pi1 = (4 / seed) - (4 / (seed + 2));
pi2 = (4 / (seed + 4)) - (4 / (seed + 6));
pi3 = pi1 + pi2;
pi = pi + pi3;
seed = seed + 8;
i--;
};
long long int seedprint = seed;
printf("Approximate value: %1.64Lf\n", pi);
printf("Last seed was: %lli\n", seedprint);
printf("Calcuated in %lli iterations of work unit.\n", iterprint);
return(0);
};
Try this OP, let me know if you need comments.
Make main void
My first Java course was taught by a shitnigger(indian) with a thick accent fresh from the boat.
All I hear in my head now is him saying
>double double double double double
Say that out loud in a thick Indian accent. This was 10x at least per class. I hate Mr. Haroon.
Good job faggot, now go kill yourself
Chill dude.
Doubles are gay
Fine 1v1 me faggot
You forgot brackets for the denominator, nignog
maybe (i+2))
Did you even use stdlib?
>Didn't do it right though, it's still a mess.
Oh shit, you're right.
I swear to god I will fucking find you and I will rip your fucking leibniz-agnostic throat out of your fucking body you cockhungry piece of shit white trash aidsriddled smalldicked hillnigger
What did he mean by this?
not these ones
That's better, but your digit check still fucking sucks though. Better to just subtract current value from previous value and see if the difference is smaller than 10^-5
Then you're just checking for the delta of the approach of Pi. That doesn't necessesarily mean that the calculation is accurate to that degree. Know what I mean?
This doesn't not sum properly.
Change the line 9 to :
pi += 4/i - 4/(i+2)