Objective is to have a program that will calculate the roots of a 2nd degree polynomial using pass by reference. I am getting 0.00 for every answer, i don't think it is my equation. I am pretty sure i am using pass by reference wrong, but not sure how.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
void find_root(double&, double&, double&);
int main()
{
double a = 0;
double b = 0;
double c = 0;
double disc = 0;
string ans = "yes";
double x1 = 0;
double x2 = 0;
while(ans == "yes")
{
cout<<"This program will calculate the roots of a 2nd degree polynomial"<<endl
<<"Please input the values for A, B, and C"<<endl<<endl;
cin>>a>>b>>c;
system("cls");
if (disc < 0)
{
cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<(-b/(2*a))<<" +/- "
<<fixed<<setw(7)<<setprecision(2)<<(sqrt(abs(disc))/2*a)<<" i"<<endl<<endl;
}
elseif(a ==0)
cout<<"That is a line, not a 2nd degree polynomial."<<endl<<endl;
else
{
cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<x1<<" and "
<<fixed<<setw(7)<<setprecision(2)<<x2<<endl<<endl;
}
cout<<"Would you like to run this again? yes for yes, any other key for no"<<endl<<endl;
cin>>ans;
}
system("pause");
return 0;
}
void find_root(double& a, double& b, double& c)
{
double disc = ((b*b)-(4*a*c));
double x1 = (-b + sqrt(abs(disc)))/(2*a);
double x2 = (-b - sqrt(abs(disc)))/(2*a);
return;
}
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
void find_root(double&, double&, double&);
int main()
{
double a = 0;
double b = 0;
double c = 0;
double disc = 0;
string ans = "yes";
double x1 = 0;
double x2 = 0;
while(ans == "yes")
{
cout<<"This program will calculate the roots of a 2nd degree polynomial"<<endl
<<"Please input the values for A, B, and C"<<endl<<endl;
cin>>a>>b>>c;
system("cls");
find_root(a,b,c);
if (disc < 0)
{
cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<(-b/(2*a))<<" +/- "
<<fixed<<setw(7)<<setprecision(2)<<(sqrt(abs(disc))/2*a)<<" i"<<endl<<endl;
}
elseif(a ==0)
cout<<"That is a line, not a 2nd degree polynomial."<<endl<<endl;
else
{
cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<x1<<" and "
<<fixed<<setw(7)<<setprecision(2)<<x2<<endl<<endl;
}
cout<<"Would you like to run this again? yes for yes, any other key for no"<<endl<<endl;
cin>>ans;
}
system("pause");
return 0;
}
void find_root(double& a, double& b, double& c)
{
double disc = ((b*b)-(4*a*c));
double x1 = (-b + sqrt(abs(disc)))/(2*a);
double x2 = (-b - sqrt(abs(disc)))/(2*a);
return;
}