Program that prints values of sin and cosines

I am working on an assignment for school that prints a table with the values for cosine and sin up to 360 degrees, as well as the degrees and radians in various step sizes.

Problem is that the values are wrong. The Radians keep starting at 1 and not 0.


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int step = 0;

cout << "Enter step size: " <<endl;
cin >> step;

if (step <= 0)
{
return 0;
}
else
{
cout << setw(10) << "degrees"
<< setw(10) << "radians"
<< setw(10) << "cos"
<< setw(10) << "sin"
<< endl;

for (double degrees = 0 ; degrees <= 360; degrees += step)
{
double radians = (degrees * M_PI) / 180.0;
cout << setw(10) << degrees
<< setw(10) << setprecision(5) << fixed << cos(radians)
<< setw(10) << setprecision(5) << fixed << sin(radians)
<< setw(10) << setprecision(5) << fixed << radians
<< endl;
}
}
return 0;
}

> The Radians keep starting at 1 and not 0.

The order in which the values are printed does not match the order in the heading.
Modify it so that the order is correct (degrees, radians, cos, sin)
1
2
3
4
5
6
cout << setw(10) << degrees
     << setw(10) << setprecision(5) << fixed << radians
     << setw(10) << setprecision(5) << fixed << cos(radians)
     << setw(10) << setprecision(5) << fixed << sin(radians)
     // << setw(10) << setprecision(5) << fixed << radians
     << endl;
Thanks, that fixed it!
Also, don't use doubles in a for loop - their representation is not exact. Instead work out how many times to loop as an integer and use that in the for loop.

M_PI / 180.0 is a constant value, so make it so:

constexpr double DegToRadians = M_PI / 180.0;


Edit: When dealing with doubles, I like to always put digits before and after the decimal point, as in 180.0 or 0.3 not 180 or .3. It just reinforces the idea that it is a double. And it can promote results of expressions to double.
Last edited on
Topic archived. No new replies allowed.