C program, very basic

Solaris

Party Escort Bot
Joined
Feb 11, 2005
Messages
10,318
Reaction score
4
Hey guys,
I'm trying to write a C program to approximate sin x, function, using the Maclurin expansion.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
double sin1(double x);
int main(void)
{
double x;
double answer;
printf("Sinx, x=...");
scanf("%lf", x);
answer = sin1(x);
printf("%1f", answer);

double sin1(double x)
{
double x3 = (x*x*x)/(3*2);
double x5 = (x*x*x*x*x)/(5*4*3*2);
double x7 = (x*x*x*x*x*x*x)/(7*6*5*4*3*2);
sin1 = x-x3+x5-x7;
printf("sin x = %lf", sin1;
return(sin1);
}
}



The first error message is for line 19,(4 from bottom), the line that says sin1= x-x3+x5-x7. Could anyone tell me whats wrong and correct it? I am very knew to C and need to finish this urgently :(

Thanks helplife2.net

:D:D
 
Okay, improved that code, still doesnt work however:

#include <stdio.h>
#include <stdlib.h>
double sin1(double x);
int main(void)
{
double x;
double answer;
printf("Sinx, x=...");
scanf("%lf", x);
answer = sin1(x);
printf("%1f", answer);

double sin1(double x)
{
double sin1x;
double x3 = (x*x*x)/(3*2);
double x5 = (x*x*x*x*x)/(5*4*3*2);
double x7 = (x*x*x*x*x*x*x)/(7*6*5*4*3*2);
sin1x = x-x3+x5-x7;
printf("sin x = %lf", sin1x);
return(sin1x);
}
}

now it generates a new error on line 10, undefined reference to sin1
 
Why are you expanding so many constants and multiplications of x.
 
I worked it out was all very fun.

I multiplied x's becuase I was writing out x^2 as X*X and so on
 
Use the pow() function in math.h:

pow(x,3) and so on.

Ideally you should use loops, but you seem like a beginner.
 
Post the finished program for future reference, or something.

Yes there are a few ways the program could have been more efficient, but that wasn't the point of the thread and understanding > efficiency especially if you are having problems.

Why would a loop have been "ideal"?
 
It looks like your {}s are wrong. They way it is now, the sin1 function is nested in main. I think you want:

Code:
#include <stdio.h>
#include <stdlib.h>
double sin1(double x);
int main(void)
{
double x;
double answer;
printf("Sinx, x=...");
scanf("%lf", x);
answer = sin1(x);
printf("%1f", answer);
}

double sin1(double x)
{
double x3 = (x*x*x)/(3*2);
double x5 = (x*x*x*x*x)/(5*4*3*2);
double x7 = (x*x*x*x*x*x*x)/(7*6*5*4*3*2);
sin1 = x-x3+x5-x7;
printf("sin x = %lf", sin1); // you were missing a ')' here
return(sin1);
}

Also, it's better to not have the long strings of multiplications (you should use x*x for x squared; x*x*x for x cubed is arguable; beyond that take a different approach). In this case there's a better answer than using pow().

Code:
double sin1(double x)
{
double x2 = x*x;
double xpower = x * x2;  // xpower = x^3

double x3term = xpower/(3*2);
xpower *= x2; // xpower = x^5
double x5term = xpower/(5*4*3*2);
xpower *= x2; // xpower = x^7
double x7term = xpower/(7*6*5*4*3*2);

double answer = x - x3term + x5term - x7term;

//printf("sin x = %lf", sin1);  // just do computation in this function.  you already print it out in main
return answer;
}

(And yes, in a perfect world this would be rolled into a loop. Also, in a perfect world, you would derive the value of sin(x) outside the range [-pi/2, pi/2] from an evaluation of it inside that range.)

edit: I should look more closely at how old a thread is before responding. This is a slow enough forum that a month-old thread was still among the top listed.
 
Back
Top