Displaying outputs in a table
Feb 7, 2016 at 7:29pm UTC
(I decided to make this a new question since I no longer need help with the math of the code) I've managed to get the correct values for the trig functions I need to display. However, now I'm struggling with the outputs.
I am supposed to have a table that looks like this:
https://i.gyazo.com/8cc08111ddedc8533165d9ec06134aac.png
and I do not know how to do this. Also, while my code sometimes displays 'inf' and '0' where it should for the functions, other times it chooses to display them as very large or very small exponents instead. How can I modify the outputs to display properly?
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <iostream>
#include <cmath>
double mySine (double x);
double myCosine (double x);
double factorial (int f);
double angleInRadians (double x);
double sine(double x);
double cosine(double x);
double tangent(double x);
double cotan(double x);
double secant(double x);
double cosecant(double x);
const double PI = atan(1.0)*4.0;
using namespace std;
int main(){
double out;
for (int i=0;i<=360;i+=15){
cout << "\nDegrees \n" <<i;
out = mySine( angleInRadians( (double )i ) );
cout << "Sin " <<out<<"\n" ;
out = sine( angleInRadians( (double )i ) );
cout << "Sin " <<out<<"\n" ;
out = myCosine( angleInRadians( (double )i) );
cout << "Cos " <<out<<"\n" ;
out = cosine( angleInRadians( (double )i) );
cout << "Cos " <<out<<"\n" ;
out = tangent( angleInRadians( (double )i) );
cout << "Tan " <<out<<"\n" ;
out = cotan( angleInRadians( (double )i) );
cout << "CoTan " <<out<<"\n" ;
out = secant( angleInRadians( (double )i) );
cout << "Secant " <<out<<"\n" ;
out = cosecant( angleInRadians( (double )i) );
cout << "CoSecant " <<out<<"\n" ;
}
}
double factorial(int x){
double f = 1.0;
for (int i = 1; i <= x; i++){
f = f * i;
}
return f;
}
double angleInRadians (double x){
return x / 180.0 * PI;
}
double power (double x, int exponent){
double out = 1.0;
for (int i=1;i<=exponent;i++){
out *= x;
}
return out;
}
double mySine (double x){
double output = 0;
double oldOutput = 9999;
int i = 1;
bool shouldAdd = true ;
while (abs(output-oldOutput) > 0.0001){
oldOutput = output;
output += (shouldAdd?1.0:-1.0) * power(x,i)/factorial(i);
shouldAdd = !shouldAdd;
i += 2;
}
return output;
}
double myCosine (double x){
double output = 1;
double oldOutput = 9999;
int i = 2;
bool shouldAdd = false ;
while (abs(output-oldOutput) > 0.0001){
oldOutput = output;
output += (shouldAdd?1.0:-1.0) * power(x,i)/factorial(i);
shouldAdd = !shouldAdd;
i += 2;
}
return output;
}
double sine(double x){
double out = x;
double term = x;
double i = 1.0;
while (abs(term) >= 0.0001){
i++;
term *= x/i;
i++;
term *= x/i;
term *= -1;
out += term;
}
return out;
}
double cosine(double x){
double out = 1;
double term = 1;
double i = 0.0;
while (abs(term) >= 0.0001){
i++;
term *= x/i;
i++;
term *= x/i;
term *= -1;
out += term;
}
return out;
}
double tangent(double x){
return sine(x)/cos(x);
}
double cotan(double x){
return 1.0/tangent(x);
}
double secant(double x){
return 1.0/cos(x);
}
double cosecant(double x){
return 1.0/sine(x);
}
Feb 7, 2016 at 9:05pm UTC
and I do not know how to do this
You may want to look into using the iomanip functions to properly space your table. To eliminate the scientific notation you may want to consider using the "fixed" manipulator.
Topic archived. No new replies allowed.