Ok for the most part I understand what this question is asking, however there are some parts which seem a little hazy which maybe somebody could help me out with:
Here is the initial question:
Write a C++ program that solves quadratic equations to find its roots.
The roots of a quadratic equation a x2 + b x + c = 0 (where a is not zero) are given by the formula:
(-b ± sqrt (b2 – 4 a c) / 2 a
The value of the discriminant (b2 – 4 a c) determines the nature of the roots. If the value of the discriminant is zero then the equation has a single real root.
If the value of the discriminant is positive then the equation has two real roots. If the value of the discriminant is negative, then the equation has two complex roots. If the roots are real, print out the real roots; otherwise print out a message stating that the roots are complex. The program takes a series of three floating point values (in the order a, b, and c) and outputs the roots for each triplet to another file.
Include a loop that reads each triplet in the file until an end of file is encountered. Please make sure the output file contains appropriate identification to determine the equations for each root. The input file should be named equations.txt and the output file should be named roots.txt.
Now I think the mechanics of it are all down, I am really just confused about the context and maybe just the math surrounding this problem (easy as it is) just the way it is stated makes me second guess what I'm doing...
Here is the code for everything:
CODE
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//Homework # 2 -- Problem # 1
int main(int argc, char *argv[])
{
ifstream in_stream;
ofstream out_stream;
in_stream.open("equations.txt");
if ( in_stream.fail() ){
cout << "Input file opening failed." <<endl;
exit(1);
}//end if
out_stream.open("roots.txt");
if ( out_stream.fail() ){
cout << "Output file opening failed."<<endl;
exit(1);
}//end if
float a, b, c;
while ( ! in_stream.eof()){
in_stream >> a >> b >> c;
out_stream << "The value of the discriminant is: " << (b * b - 4 * a * c) <<endl;
if ( (b * b - 4 * a * c) == 0 ){
out_stream << "The equation contains a single root" <<endl;
}//end if
if ( (b * b - 4 * a * c) < 0 ){
out_stream << "The equation contains two complex roots" <<endl;
}//end if
if ( (b * b - 4 * a * c) ) > 0 ){
out_stream << "The equation contains two real roots" <<endl;
}//end if
}//end while
system("PAUSE");
return EXIT_SUCCESS;
}
My actual question is this: How, if the roots are real, do I go about getting them? I can defitinely determine whether or not they are real by seeing if they output a positive discriminant but I have no idea what they are
Any help would be appreciated. Thanks