help with discriminant based code!!

Hello, I've been stuck on this code for the past 2 hours. I'm supposed to write a program that uses double a, b, c, d; which asks the user to enter the value of the coefficients a, b, c. Note that a - the coefficient of x^2 can not be zero, for otherwise the equation is at most linear. The code should check that, but I don't know how to do that. Then the code is supposed to compute the value of the discriminant d. Based on the value of the discriminant d, the program will compute the solutions double x1, x2;

In the negative determinant case, I have to use two additional variables double xr, xi; where xr = -b / (2a) and xi = sqrt(-d)/(2a). The solutions are of the form xr - 1 * xi, xr + i * xi.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <math.h>

int main () {
double a, b , c, d;
cout<< " Enter coefficient A: " << endl;
cin>> a;
cout<< " Enter coefficient B: " << endl;
cin>> b;
cout<< " Enter coefficient C: " << endl;
cin>> c;

Any help is greatly appreciated!
closed account (o3hC5Di1)
Hi there,

I'm personally terrible at mathematics, but I don't see any actual maths going on in your code.
I assume you left that part out?

compscimajor wrote:
Note that a - the coefficient of x^2 can not be zero, for otherwise the equation is at most linear. The code should check that, but I don't know how to do that.


A relatively easy way to do this is using a loop to keep asking for A until the user enters a value higher than zero:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main () {
double a=0, b=0 , c=0, d=0;  //initialize them to zero

while (a < 0)  //as long as a is smaller than zero
{
    //keep asking the user
    cout<< " Enter coefficient A, must be positive: " << endl;
    cin>> a;
}

cout<< " Enter coefficient B: " << endl;
cin>> b;
cout<< " Enter coefficient C: " << endl;
cin>> c;



I'm afraid the rest of your problem sounds a bit like Greek to me, perhaps if you shared your full code we could have a look at that and be able to help you along further.

Sorry I couldn't be of more help.

All the best,
NwN
Last edited on
NwN's line 4 won't quite do. A negative 'a' is fine, 0==a is not. You can drop the sign with abs(), e.g. abs(a).

Testing a double for being 0 is not trivial though; floating point values do not behave the way they do in math. You have to use a abs(a) < epsilon, where the epsilon is a tiny number, almost zero. I cannot remember what is epsilon for double.

You have the equations. You can write them almost directly as C++.
closed account (o3hC5Di1)
Thanks for correcting that Keskiverto - I've changed it in my post anyway to avoid confusion.

All the best,
NwN
Testing a double for being 0 is not trivial though;
I think you over-complicate matters here. For an integer value entered directly at the keyboard, there should be no problem.

Things only become more tricky with fractional values, or those which are the result of some previous calculation, neither of which apply here.
Topic archived. No new replies allowed.