- #1
fluidistic
Gold Member
- 3,923
- 261
So this isn't for homework. Basically I have a program in fortran 90 that computes an approximation of pi using a very non efficient method. So that it can do about 10^8 operations and still doesn't converge that much which is what I'm looking for.
I want to write this program in C too, but I've never written any program in C yet, not even the hello world. I want to compare the speed of both programs for the task of approximating pi via a product.
In fortran the code is
Executing it gives:
I'd like to have the same characteristics for the program in C. Namely, I want to be able to enter n whenever I execute the program.
I do not know how to assignate a value to a real number (float in C?) that can change. So #assignate wouldn't work I think. I'd need this in order to start the product, p must equal 1.
I've been through some pages of a tutorial on the internet, so far my program is:
I know the lines "m=1" and "p=1" are probably wrong. Any help is appreciated.
I want to write this program in C too, but I've never written any program in C yet, not even the hello world. I want to compare the speed of both programs for the task of approximating pi via a product.
In fortran the code is
Code:
program pi
implicit none
real (8) ::p
integer ::n,m
write(*,*)'Enter n please'
read(*,*)n
p=1d0
do m=1,n
p=p*((2d0*m)**2d0)/((2d0*m)**2d0-1d0)
end do
write(*,*)"The approximation of pi is",2*p
end program
Code:
Enter n please
1000000
The approximation of pi is 3.1415918681920378
I do not know how to assignate a value to a real number (float in C?) that can change. So #assignate wouldn't work I think. I'd need this in order to start the product, p must equal 1.
I've been through some pages of a tutorial on the internet, so far my program is:
Code:
#include <stdio.h>
using namespace std;
int n,m
float p
m=1
int main()
{
prinf ("Enter n.\n");
scanf( "%d", n );
getchar();
return 0;
p=1;
do{
} while(m<n);
}