RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Wrong answer.

Maynza

Formerly DontdroptheSOAD
Wrong answer.

I know this is simple, but I am teaching myself and i am trying to figure out why I am getting the wrong answer.

I think it is probably my order of operations.
Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  cout<<"The Quadratic Formula is represented as..."<<endl;
  cout<<"-b+sqrt(b^2-4ac)/2a \n& \n-b-sqrt(b^2-4ac)/2a"<<endl;
  cout<<"Enter the value of your polynomials."<<endl;
  double a, b, c, x1, x2, q;
  cout<<"A=";
  cin>>a;
  cout<<"\nB=";
  cin>>b;
  cout<<"\nC=";
  cin>>c;
  x1 = (-b+sqrt((b*b)-4*a*c))/2*a;
  cout<<"X="<<x1;
  cin>>q;
  return 0;
}
 

arul

Sorceror
I'd try to put a parentheses there, since the numerator part of the expression "(-b+sqrt((b*b)-4*a*c))" is divided by 2, then multiplied by A.
Code:
 x1 = (-b+sqrt((b*b)-4*a*c))/[B][COLOR="Red"]([/COLOR][/B]2*a[B][COLOR="#ff0000"])[/COLOR][/B];
 

Maynza

Formerly DontdroptheSOAD
it is still giving me -1.#IND

I'm using 1,3,4 as A,B,C and I am thinking i should be getting -4
 

arul

Sorceror
Those numbers sounds like an error to me, because they will give you a negative argument to the sqrt, which will result in mathematical error.
Code:
3^2 - 4 * 1 * 4  = 9 - 16 = -7
You can't compute a square root of the negative number in the scope of real numbers. ( you would need to work with the complex numbers )
 

Maynza

Formerly DontdroptheSOAD
ahh ok. I was too lazy to make my own polynomial and the one i was using was wrong :p
 
Top