473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Quadratic equations, need some help

// Program Description:
// This program solves quadratic equations to find their roots. This
// program takes values of a, b, and c as input and outputs the root(s).
// The user can repeat the calculation for as many equations as they like.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. #include <complex>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.    char cAgain;
  9.    int iDataset = 1;
  10.    double dRoot1, dRoot2, dValuea, dValueb, dValuec, dDescriminant;
  11.  
  12.    cout.setf(ios::fixed);
  13.    cout.setf(ios::showpoint);
  14.    cout.precision(4);
  15.  
  16.    do
  17.    {
  18.       cout << endl << "****************" << endl
  19.            << "* Data Set " << iDataset << " *" << endl
  20.            << "****************" << endl << endl;
  21.       cout << endl << "Please enter coefficients for quadratic equation"
  22.            << " ax^2 + bx + c = 0" << endl;
  23.       cout << endl << "Value of 'a' : ";
  24.       cin >> dValuea;
  25.       cout << "Value of 'b' : ";
  26.       cin >> dValueb;
  27.       cout << "Value of 'c' : ";
  28.       cin >> dValuec;
  29.       dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  30.       dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  31.       dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
  32.  
  33.          if(dDescriminant < 0)
  34.          {
  35.             cout << endl << "You have entered the equation " << dValuea
  36.                  << "x^2 + " << dValueb << "x +" << dValuec
  37.                  << " = o" << endl;
  38.             cout << "The root to this equation is: " << endl << endl
  39.                  << dRoot1 << " i  and" << endl << dRoot2 << endl << endl;
  40.  
  41.  
  42.          }
  43.          else if(dDescriminant == 0)
  44.          {
  45.             cout << endl << "You have entered the equation " << dValuea
  46.                  << "x^2 + " << dValueb << "x +" << dValuec
  47.                  << " = 0" << endl;
  48.             cout << "The root to this equation is: " << endl << endl
  49.                  << dRoot1 << endl;
  50.          }
  51.          else
  52.          {
  53.             cout << endl << "You have entered the equations " << dValuea
  54.                  << "x^2 + " << dValueb << "x +" << dValuec
  55.                  << " = o" <<endl;
  56.             cout << "The root to this equations is: " << endl << endl
  57.                  << dRoot1 << " and" << endl << endl << dRoot2 << endl;
  58.          }
  59.       cout << endl << "Want to do this again? Y/y/N/n" << endl << endl;
  60.       cin >> cAgain;
  61.       iDataset++;
  62.    }while (cAgain == 'Y' || cAgain == 'y');
  63.  
  64.    return 0;
  65.  
  66. }
  67.  
My else if and else statements both work the way I want them too, but I need my if tatement to do negative numbers. When I enter the values for a, b, c as
1, 4, a nd 7 respectfully I need the answer to be -2.0000 - 1.7321 i and -2.0000 + 1.7321 i. I am just not sure how to do this, so if anyone has any suggestions.
Mar 5 '07 #1
2 2103
horace1
1,510 Expert 1GB
you need to test
Expand|Select|Wrap|Line Numbers
  1.      dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
  2.          if(dDescriminant < 0)
  3.          {
  4.  
before you calculate dRoot1 and dRoot2

if dDescriminant>0 is real rools (note your calculation is not quite correct)
Expand|Select|Wrap|Line Numbers
  1.       dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  2.       dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  3.  
if dDescriminant< 0
the real part is
Expand|Select|Wrap|Line Numbers
  1.         - dValueb /( 2 * dValuea)
  2.  
the imaginary parts are
Expand|Select|Wrap|Line Numbers
  1.    + and -   sqrt(-dDescriminant) / (2 * dValuea)
  2.  
Mar 5 '07 #2
Thanks, I am not sure if what I did is correct, but i went off of what you told me and it worked, so thanks

you need to test
Expand|Select|Wrap|Line Numbers
  1.      dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
  2.          if(dDescriminant < 0)
  3.          {
  4.  
before you calculate dRoot1 and dRoot2

if dDescriminant>0 is real rools (note your calculation is not quite correct)
Expand|Select|Wrap|Line Numbers
  1.       dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  2.       dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
  3.  
if dDescriminant< 0
the real part is
Expand|Select|Wrap|Line Numbers
  1.         - dValueb /( 2 * dValuea)
  2.  
the imaginary parts are
Expand|Select|Wrap|Line Numbers
  1.    + and -   sqrt(-dDescriminant) / (2 * dValuea)
  2.  
Mar 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: smjmitchell | last post by:
Hi All, I need to display some equations on a form in VB (I will print the result in a text box beside the equation). The equations will in some cases be quite complicated and include...
6
by: fb | last post by:
Hello everyone. I'm having a touch of trouble solving a problem using the quadratic formula. I get a domain error...Somewhere in the sqrt function I think. Could you guys give me a hint on...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
3
by: amitsoni.1984 | last post by:
Hi, I need to do a quadratic optimization problem in python where the constraints are quadratic and objective function is linear. What are the possible choices to do this. Thanks Amit
3
by: sjabang1 | last post by:
Hay guys i want to programme a quadratic of this nature ax2+bx+c=o
2
by: VanHayden | last post by:
hey i need help with this word problem ive tried it but cant figure it out: Kylie bought an item for $x and sold it for $10.56. If Kylie incurred a loss of x per cent, find x.
2
by: ioannoual | last post by:
Hi...I 'am new in C and I want a program that solves a quadratic equation!! I try something by my self to write some code about that but I want you to help me writing a new one that will work!! ...
49
by: Constantine AI | last post by:
Hi i am trying to calculate two fields 'AcrossGrain' and 'WithGrain' both of which have equations assigned to them for example: Equations: Depth-BBackVoid-CarcT or Height-CarcT*2 I do have a...
1
by: HypeBeast McStreetwear | last post by:
Hello everyone. I got a assignment that states. The set of linear equations a11X1 = a12X2 = c1 a21X1 = a22X2 = c2 May be solved using Cramer’s rule: X1 = c1a22 – c2a12 a11a22 –...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.