Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<cmath> using namespace std; int nfact=1, k, x; doubl

ID: 3657211 • Letter: #

Question

#include<iostream>

#include<cmath>

using namespace std;


int nfact=1, k, x;
double sinvalue,j,d, sin = 0;
for(int i = 0; i<10; i++){

j = pow(-1,i);
k=( (2*i)+1);
d= pow(x,k);

nfact=1;

do
{

nfact = nfact*k;
k--;
}while(k > 0);

sinvalue=(( j)/nfact)*d;
sin= sinvalue +sin;
}

cout<<"The sin value is "<<sin<<endl;

}


int main(){

Please help me debug my code so that it outputs the correct value for sine in radians when the user inputs the angle. I intend to use cmath library for pow function only. when I enter the angle wrong results are outputed i.e if I input 2 for example I get 0.904048 radians instead of 0.909297 radians. The error increases as I input larger angles. what am i doing wrong?

Explanation / Answer

#include #include #define PI 4*atan(1.0) main() { double x_deg, x, Y, y1, y2, n_fact; int ask1, ask2, n_term, k, i; do { printf(" Enter x_deg: "); scanf("%lf", &x_deg); x = x_deg * (PI/180.); Y = pow(sin(x), 2); printf("True value of sin^2(x) = %g ", Y); printf("n_term approximation of sin^2(x) "); do { printf("Enter number of terms: "); scanf("%d", &n_term); printf("%d term approximation ", n_term); y1 = 1.0; for(k=n_term;k>0;k--) { n_fact = 1.0; for(i=(2*k)+1;i>0;i--) { n_fact *= i; } y1 += (pow(-1,k)/(n_fact+1))*(pow(x,(2*k)+1)); y2 = pow(y1, 2); } printf("sin^2(x) = %g ", y2); printf("Relative Error = %g percent ", 100*(y2-Y)/Y); printf("Do you want a new n_term: y=1 or n=0 "); scanf("%d", &ask2); } while(ask2!=0); printf("Do you want a new x_deg: y=1 or n=0 "); scanf("%d", &ask1); } while(ask1!=0); }