I have doubts with this task if I can program in php but in c++ I am just learning, the program shows me an error in the function
xalan(x,i) 15 24 C:\Users\admin\Documents\task8\9\main.cpp [Error] 'xalan' was not declared in this scope
#include <iostream>
#include <string>
#include <stdio.h>
//sin(x)=x-x^3/(3!)+x^5/(5!)+...+(-1)^n(x^(2n+1))/((2n+1)!)
int main(int argc, char** argv) {
float x,sinx,sinx2;
printf("\nprograma para calcular sin(x)");
printf("\nintrodusca el angulo x en radianes \n");
scanf("%f",&x);
sinx=x-(x*x*x)/(3*2)+(x*x*x*x*x)/(5*4*3*2)-(x*x*x*x*x*x*x)/(7*6*5*4*3*2)+(x*x*x*x*x*x*x*x*x)/(9*8*7*6*5*4*3*2);
printf("\nsin(x)=%f ",sinx);
sinx2=x;
for(int i=1;i<10;i++)
sinx2=sinx2+xalan(x,i);
printf("\nsin2(x)=%f ",sinx2);
}
float xalan(float x,int n)
{ //(-1)^n(x^(2n+1))/((2n+1)!)
int fact=2*n+1;
float xn=1;
for(int i=(2*n);i>0;i--)
fact=fact*(i);
for(int i=1;i<=n;i++)
xn=xn*(-1)*(x*x);
xn=xn*x;
return xn/(fact);
}
Move the xalan function declaration up a before main.
The problem is that xalan has not been loaded by the program when you are trying to call it.
You need the prototype of the function, it is declared before the main like this:
Prototypes are necessary in c++ if functions are to be declared below main. If they are going to be done before main, a prototype is not necessary.