-
Help with a program
My prof. wantedme to write a C program that calculates the sine of x with a truncation error less than .000001. He also says that for each iteration of the series I should print out the iteration number, the calculated value, truncation error, and the C math librar calculated sine or x. I'm not sure whether I need the printf and the scanf that prompts the user to input a number, or what I should set myint equal to. If you can help in any way, I would greatly appreciate it. Thank you,
Kristina
#include <stdio.h>
#include <math.h>
main()
{
double iter, flipflop, num, fact, k, sinx, mynum;
num=1;
iter=1;
fact=1;
for(k=2; k<=iter; k++);
{
fact=fact*k;
}
do
{
pow(num,iter);
sinx=sinx + num/fact*flipflop;
printf("truncation error %f\n",sinx);
sin(num);
~~> printf("please enter a number\n");
~~>scanf("%d " , mynum);
printf("iteration number %f\n", iter);
printf("math library calculation %f\n",sin(num));
printf("the calculated value is %f\n",sinx);
iter=iter+2;
flipflop=-1;
}
while(num/fact <= .000001);
}
-
You and everyone else... Judging by the time difference between your post and this other one, I'd say your homework's about due now, huh?
http://www.cprogramming.com/cboard/s...threadid=10837
-
Well you've got most of the bits for the airplane, but it's no where near able to fly yet.
Some things
1. for(k=2; k<=iter; k++);
See that ; at the end?
This loop does exactly nothing, and since iter is 1 when it enters the loop, it does nothing zero times
2. {
> fact=fact*k;
> }
This isn't being controlled by the previous loop, so it happens just once. It wouldn't have happened at all, if you'd not put the trailing ; on the for loop.
3. unassigned results
> pow(num,iter);
> sin(num);
4. flipflop=-1;
So it starts off uninitialised, then it gets stuck at -1
I think you meant to
a) initialise it to 1
b) say flipflop = -flipflop;
-
Thanks Salem, but what do you suppose iter should be equal to??
-
A number you increment until
"with a truncation error less than .000001"