0
votes

I have this code in my program: (I included the cout statements for debugging purposes)

cout << "b: " << b << "\na: " << a;
constant[0] = (-b / (2 * a));
cout << "\nconstant: " << constant[0] << endl;

The output I get is:

b: -4
a: 3
constant: 0

Whereas I'm trying to make constant[0] equal to -(-4)/(2 * 3), or 0.6666... What am I doing wrong with the formula I put there?

3
This is very basic thing. This must have been explained in any basic C/c++ text book.Adil
Yes, I learned it in my textbooks years ago. But errors can happen anytime, no? Why do you guys just have to denounce stuff as "very basic" and "must have been explained in any basic" book?wrongusername

3 Answers

9
votes

Undoubtably you have a and b defined as integers, causing your whole formula to be done in integer math. Either define them as floating point numbers or do something like this:

constant[0] = (-b / (2.0 * a));

which forces the math to be done in floating point.

2
votes

Is constant an integer? Are a and b integers?

cout << "b: " << b << "\na: " << a;
constant[0] = (-b / (2.0 * a));
cout << "\nconstant: " << constant[0] << endl;

Integer division and/or variable types are the issue.

1
votes

Your constant 2 is an int, make it 2.0.

Make sure your variables a and b are doubles or floats too?