Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 12-29-2005, 08:24 PM   #1 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default 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;
}
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-29-2005, 08:56 PM   #2 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

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))/(2*a);
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Old 12-29-2005, 10:11 PM   #3 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

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
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-29-2005, 10:22 PM   #4 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

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 )
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Old 12-29-2005, 11:03 PM   #5 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

ahh ok. I was too lazy to make my own polynomial and the one i was using was wrong :P
__________________

Robin Lovett
April 21, 1948 - March 12, 2007
DontdroptheSOAD is offline   Reply With Quote
Old 12-29-2005, 11:47 PM   #6 (permalink)
Master of the Internet
 
DontdroptheSOAD's Avatar
 
Join Date: Apr 2003
Location: Glen Saint Mary, Florida
Age: 19
Posts: 6,834
Send a message via AIM to DontdroptheSOAD
Default

nvm problem solved.
__________________

Robin Lovett
April 21, 1948 - March 12, 2007

Last edited by DontdroptheSOAD; 12-30-2005 at 01:07 AM.
DontdroptheSOAD is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5