473,395 Members | 1,530 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,395 software developers and data experts.

Check if variable is defined

I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that.

Here's the relevant parts:
Expand|Select|Wrap|Line Numbers
  1. double a, b, c, root1, root2;
  2.     cout << "Enter the 3 coefficients of the quadratic equation." << endl;
  3.     cin >> a >> b >> c;
  4.     if(cin)
  5.     {
  6.         cout << "Last input was successful." << endl;
  7.         cout << a << "x" << (char)253 << " + " << b << "x + " << c << endl;
  8.         root1 = (-b + sqrt(pow(b,2)+4*a*c)) / (2*a);
  9.         root2 = (-b - sqrt(pow(b,2)+4*a*c)) / (2*a);
  10.         if(root1 && root2)
  11.             cout << "There are two real roots." << endl;
  12.         else if(root1 || root2)
  13.             cout << "There is one real root." << endl;
  14.         else cout << "All roots are complex numbers." << endl;
  15.         if(root1)
  16.             cout << "Root 1 = " << root1 << endl;
  17.         if(root2)
  18.             cout << "Root 2 = " << root2 << endl;
  19.     }
  20.     else
  21.         cout << "Last input was unsuccessful. Check your data and try again." << endl;
Obviously, those if statements aren't of much use. So, is there some method or something to test the variables to see whether they're defined?
Sep 18 '06 #1
2 11385
risby
30
I need help with this program and I'm not sure how to do something. The program is supposed to take 3 doubles to make a quadratic equation, then calculate and display all real roots. Here's the problem: If one or more of the roots is unreal or complex, it tries to display them anyway because I don't know how to test for that.
the equation you are solving is ax^2 + bx + c = 0 and you are using the formula -b +/- sqrt(b^2 - 4ac) / 2a to solve it

but wait, if a = 0 there is no ^2 term so the equation is the rather simpler linear equation bx + c = 0

if you used the quadratic equation when a = 0, and therefore also 2a = 0, you'd be dividing by 0. this is naughty!

so when a=0 use:

bx + c = 0
or
bx = 0 - c
or
x = -c / b

to find the single real root

otherwise

calculate the determinant, that bit of the formula of which you are going to take the square root. If the determinant is zero the roots are co-incident. Imagine the U shaped curve sitting on the x-axis.

If the determinant is zero the formula is -b +/- sqrt(0) / 2a which simplifies to -b/2a since +/- sqrt(0) is also zero.

So when the determinant = 0 use

x = -b / 2a

to find the single real root.

On the other hand if the determinant is less than zero, because the square root of -1 is i, the roots will have an imaginary component and you will have to use complex numbers in the formula

But if the determinant is positive and non-zero then you can use the formula in the way you are doing already.

Hope this helps.
Sep 18 '06 #2
Yeah, that was a big help. Thanks.
Sep 18 '06 #3

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

Similar topics

0
by: Thomas Scheffler | last post by:
Hi, I have several xslt documents that import each other in a specific way to allow resulting documents to be layout in a common way. In the "master stylesheet" I need some kind of test to check...
5
by: Brandolon Hill | last post by:
Hi, Is there a way to check and see if a parameter is defined in XSLT? I have many stylesheets that all have an <xsl:include> of the same stylesheet, "foo.xsl". Foo relies on several...
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
15
by: Rey | last post by:
Howdy all. Appreciate your help with several problems I'm having: I'm trying to determine if the Visit subform (subformVisits) has a new record or been changed, i.e. dirty. The form that...
5
by: DJTB | last post by:
Dear Group, I'd like to check if a value is defined in an enum. Example: ------------------------------------------------------ typedef enum { A_VALUE = 1,
7
by: Wessel Troost | last post by:
Hi Group, How can you check if a variable is defined in C#? For example, "string s;" would declare an undefined variable. But how can I check in an "if" statement wether it's defined or not?...
10
by: Patrick B | last post by:
Is there a way to check if a certain variable is an enum? Example code: public enum MyEnum { Monday, Tuesday, Wednesday } public void MyMethod()
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
8
by: Antony | last post by:
compiler£ºVisual Studio.Net 2003 (VC7.1) compile type£ºDebug problem: wanted more information about the "Run-Time Check Failure #n",thanks! Example1: #include "stdafx.h" void malice() {...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.