473,769 Members | 7,315 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quadratic equations, need some help

8 New Member
// 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 2127
horace1
1,510 Recognized Expert Top Contributor
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
DaRok28
8 New Member
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
1835
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 squares, fractions, powers, square roots, summations, greek characters etc. The question is: what is the best way to display these equations on
6
2330
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 what's up? Is there maybe some kind of standard Quadratic function hiding away in the libraries? That would be nice to have...Here's my source: /* Trying to calculate the real roots of "ax^2 + bx + c = 0" using the quadratic formula ...
8
4810
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 write a code and solve these equations in C or C++? I am not an expert, but have elementary working knowledge of C.
3
5176
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
1762
by: sjabang1 | last post by:
Hay guys i want to programme a quadratic of this nature ax2+bx+c=o
2
2080
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
2614
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!! #include <stdio.h> #include <stdlib.h> #include <math.h>
49
2545
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 parameter table (parmas) which has all the values assigned to them for example: BBackVoid = 22 CarcT = 18 Default Depth = 300
1
3208
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 – a12a21
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.