473,583 Members | 3,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if variable is defined

2 New Member
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 11413
risby
30 New Member
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
tycoonius
2 New Member
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
4774
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 if a variable exists. <xsl:if test="$testvariable"> is false when the variable exists. Is there any test that delivers "true" if the variable...
5
4810
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 parameters that all contain the names of various XML tags. Foo.xsl then performs a standard action (outputting the information in HTML) on the tag given...
26
9595
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
24797
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 contains the subform is named Clients. I have this code in the Add Client btn: If Forms!Clients.subformVisits!VisitDirty = True Then MsgBox "Visit...
5
31527
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
44506
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? Regards, Wessel
10
19970
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
3855
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
10155
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() { printf("Hey,you're been attacked.\n");
3
6310
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, which is called once the user clicks the OK button on this dialog. try { int rows = cmd.ExecuteNonQuery(); } catch(SqlException se)
0
7890
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...
0
8168
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. ...
0
8316
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...
1
7926
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8187
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...
0
6573
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5367
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...
0
3812
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...
0
1149
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...

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.