I need help doing a c++ problem. Im required to do the following: You goal in th
ID: 3596886 • Letter: I
Question
I need help doing a c++ problem.
Im required to do the following:
You goal in this project is to create a program that evaluate the sin, cos, and tan of a given value of x and given value of n. The user should enter those two values interactively. Once the two values are entered, you would use the series representation, which is simply a looping structure,
My code is:
int factorial(int n)
{
int F = 1;
for (int i = 1; i < n; i++)
{
F = F*i;
}
return F;
}
int main()
{
float x;
int n;
cout << "Please enter the value of n (terms) " << endl;
cin >> n;
cout << "Please enter the value of x " << endl;
cin >> x;
double sine = 0;
double cosine = 0;
for (int j = 0; j < n; j++)
{
sine = (sine + pow(-1, j) / factorial((2 * j) + 1))*pow(x, 2 * n + 1);
}
for (int k = 0; k < n; k++)
{
cosine = cosine + pow(-1, k)*pow(x, 2 * k) / factorial(2 * k);
}
cout << "the value of sine is : " << sine << endl;
cout << "the value of cosine is : " << sine << endl;
}
When I run it, both sine and cosine give me the same result. I had both sine and cosine in the same loop but I was getting the same results.
What should I modify?
Explanation / Answer
I have checked the entire program according to complain given by the output is giving the same output because you wrote the cout stament by having sine as both the values please check the below two lines
cout << "the value of sine is : " << sine << endl;
cout << "the value of cosine is : " << sine << endl;
so i gave n=4 and x=5 i got the follwoing outpu with same values as you said before
output:
Please enter the value of n (terms)
4
Please enter the value of x
5
the value of sine is : 1.45519e+025
the value of cosine is : 1.45519e+025
*****************************************************************************************************
so if you put the cosine in the place of sine you will get two different values
cout << "the value of sine is : " << sine << endl;
cout << "the value of cosine is : " << cosine << endl;
output:
Please enter the value of n (terms)
4
Please enter the value of x
5
the value of sine is : 1.45519e+025
the value of cosine is : -50.0417
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.