// 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. -
#include <iostream>
-
#include <cmath>
-
#include <complex>
-
using namespace std;
-
-
int main()
-
{
-
char cAgain;
-
int iDataset = 1;
-
double dRoot1, dRoot2, dValuea, dValueb, dValuec, dDescriminant;
-
-
cout.setf(ios::fixed);
-
cout.setf(ios::showpoint);
-
cout.precision(4);
-
-
do
-
{
-
cout << endl << "****************" << endl
-
<< "* Data Set " << iDataset << " *" << endl
-
<< "****************" << endl << endl;
-
cout << endl << "Please enter coefficients for quadratic equation"
-
<< " ax^2 + bx + c = 0" << endl;
-
cout << endl << "Value of 'a' : ";
-
cin >> dValuea;
-
cout << "Value of 'b' : ";
-
cin >> dValueb;
-
cout << "Value of 'c' : ";
-
cin >> dValuec;
-
dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
-
-
if(dDescriminant < 0)
-
{
-
cout << endl << "You have entered the equation " << dValuea
-
<< "x^2 + " << dValueb << "x +" << dValuec
-
<< " = o" << endl;
-
cout << "The root to this equation is: " << endl << endl
-
<< dRoot1 << " i and" << endl << dRoot2 << endl << endl;
-
-
-
}
-
else if(dDescriminant == 0)
-
{
-
cout << endl << "You have entered the equation " << dValuea
-
<< "x^2 + " << dValueb << "x +" << dValuec
-
<< " = 0" << endl;
-
cout << "The root to this equation is: " << endl << endl
-
<< dRoot1 << endl;
-
}
-
else
-
{
-
cout << endl << "You have entered the equations " << dValuea
-
<< "x^2 + " << dValueb << "x +" << dValuec
-
<< " = o" <<endl;
-
cout << "The root to this equations is: " << endl << endl
-
<< dRoot1 << " and" << endl << endl << dRoot2 << endl;
-
}
-
cout << endl << "Want to do this again? Y/y/N/n" << endl << endl;
-
cin >> cAgain;
-
iDataset++;
-
}while (cAgain == 'Y' || cAgain == 'y');
-
-
return 0;
-
-
}
-
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.
2 2127 horace1 1,510
Recognized Expert Top Contributor
you need to test -
dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
-
if(dDescriminant < 0)
-
{
-
before you calculate dRoot1 and dRoot2
if dDescriminant>0 is real rools (note your calculation is not quite correct) -
dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
if dDescriminant< 0
the real part is -
- dValueb /( 2 * dValuea)
-
the imaginary parts are -
+ and - sqrt(-dDescriminant) / (2 * dValuea)
-
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 -
dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
-
if(dDescriminant < 0)
-
{
-
before you calculate dRoot1 and dRoot2
if dDescriminant>0 is real rools (note your calculation is not quite correct) -
dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
-
if dDescriminant< 0
the real part is -
- dValueb /( 2 * dValuea)
-
the imaginary parts are -
+ and - sqrt(-dDescriminant) / (2 * dValuea)
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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 ...
|
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.
|
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
|
by: sjabang1 |
last post by:
Hay guys i want to programme a quadratic of this nature
ax2+bx+c=o
| |
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.
|
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>
|
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
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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...
| | |