-
While Loop Problem
Ok, so I am doing an assignment for my C++ class at a local community college. The assignment is that I have to program the series "sine(x) = (x^1/1!) - (x^3/3!) + (x^5/5!) - (x^7/7!)....". I have coded everything, but no matter what I try, the while loop in my sine function, does not want to work the way I want it to. I have to enter a precision, such as .001 so the user can see how close the program gets. Anyways, the loop never executes properly. Try and see if you can spot the problem, here is my source code..
Code:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
const double PI = acos(-1);
double d_to_r(double term)
{
double radians;
radians = term * (PI/180);
return radians;
}
long factorial(int x)
{
long product = 1;
int k=0;
while (k<x)
{
k++;
product = product * k;
}
return product;
}
double power(double x, int p)
{
double product = 1;
int k = 0;
while (k<p)
{
k++;
product = product * x;
}
return product;
}
double sine(double x, double precise)
{
double term = 1;
int k = 1;
double sum = 0;
do
{
cout << "k: " << k << endl;
cout << "term: " << term << endl;
cout << "sum: " << sum << endl;
term = (power(-1, k+1) * (power(x, 2*k-1)/factorial(2*k-1)));
sum += term;
k++;
}while ((abs(term)<precise) || (k<7/*Due to factorial, after 7, it is not accurate*/));
return sum;
}
int main()
{
string conversion; //The string for the conversion question
double term; //The double to hold the angle
double precise; // The double to hold precision
cout << "Hi, welcome to the Sine Function\n";
cout << "Please enter a angle for the Sine Function\n";
cin >> term;
cout << "To what precision?\n";
cin >> precise;
cout << "Is the value in degrees or radians?\n";
cout << "If the value is in degrees, type 'degrees', otherwise you\n";
cout << "may type anything else.\n";
cin >> conversion;
if (conversion == "degrees")
{
cout << sine(d_to_r(term), precise);
cout << sin(term);
}
else
{
sine(term, precise);
cout << sin(term); //math.h function to compare
}
return 0;
}
Ok, all help will be appreciated, btw d_to_r converts degrees to radians..
-
wat do u mean it doesnt run properly?
if i enter
45
.001
degrees
i get a never ending loop, with the same values and entering "thtfhht" instead of degrees it only runs a few times, is that the problem?
-
yes, that is a problem, ignore the degrees thing, and just focus on the actual while loop, WHY DOESNT IT WORK!?!
-
It probably doesn't work because you're using floating point numbers for comparison. This usually doesn't work, especially since 'abs' returns an integer, and you're comparing it to a double.
Quzah.
-
i don't know, the teacher told us to do it that way, you'd think a c++ instructor knows what he wants us to do to get it to work? thanks for the suggestion? any more..?
-
I'm going to re-suggest what Quzah said, hes pretty dead on with stuff.
Perhaps you can look into it and do it otherwise then show him documents as to why u changed his instructions?
-
I can scan the document that has the assignment and put it up on my webspace so you can check it out. While you are doing that, I will mess with the program..(Btw, other kids have gotten this to work and turned it in, so I have absolutely no idea, they followed the teachers instructions as well(or so i think))
-
Yea scan that baby lets have a looksy. If you dont wanna upload it send email to [email protected] its quicker.
-
Here we go, as you will see in the assignment, I really need the functions to stay the data types they are and I need the logic condition too.. here are the links..
Front of Assignment Page
Back of Assignment Page
-
hmmmmm....I see your dilema but i dont have a solution, sorry.
-
i swear, NOONE has a solution! THE INSTRUCTOR DIDN'T HAVE A SOLUTION! I asked one of the 30 best math students in the country who is also a great programmer, he had no idea! Oh well, I will keep testing things, any input at all is very helpful and i am very thankful:)
-
Have u tried using a different condition for the loop? Or changing it up a little? Im not good with the math functions (yet) so i wont be a big help on this one, but im trying since its something you are obviously stuck on.
-
well, after a test, I found something interesting, you have all been saying it.
i modified the code in my while loop so it looks like this
Code:
double sine(double x, double precise)
{
double term = 1;
int k = 1;
double sum = 0;
do
{
cout << "k: " << k << endl;
cout << "term: " << term << endl;
cout << "sum: " << sum << endl;
cout << "abs: " << abs(term) << endl;
term = (power(-1, k+1) * (power(x, 2*k-1)/factorial(2*k-1)));
sum += term;
k++;
}while ((abs(term)<precise) || (k<7));
return sum;
And it produces this interesting output.
Hi, welcome to the Sine Function
Please enter a angle for the Sine Function
4
To what precision?
.001
Is the value in degrees or radians?
If the value is in degrees, type 'degrees', otherwise you
may type anything else.
no
k: 1
term: 1
sum: 0
abs: 1
k: 2
term: 4
sum: 4
abs: 4
k: 3
term: -10.6667
sum: -6.66667
abs: 10
k: 4
term: 8.53333
sum: 1.86667
abs: 8
k: 5
term: -3.25079
sum: -1.38413
abs: 3
k: 6
term: 0.722399
sum: -0.661728
abs: 0
k: 7
term: -0.105076
sum: -0.766805
abs: 0
k: 8
term: 0.0347345
sum: -0.73207
abs: 0
k: 9
term: -0.535716
sum: -1.26779
abs: 0
Notice the absolute value, I need to find a way to make it work...
-
Think I solved it with a fabs instead of abs..
Edit: No I didnt, heh
-
try looking up the abs thru a board search to see how others have used it.