quadratic equation


Hey everyone, im having a problem with a program which im trying to write to cover the quadratic equation formula..i was asked by my teacher to include only real roots for the quadratic equation but i am having a problem on how to fix the code so that if the value of A (in the equation ax2+bx+c=0) is equal to zero, i get the double roots; it is always conflicting with the the other conditions..please helpe me.thanks

here is the code:-


#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
double x1, x2, a, b, c, d;
cout<<"Enter the value of the cofficient of x^2 (a):\na = ";
cin>>a;
cout<<"Enter the value of the cofficient of x (b):\nb = ";
cin>>b;
cout<<"Enter the value of the constant c:\nc = ";
cin>>c;
d = (b*b) - (4 * a * c);
if (((a > 0)||(a < 0)) & d >= 0) {
x1 = (-b + sqrt(d) )/ (2 * a);
x2 = (-b - sqrt(d) )/ (2 * a);
cout<<"x1 = "<<x1;
cout<<"\n";
cout<<"x2 = "<<x2;
cout<<"\n";

}
else (a = 0); {
x1 = (-c) / (b);
x2 = (-c) / (b);
cout<<"x1 = "<<x1;
cout<<"\n";
cout<<"x2 = "<<x2;
cout<<"\n";

}
if (((a > 0)||(a < 0)) & d < 0) {
cout<<"There are no REAL roots for the quadratic equation containing the values of a, b and c which you have entered."<<endl;
cout<<"\n";
}






return 0;
}
Put that in code tags and indent it first.
I'm going to look at your problem from a more theoretical view because I understand what you are doing. Here's the steps I would propose you undertake:
Get the three coefficients a, b and c.
Check a - if a is zero, either force them to input a again, or say that the equation is not quadratic. (Because of course, a=0 would make it a linear equation.)
1
2
3
4
5
while (a == 0)
{
   cout << "NOT QUADRATIC! Input that again please.";
   cin >> a;
}


Second check - examine the discriminant. Mathematically speaking, if the discriminant is less than zero there are no real roots. (If you have forgotten your quadratics, the discriminant is (b^2) - 4ac)
1
2
3
4
if (((b*b)-(4*a*c)) < 0)
{
   cout << "Sorry, there are no real roots.";
}


If the discriminant is not negative, check if it is zero. If so, adjust your equation accordingly because there is only one root.
Finally - if the discriminant is neither zero nor negative, it must be positive so calculate the two roots accordingly and display them.

EDIT: By the way, I do see your actual problem. You have only one equals sign in your else and you need to say else if, not just else. Review your logical operators and if/else if tree. I don't really see the point of finding a line's x-intercept in a quadratic root finder but that's beside the point.
Last edited on
if ( ( (a > 0) || (a < 0) ) & d >= 0) {

In this line, you use the bitwise AND '&' instead of the logical and '&&'. There is a big difference! Bitwise and makes a comparison between the binary bit patterns of a number. In order to use it on floats (doubles), you should have a good understanding of the IEEE float specs.

My guess is that you were after the logical AND: &&

Example:

true && true == true

(4 < 6) && (5 != 7) == true

bitwise:

109 & 220 == 76

Does this solve your problem?
To add a bit of other information, ((a < 0) || (a > 0)) is the same as (a != 0)
True. You could even simplify it to simply (a) if you like!
Topic archived. No new replies allowed.