Don't use void main() -- use int main().
Assignment.txt:
Code:
ASSIGNMENT 1
Emanuele Trucco .
1. THE PROBLEM
Calculating the value of ?, the ratio between circumference and diameter,
has been a classic problem of mathematics throughout history.
One of the many formulae proposed in modern times is an infinite series created in 1699 by the
English mathematician Abraham Sharp:
Write a program which computes and prints Sharp’s approximation of pi using a number of terms to be
input from the keyboard (via scanf ). The program must also print the difference between the
approximation and the value of pi stored in the macro constant M_PI, defined in <math.h>.
To help you in this first assignment, here is a sketchy pseudo-code from which you can develop your
code. The variable estimate is an accumulator, that is, a variable to which you keep adding iteratively.
We also use a variable pow3 in which we build increasing powers of 3 through the iterations, and a
variable sign which takes care of the alternating plus and minus.
1. Initialize: estimate = 1; pow3 = 1;
2. Input number of iterations, N
3. For (k=1 to N, step 1) {
4. if k is even, sign = 1; else sign = -1;
5. pow3 = pow3*3;
6. add k-th term to estimate, using sign and pow3;
7. }
8. Print estimate and (PI-estimate)
To decide whether k is even or odd, use the remainder function of C. Its operator is the percentage
sign, %: for example, 11%3 evaluates to 2, as 3*3=9 and 11-9 = 2. So, if val%2 is null, then val
is even.
You made a good start.
Code:
/*Code for iterations*/
N <= 80; /*number of iterations is less than or equal to 80*/
printf("Input number of iterations: ");
scanf("%d",&N);
printf("N = %d\n", N);
printf("root = %f\n", root);
}
That closing brace ends main(). You have code following that. You should put all of your code in main or other functions.
Code:
/*Code for iterations*/
N <= 80; /*number of iterations is less than or equal to 80*/
printf("Input number of iterations: ");
scanf("%d",&N);
printf("N = %d\n", N);
printf("root = %f\n", root);
} /*Code for iterations*/
N <= 80; /*number of iterations is less than or equal to 80*/
printf("Input number of iterations: ");
scanf("%d",&N);
printf("N = %d\n", N);
printf("root = %f\n", root);
This doesn't do anything. If you're trying to make sure N is <= 80, use an if statement and put it after you read the number in with scanf(). Something like this:
Code:
if(N > 80) exit(1);
root is uninitialized. You need to assign a value to it before you do anything like printing it.
My problems start at step 4 in the pseudo code
Code:
4. if k is even, sign = 1; else sign = -1;
You have a good start:
Code:
for (k = 1, k<N; k++)
if (k%2 == 0)
{ sign = 1; }
else
{ sign = -1; }
Your code does the same thing as the pseudo-code.
The next step is 5:
Code:
1. Initialize: estimate = 1; pow3 = 1;
2. Input number of iterations, N
3. For (k=1 to N, step 1) {
4. if k is even, sign = 1; else sign = -1;
5. pow3 = pow3*3;
6. add k-th term to estimate, using sign and pow3;
7. }
8. Print estimate and (PI-estimate)
That is C code. You can stick it right into your program:
Code:
for (k = 1, k<N; k++)
if (k%2 == 0)
{ sign = 1; }
else
{ sign = -1; }
->
Code:
for (k = 1, k<N; k++) {
if (k%2 == 0) {
sign = 1;
}
else {
sign = -1;
}
pow3 = pow3 * 3;
}
Step 6:
Code:
6. add k-th term to estimate, using sign and pow3;
Hint (I think):
Code:
estimate = k*sign*pow3;
Step 7 isn't a step. And step 8 you might be able to figure out.
Good luck!