The program is supposed to prompt the user for coefficients a b and c from a quadratic equation. Then it should output real or imaginary roots.

Will you please tell me if you spot where the issue may be?

#include <iostream> /* include header file to do input and output */

#include <fstream> /* include the library so you can read/write files */

#include <iomanip> /* include the library that allows formatting */

#include <cmath>

using namespace std; /* allow operations from standard library */

void roots_of_quadratic(float a, float b, float c, float *Results);

int main(void)

{

ofstream outfile; /* declare var to write to a file */

outfile.open("hw5SJGb.txt"); /* open output file to write to */

outfile << "Savannah Gregor \n\n";

cout << "Savannah Gregor..hw5SJGb \n" << endl;

float a, b, c;

float roots[2]; //store results of x

cout<<"Please input a, b, and c to get roots:\n";

cout<<"\n\n a: ";

cin>>a;

outfile<<"\n\n a is : "<<a;

cout<<"\n\n b: ";

cin>>b;

outfile<<"\n\n b is : "<<b;

cout<<"\n\n c: ";

cin>>c;

outfile<<"\n\nc is : "<<c;

roots_of_quadratic(a, b ,c, &roots[0]);

cout<<"\nRoots = "<<roots<< endl;

outfile<<"\n\nThe roots are \t\t : "<<roots;

for(int i = 0; i < 2; i++)

{

cout<<"\n\nThe roots are \t\t "<< roots[i] <<endl;

}

system("pause");

return 0;}

void roots_of_quadratic(float a, float b, float c, float *Results)

{

float num = sqrt( (b*b) - (4*a*c) );

float denominator = 2 * a;

float neg_b = 0 - b;

if (denominator == 0)

{

cout<<"Undefined\n";

exit(0);

}

Results[0] = (neg_b + num)/denominator;

Results[1] = (neg_b - num)/denominator;

}

Will you please tell me if you spot where the issue may be?

#include <iostream> /* include header file to do input and output */

#include <fstream> /* include the library so you can read/write files */

#include <iomanip> /* include the library that allows formatting */

#include <cmath>

using namespace std; /* allow operations from standard library */

void roots_of_quadratic(float a, float b, float c, float *Results);

int main(void)

{

ofstream outfile; /* declare var to write to a file */

outfile.open("hw5SJGb.txt"); /* open output file to write to */

outfile << "Savannah Gregor \n\n";

cout << "Savannah Gregor..hw5SJGb \n" << endl;

float a, b, c;

float roots[2]; //store results of x

cout<<"Please input a, b, and c to get roots:\n";

cout<<"\n\n a: ";

cin>>a;

outfile<<"\n\n a is : "<<a;

cout<<"\n\n b: ";

cin>>b;

outfile<<"\n\n b is : "<<b;

cout<<"\n\n c: ";

cin>>c;

outfile<<"\n\nc is : "<<c;

roots_of_quadratic(a, b ,c, &roots[0]);

cout<<"\nRoots = "<<roots<< endl;

outfile<<"\n\nThe roots are \t\t : "<<roots;

for(int i = 0; i < 2; i++)

{

cout<<"\n\nThe roots are \t\t "<< roots[i] <<endl;

}

system("pause");

return 0;}

void roots_of_quadratic(float a, float b, float c, float *Results)

{

float num = sqrt( (b*b) - (4*a*c) );

float denominator = 2 * a;

float neg_b = 0 - b;

if (denominator == 0)

{

cout<<"Undefined\n";

exit(0);

}

Results[0] = (neg_b + num)/denominator;

Results[1] = (neg_b - num)/denominator;

}

hmm... does division in C++ handle imaginary numbers? I can't remember... if it does not, much more work will be required for this (you'd likely need to know the form re^(iO), where O is theta, which converts to r(cos(O)+isin(O)).

Topic archived. No new replies allowed.