Hey there C++ programmers, I need some help getting my code. I'm stuck on a homework assignment and need a little help clarifying what needs to be done. I'm running everything in Visual Studio 2010 Professional (well I'm supposed to, but I can't Debug, so im using an online compiler )
Here's what I've got so far:
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
|
#include <iostream>
#include <cmath> //square root function
using namespace std;
int main () // my main
{
double a,b,c,x1,x2; // my doubles
cout<<"Enter value a:";
cin>>a;
// promts the user for the A value
cout<<"Enter value b:";
cin>>b;
// promts the user for the B value
cout<<"Enter value c:";
cin>>c;
// promts the user for the C value
x1= (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x1, which is the positve root
x2= (-b - sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x2, which is the negative root
cout << "X1: " << x1 << endl; // displays value of X1 or the first root
cout << "X2: " << x2 << endl; // displays value of X2 or the second root
cout << "Have a nice day :) " << endl;
// informs to have the user to have a nice day
return 0; //returns zero to terminate program without error
}
|
There are 3 criteria that I can't seem to get/understand.
The discriminant, b
2 - 4ac, describes the roots of a quadratic equation: if it is greater than or equal to 0, there are two real roots; and if it is less than 0, there are two complex roots.
a. Complex numbers are displayed with a real part and an imaginary part: e.g., 6 + 3i and 6 - 3i
b. The real part (6 in the example) is calculated by the formula -b/2a
c. The imaginary part (3i in the example) is calculated by the formula
sqrt(descriminant)/2a with an āiā character printed at the end
d. Taking the square root of a negative number with the sqrt function causes a run-time error, so the discriminant must be negated (i.e., multiplied by -1) before taking the square root
3. Your program should find the roots in all three situations (the lab 1 version should work when the discriminant = 0 or > 0); your modifications for lab 2 should handle the case when the discriminant is < 0.
The test cases should equal as follows:
a = 2, b = 2, c = 2; x1 = -0.5 + 0.866025i, x2 = -0.5 - 0.866025i