Hey guys, help this beginner pls. The professor wants us to write the program below. The problem is I can only return one or the other value but not both.One friend suggested me to use a static int in the function but I don't know how.I also made the program work but I used 4 arguments and the professor only wants us to use 3 arguments.Pass by reference or global var are not allowed either. Any help would be appreciated ty.
I was also trying to create 2 functions one to find r1 and the other r2 but i couldnt make it work ;/
Write a C++ program that utilizes a function named “find_root” to find the roots of a second order polynomial. Prompt the user for the coefficients and you must create the “find_root” function and all arguments must be “pass by value”. The function can only return one value using the “return” statement. Since a second order polynomial has two solutions, you will probably have to call the “find_root” function more than once for each set of solutions. All output must be from within the main program.
Ty MiiniPaa. I'll fix the if condition, but where how do i use std::pair<double, double>? or
std::vector<double>. Should it be something like std::roots<double,double> (float a,float b , float c) for the prototype and then for the function std::roots<double roots,double root2>(float a,float b , float c)?
This is the language in your question that bothers me the most:
Since a second order polynomial has two solutions, you will probably have to call the “find_root” function more than once for each set of solutions.
It implies that you may only return one value, and that the solution desired is one that will spit out different roots on subsequent calls to the same function. It is possible, but ugly considering such alternatives as what MiiNiPaa has already offered. One way to accomplish it through consecutive calls to the function is to have a static variable inside the function that decides whether to add or subtract the discriminant. Once the calculation is made, this flag is flipped so that the next invocation of the function performs the opposite operation:
double getRoot (double a, double b, double c)
{
//Add discriminant if true, subtract if false.
//The "= true" part only applies once at initialization,
//not on subsequent calls to this function
staticbool addDiscriminant = true;
double retVal = 0.0;
double discriminant = std::sqrt(std::pow(b, 2) - (4.0*a*c));
if (addDiscriminant)
{
retVal = ((-b) + discriminant) / (2*a);
}
else
{
retVal = ((-b) - discriminant) / (2*a);
}
//Flip the flag from true to false, or vice versa.
//Basically, each consecutive call to the function
//will do the opposite of what the last one did
addDiscriminant = !addDiscriminant;
return retVal;
}
Yes I thought I wrote the suggestion that one of my friends gave me. He said he created an static int inside the function, he did something similar as you just did, but I just couldnt understand it. But if he used and static int and made it work then thats how it should be done. Thank you