Add example inputs
This commit is contained in:
42
examples/taylor/taylor.mc
Normal file
42
examples/taylor/taylor.mc
Normal file
@@ -0,0 +1,42 @@
|
||||
float taylor_approx(int n) {
|
||||
float signed_one;
|
||||
signed_one = 1.0;
|
||||
float pi;
|
||||
pi = 0.0;
|
||||
int i;
|
||||
i = 1;
|
||||
float j;
|
||||
j = 1.0;
|
||||
|
||||
/* summation loop i goes from 1 to n */
|
||||
while(i <= n) {
|
||||
/* add next term to sum */
|
||||
pi = pi + (signed_one/((2.0*j) * (2.0*j+1.0) *(2.0*j+2.0)));
|
||||
|
||||
/* reverse sign for next term */
|
||||
signed_one = -signed_one;
|
||||
i = i + 1;
|
||||
j = j + 1.0;
|
||||
}
|
||||
|
||||
/* finishing touches */
|
||||
pi = 4.0 * pi + 3.0;
|
||||
|
||||
return pi;
|
||||
}
|
||||
|
||||
int main() {
|
||||
print("Enter a number to obtain the n Taylor series for pi: ");
|
||||
int n;
|
||||
n = read_int();
|
||||
print_nl();
|
||||
float pi;
|
||||
|
||||
pi = taylor_approx(n);
|
||||
|
||||
print("Aproximation of pi: ");
|
||||
print_float(pi);
|
||||
print_nl();
|
||||
|
||||
return 0;
|
||||
}
|
1
examples/taylor/taylor.stdin.txt
Normal file
1
examples/taylor/taylor.stdin.txt
Normal file
@@ -0,0 +1 @@
|
||||
4
|
2
examples/taylor/taylor.stdout.txt
Normal file
2
examples/taylor/taylor.stdout.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Enter a number to obtain the n Taylor series for pi:
|
||||
Aproximation of pi: 3.14
|
Reference in New Issue
Block a user