3 Replies - 920 Views - Last Post: 18 October 2009 - 08:40 PM Rate Topic: -----

#1 AhmerjavedC++   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Need help with logical error

Posted 16 October 2009 - 07:41 PM

the program calculates the Quadratic formula. But i am having difficulties with finding the write zeros for one equation
I tried the following:
A = 2 B = -4 C= -3
But it does not give me the following values:
x1 = –0.58, x2 = 2.58
#include <cmath> // includes all instrutions for CMATH
#include <iostream>
using namespace std;

int main()
{
	long double A,B,C,DISC,X1,X2;
	
	cout << "Welcome to the program quad, this program calculates the zeros in";
	cout << endl;
	cout << "a given function (ex. 1x^2+3x-4) by calculating the quadratic\n";
	cout << "formula.";
	cout << endl;
	do // error Check loop
	{
		  cout << "Please enter a value for A:";
		  cout << endl;
		  cout << "WARNING: A CAN NOT EQUAL 0";
		  cout << endl;
		  cin  >> A;
		  cout << endl;
		  cout << "Please enter a value for B:";
		  cout << endl;
		  cin  >> B;
		  cout << endl;
		  cout << "Please enter a value for C:";
		  cout << endl;
		  cin  >> C;
		  cout << endl;
	 }while ((A <= 0)||(A == 0));
	 
	 DISC = pow(B,2)-4*A*C;
	
	if (DISC < 0)
	{
	   cout << endl;
	   cout << "NO SOLUTIONS: IMAGINARY Awnser\n";
	   cout << endl;
	}
	else if (DISC == 0)
	{
	 cout << endl;
	 cout << "1 Solution: "<< -1*B/2*A;
	 cout << endl;
	 }
	else if (DISC > 0)
	{
	 cout << endl;
	 cout << "2 solutions: "<<endl;
	 X1= -1 * B + sqrt(DISC) / 2 * A;
	 X2= -1 * B - sqrt(DISC) / 2 * A;
	 cout << endl;
	 cout << "solution 1 = " << X1;
	 cout << endl << endl;
	 cout << "solution 2 = " << X2;
  
	 }
	
	cout << endl << endl << endl << endl << endl;
	system ("pause");
	return 0;
}
	 



Please HELP!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with logical error

#2 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Need help with logical error

Posted 16 October 2009 - 08:52 PM

>>finding the write zeros
You need to learn the difference between write and right. write means to write something down on paper, right means correct.

This post has been edited by Ancient Dragon: 16 October 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Need help with logical error

Posted 16 October 2009 - 08:52 PM

The problem is in these two lines:

	 X1= -1 * B + sqrt(DISC) / 2 * A;
	 X2= -1 * B - sqrt(DISC) / 2 * A;	 




The order of operations calculate this as -b + (sqrt(DISC) / 2) * A.

Change it to this:

	 X1= -1 * B + sqrt(DISC) / (2 * A);
	 X2= -1 * B - sqrt(DISC) / (2 * A);	 


This post has been edited by SixOfEleven: 16 October 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 AhmerjavedC++   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Need help with logical error

Posted 18 October 2009 - 08:40 PM

Thanks, Program works perfectly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1