Roots of a quadratic not working for all a,b,c

#include
using namespace std;

float Sqrt(int n, int dp){
float ans=0,inc=1;
for (int i=0;i<=dp;i++,inc=inc/10){
for (;ans*ans<=n;ans=ans+inc);
ans=ans-inc;
}
return ans;
}

bool SolveQuad (int a, int b, int c, float &x1, float &x2){
int d = bb-4ac;
if (d>=0){
x1 = (-b+Sqrt(d,2))/2
a;
x2 = (-b-Sqrt(d,2))/2*a;
}
else return false;
}

int main(){
int a,b,c;
cin>>a>>b>>c;
float x1,x2;
bool isSol = SolveQuad(1,4,4,x1,x2);
if(isSol) cout<<x1<<" "<<x2;else cout<<“No Sol”;
return 0;
}

this works for inputs 1 4 4, 1 0 -4, etc
but doesn’t for many inputs like : 2 3 -5 giving output (roots) as : 2 -12, where actual roots are -3 and 0.5.

i can’t seem to find error in the code. Help. Thanks!

Hey Samarth, can you please copy your exact code on ide, save the code and then share that link. Actually this code is very ambiguous for eg. in line 14: int d = bb-4ac; for b^2 it should be b*b, like this there are many errors in this code you copied here, so do share the online ide link of your code.

I am sorry. Its my first time here.

For some reason its showing only no solution on online ide. ( worked atleast partially on code blocks )

https://ide.codingblocks.com/s/45114

Hey Samarth, if i am not wrong this is Revising Quadratic Equations problem and in this problem first you are supposed to calculate d and according to the value of d there will be 3 cases

  if (d == 0)
        cout << "Real and Equal";
    else if (d >= 0)
        cout << "Real and Distinct";
    else if (d < 0)
        cout << "Imaginary";

and every time you are getting No sol because isSol is never becoming true. So, to correct this just return true when valid roots exist as shown below(i have corrected this in your code only)

//Function for giving roots
bool SolveQuad (int a, int b, int c, float &x1, float &x2){
    int d = b*b-4*a*c;
    if (d>=0){
        x1 = (-b+Sqrt(d,2))/(2*a);
        x2 = (-b-Sqrt(d,2))/(2*a);
        return true;
    }
    else return false;
}

One more thing is according to the problem you are supposed to print the roots in sorted order.

could you check the code again. I have put so as the program will sort roots in ascending order.

https://ide.codingblocks.com/s/45114

but on giving 2 5 -3 I am getting roots as -12 and 2 not -3 and 0.5.

(as given here : https://www.mathsisfun.com/quadratic-equation-solver.html )

Hey Samarth, this issue is just because of precedence of operators. As * and / have the same precedence in c++, when your x1 = (-b+Sqrt(d,2))/2*a; statement is executed it is calculated in this way:
ans1 = (-b+Sqrt(d,2))
firstly (-b+Sqrt(d,2)) is calculated i.e ans1 then ans1 is divided by 2
ans2 = (-b+Sqrt(d,2))/2
after calculating ans2 it is multiplied by a
ans3 = (-b+Sqrt(d,2))/2*a

but we first want to calculate ans = 2*a and then divide ans1 = (-b+Sqrt(d,2)) by ans.

So, for this modify your syntax for formula of x1 and x2 by putting parentheses over 2*a, so that it will be calculated first, as shown below
x1 = (-b+Sqrt(d,2))/(2*a);

1 Like

That solved it…
Got it…
Thank you!!

No problem keep coding and raising doubts :slight_smile: