0

Unexpected values being printed

The attached code is supposed to take three inputs (like 1, -3, -306), make a quadratic equation using them(like 1x² - 3x - 306 = 0), and output the roots of the quadratic equation if Discriminant is greater than or equal to 0. However, it does not print the answers, the roots are displayed 0 and the Discriminant has a very high value no matter what the input is https://sololearn.com/compiler-playground/cgNDf4cpPecQ/?ref=app

23rd Jul 2025, 9:40 AM
Vaibhav
Vaibhav - avatar
10 odpowiedzi
+ 2
Vaibhav the problem that I see is that all parameters to the solvequadeq() function are being passed By Value. So any local value changes within the function remain local and do not get passed back. Make x1, x2, and D pass By Ref, by inserting an ampersand (&) like this: int solvequadeq(double a, double b, double c, double &x1, double &x2, double &D)
23rd Jul 2025, 1:57 PM
Brian
Brian - avatar
+ 4
In line 34, you compare 2 doubles with ==. This likely is an issue because of floating point precision. I haven't tested yet if this solves your issue.
23rd Jul 2025, 10:15 AM
Lisa
Lisa - avatar
+ 1
I wonder what that error was. Going back to your original code before it had pointers, here is the small modification to the parameters that works for me: int solvequadeq(double a, double b, double c, double &x1, double &x2, double &D) { D = pow(b, 2) - 4 * a * c; if (D < 0.0) return 0; if (D == 0.0) { x1 = (-b) / (2 * a); x2 = (-b) / (2 * a); return 1; } else { x1 = (-b - sqrt(D)) / (2 * a); x2 = (-b + sqrt(D)) / (2 * a); return 2; } } And in main(), call it as you did originally without pointer references: int r = solvequadeq(a, b, c, x1, x2, D);
23rd Jul 2025, 6:07 PM
Brian
Brian - avatar
0
Lisa, that is not a problem at all. The value of D will exactly be equal to 0.0 if roots are equal. Also, it should still print some value of the roots because the else statement will execute if the value is not 0.0
23rd Jul 2025, 11:57 AM
Vaibhav
Vaibhav - avatar
0
Even if it doesn't solve your main problem, I think it might still be a not-so-clean solution? I think part of the problem is how you pass the arguments to the solvequadeq() function: You pass them directly but then change them inside of the functions. Perhaps you need to use local variables or pass by reference. Does "D" get an initial value somewhere?
23rd Jul 2025, 12:17 PM
Lisa
Lisa - avatar
0
Is there a way to do it without pointers?
23rd Jul 2025, 1:59 PM
Vaibhav
Vaibhav - avatar
0
Naturally there are other ways to return multiple values, but that would add unnecessary complication. For example, you could define a struct or an object that has x1, x2, and D as properties. Call solvequadeq(a, b, c) as a method, then get x1, x2, D for the results. I think the simplest solution is to merely add three & characters to the existing program. Don't overwhelm yourself with fear of pointers. Just understand that if you want the function to modify the variables that you passs in, then precede the parameter with an ampersand.
23rd Jul 2025, 2:53 PM
Brian
Brian - avatar
0
I see, I've updated the code and now it works by the way, *D, *x1, and *x2 have to be used instead of D, x1, x2 because conversion from a double pointer to double is not possible. Anyway, thanks for the help, the code works fine now
23rd Jul 2025, 2:58 PM
Vaibhav
Vaibhav - avatar
0
The updated code that passes pointers works too. Although it is an older, C-style syntax. The pass by reference syntax that I recommended is a C++ upgrade that is easier to comprehend than pointer syntax. Then there is no need to adjust the arguments to be pointers when you call the function.
23rd Jul 2025, 3:25 PM
Brian
Brian - avatar
0
But then it gives me an error
23rd Jul 2025, 4:03 PM
Vaibhav
Vaibhav - avatar