I have to write a program that will find the roots of a 2nd degree polynomial. I have to use a function, use pass by value, and cout everything in the main function. I am having trouble knowing how to format the equation into the function and how to call the function twice for each root. This is what i have so far.
You are basically there. Why not just have two cout << statements in your find_root function? That will definitely work. Also, you should use double disc in that function since you have initialized it properly. Just put it in the x1 = and x2 = expressions.
I'm confused, what do you mean by use double disc in that function? and put x1 = / x2 = where? I am not supposed to "cout" anything from my find_root function, only from main0. Here is my updated code, I think i almost have it. I have to format it for the imaginary roots, but am i returning x1 then x2? I can't tell right now.
As you have it right now, your root1 and root2 are the same, since find_root is only returning x1. Are you allowed to add a 4th argument to your find_root function? If so, you could do something like this:
1 2 3 4 5 6 7 8 9
double find_root(double a, double b, double c, int sign)
{
double x;
double disc = b*b-4*a*c;
x = (-b+sign*sqrt(disc))/(2*a); //This is what I meant in my first post... disc wasn't doing anything before
return x;
}
The disadvantage to the above code is that the user should only be entering +1 or -1 for the sign variable, but is free to enter whatever they want. But, there are plenty of ways you could modify that to make it easier to use.
I am only supposed to use 3 arguments, unfortunately. I am able to use a static variable(s) to return the two values, i am just not quite sure how to do that because we just went over it.
My prof said we should be either using static variables if we want, or just call the function twice to send both roots back, i am unsure on how to do both is the problem.
I'm not the most experienced programmer in the world, so maybe this isn't the best method, but you could do the following:
1 2 3 4 5 6 7 8 9 10
double find_root(double a, double b, double c)
{
double x;
double disc = b*b - 4*a*c;
staticint sign = 1;
sign = -sign; //Change sign for second root
x = (-b + sign*sqrt(disc))/(2*a);
return x;
}
You'll have to call the find_root function twice for each set of parameters a, b, and c; each call to the find_root function flips the sign of the +/- in the quadratic formula.
Well, here is my updated program. Still a few bugs. Such as: my while loop doesn't return to the beginning, after input "ans", it just stop the program. Also, still getting root1 = root2 and also i am sometimes getting weird roots like "-1.ind#". where am i going wrong?
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
double find_root (double, double, double);
int main()
{
double a = 0;
double b = 0;
double c = 0;
double root1;
double root2;
double disc = ((b*b) - (4*a*c));
string ans = "yes";
while (ans == "yes" || ans == "Yes")
{
cout<<"This function will find the roots of a 2nd degree polynomial""Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
cin>>a>>b>>c;
root1 = find_root(a,b,c);
root2 = find_root(a,b,c);
if (disc <0)
{
cout<<"The roots are "<<(-b/(2*a))<<"+/-"<<(sqrt(abs(disc)))<<" i"<<endl;
}
elseif(a ==0)
cout<<"That is a line, not a 2nd degree polynomial."<<endl;
else
{
cout<<"The roots are "<<root1<<" and "<<root2<<endl;
}
cout<<"Would you like to run this again? Yes or yes for yes, anything else for no."<<endl;
cin>>ans;
system("Pause");
return 0;
}
}
double find_root(double a, double b, double c)
{
staticdouble x;
staticint sign;
sign = -sign;
double disc = ((b*b)-(4*a*c));
x = (-b + (sign)*sqrt(b*b - 4*a*c))/(2*a);
return x;
}
The while doesn't keep going because you put return 0; inside the loop. You want it outside. You also need to insert an additional << in line 25. The other stuff isn't working because you didn't initialize staticint sign. Right now, it by default initializes to 0, which will obviously produce the wrong result.
It did! Just one more problem, probably simple, but i am getting "501...BB.." before my cout of the complex roots. Also, when a "0" is input for b, it seems to ignore the setprecision and it couts "0.00000" instead of just "0.00".
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
usingnamespace std;
double find_root (double, double, double);
int main()
{
double a = 0;
double b = 0;
double c = 0;
double root1;
double root2;
double disc = 0;
string ans = "yes";
while (ans == "yes" || ans == "Yes")
{
cout<<"This function will find the roots of a 2nd degree polynomial\n"
<<"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
cin>>a>>b>>c;
system("Cls");
root1 = find_root(a,b,c);
root2 = find_root(a,b,c);
disc = ((b*b) - (4*a*c));
if (disc < 0)
{
cout<<fixed<<setw(7)<<setprecision<<(2)<<"The roots are "<<(-b/(2*a))<<" +/- "
<<fixed<<setw(7)<<setprecision(2)<<(sqrt(abs(disc))/2*a)<<" i"<<endl;
}
elseif(a ==0)
cout<<"That is a line, not a 2nd degree polynomial."<<endl;
else
{
cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<root1<<" and "
<<fixed<<setw(7)<<setprecision(2)<<root2<<endl;
}
cout<<"Would you like to run this again? Yes or yes for yes, anything else for no."<<endl;
cin>>ans;
}
system("Pause");
return 0;
}
double find_root(double a, double b, double c)
{
staticdouble x;
staticint sign = 1;
double disc = ((b*b)-(4*a*c));
sign = -sign;
x = (-b + (sign)*sqrt(abs(disc)))/(2*a);
return x;
}
It seems like setw sets the number of characters to output; setprecision sets the decimal precision you would like to output. Consider 0.00923. Setprecision of two should output as 0.0092.