Need Help Please! - C++ Program
Hello,
I'm trying to figure this program out. I am a beginner C++ programmer and I have done pretty well in school so far with the programming assignments, but this one has me so confussed I don't understand where to even start or even how to do this. Can someone please help me out?? Here is the question:
The roots of the quadratic equation ax^2+bx+c=0, a does not = 0 are given by the following formula:
-b+/-sqrt b^2-4ac
------
2a
In this formula, the term b^2-4ac is called the discriminant. If b62-4ac=0, then the equation has a single (repeated) root. If b^2-4ac>0, the equation has two real roots. if b^2-4ac<0, the equation has two complex roots. Write a program that prompts the user to input the value of a (the coefficient of x^2), b (the coefficient of x), and c (the constant term), and outputs the type of roots of the equation. Furthermore, if b^2-4ac>=0, the program should output the roots of the quadratic equation. (Hint: Use the function pow from the header file cmath to calculate the square root.)
Can someone please explain this to me? To be completely honest, this question sounds like a total different language (let's just say when I was in high school, I was lucky I passed geometry (that was 10 yrs ago)). Please help and explain so I can comprehend this question that has been asked for us to program. I REALLY appreciate your help!!!
[1458 byte] By [
ethompson] at [2007-11-11 8:50:06]

# 1 Re: Need Help Please! - C++ Program
I think I can help you with this...
First of all, you need to input a, b, and c. I am sure you know how to do that - use scanf or cin or whatever else you've been using. As far as the types of these variables, you will probably want to use doubles, since the pow function that you are going to use takes doubles (but there is a powf version for floats, if you can stand less precision).
Here's an example:
double a, b, c;
cout << "enter a, b, and c values separated by space(s): "
cin >> a >> b >> c;
Then you need to calculate the discriminant, which is b^2 - 4ac. In C/C++ form, this will look something like
double discr = pow(b, 2.0) - (4.0 * a * c);
Note that the second arg to pow function is the power you want to raise the first arg to. If the power is 1, the number remains unchanged and if it's 0, you get 1 (you should know this from math class). If it's greater than one, then the number is multiplied by itself that many times (obviously). If it's less than 1 but more than 0, you get a root of the number - for example pow(2.0, 0.5) will give you a square root of 2, a.k.a 2 raised to 1/2 power.
Then you need to examine discr. If it's equal to 0, then you print "the equation has a single repeated root" or something like that. If it's greater than 0, print that it has two real roots. If it's less than 0, print that it has two complex roots.
In the last step, you check if the discr is either equal to zero or greater than zero (>=). If it's not, then you exit. If it is, then you proceed to programmatically solve the equation - first solve for (-b + pow(discr, 0.5)) / 2.0 * a, then solve for (-b - pow(discr, 0.5)) / 2.0 * a. That will give you the answers, which you will output.
val at 2007-11-11 21:01:01 >
