Roots.cpp Program help.

Hey there C++ programmers, I need some help getting my code. I'm stuck on a homework assignment and need a little help clarifying what needs to be done. I'm running everything in Visual Studio 2010 Professional (well I'm supposed to, but I can't Debug, so im using an online compiler )
Here's what I've got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath> //square root function
using namespace std;


int main () // my main
{
	
		double a,b,c,x1,x2; // my doubles
	
	cout<<"Enter value a:";
	cin>>a; 
	// promts the user for the A value

	cout<<"Enter value b:";
	cin>>b;
	// promts the user for the B value

	cout<<"Enter value c:";
	cin>>c;
	// promts the user for the C value

	x1= (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x1, which is the positve root

	x2= (-b - sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x2, which is the negative root
	
	
	cout << "X1: " << x1 << endl; // displays value of X1 or the first root
	cout << "X2: " << x2 << endl; // displays value of X2 or the second root

	cout << "Have a nice day :) " << endl;
	// informs to have the user to have a nice day

	return 0;	//returns zero to terminate program without error
}


There are 3 criteria that I can't seem to get/understand.

The discriminant, b2 - 4ac, describes the roots of a quadratic equation: if it is greater than or equal to 0, there are two real roots; and if it is less than 0, there are two complex roots.

a. Complex numbers are displayed with a real part and an imaginary part: e.g., 6 + 3i and 6 - 3i

b. The real part (6 in the example) is calculated by the formula -b/2a

c. The imaginary part (3i in the example) is calculated by the formula
sqrt(descriminant)/2a with an ā€˜iā€™ character printed at the end

d. Taking the square root of a negative number with the sqrt function causes a run-time error, so the discriminant must be negated (i.e., multiplied by -1) before taking the square root

3. Your program should find the roots in all three situations (the lab 1 version should work when the discriminant = 0 or > 0); your modifications for lab 2 should handle the case when the discriminant is < 0.

The test cases should equal as follows:

a = 2, b = 2, c = 2; x1 = -0.5 + 0.866025i, x2 = -0.5 - 0.866025i
Last edited on
What you have so far will only work for a positive discriminant.
Let a= 1, b=3, c=1 then b^2 - 4ac = 3^2 - 4*1*1 = 5
5 is a positive number so there exists a square root in the real numbers

Whereas if u let a= 1, b=1, c=1 then b^2 - 4ac = 1^2 - 4*1*1 = -3
-3 is negative so sqrt(-3/something) is not defined in the real numbers. To fix this, u have to multiply that -3 by -1 (where -1 = i^2) which makes the discriminant positive. Then, u can compute that root: ie: sqrt(3i^2/something)

To help visualize, imagine trying to find a number whose square is a negative number. Impossible with real numbers right ? What multiplied by itself is -4 ? ie: x^2 = -4 ?

You'll have to create test cases to handle the different signs of the discriminant by itself before taking the square root of it.
Last edited on
That actually explains A LOT :) +1 soranz

I should have some more code tonight when I get off work for some additional help if I get stuck
This is what I've got so far; not sure where I'm going wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <iostream>
#include <cmath> 
#include <stdlib.h>
using namespace std;

int main () // my main
{

	double a,b,c,; // my doubles
	float discriminant, real_1, real_2, I_1, I_2;

	cout<<"Enter value a:";
	cin>>a; 
	// promts the user for the A value

	cout<<"Enter value b:";
	cin>>b;
	// promts the user for the B value

	cout<<"Enter value c:";
	cin>>c;
	// promts the user for the C value
	
	discriminant = ((b * b)- 4 * a * c);


	if (a==0)
	{
		cerr << " 'A' cannot be equal to Zero" << endl;
		exit (0); 
	}
	
	else if (discriminant >= 0)
	{
	real_1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x1, which is the positve root
	real_2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x2, which is the negative root
	
	}

	else if (discriminant < 0)
	{

	//I_1 = ();

	//I_2 = ();

	}

	cout << "X1: " << real_1 << endl; // displays value of X1 or the first root
	cout << "X2: " << real_2 << endl; // displays value of X2 or the second root

	cout << "Program Terminated" << endl;
	// informs to have the user to have a nice day

	return 0;	//returns zero to terminate program without error
}
For the complex part just multiply the determinant by -1 and then u can take the sq root. The only difference vs the real_1 is that u have to keep track of the part that is real and the part that is imaginary. This means having 2 expressions:
realpart = -b/ (2 * a);

imaginarypart = sqrt(b * b - 4 * a * c)) / (2 * a);

And when u print it out it has to be in the form:
cout << realpart << " +- " << imaginarypart << "i" << endl;
Where the 'i' represents the imaginary part.
I thought lines 36 + 37 were taking care of the reals; and then the else if on 41-49 would be for the I_1 + I_2 (imaginary) I had to comment them out till i filled them in

so what it would look like (again, at work till late)
It might help if u read on wiki what a complex number is and a couple of basic examples on how +-/* arithmetic works with them. Technically, all real numbers can be considered complex such that the imaginary component is 0.
consider the real: 1 + 1 == 2
and the equivalent in complex: 1 + 0i + 1 + 0i == 2 + 0i == 2
but 0i = 0 so just the real part of the number remains.

Lines 36, 37 are fine as they are. U will also have 2 different sets of 'cout'. The ones on line 50 and 51 will only work for real_1/real_2.
U'll need 2 others (as I outlined in my previous post) to format the correct complex forms that were determined on lines 43-47
Thanks again for sure :)
Me again. I get an error when I run it. This is my Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath> 
#include <stdlib.h>
using namespace std;

int main () // my main
{
	float discriminant, real_1, real_2, I_1, I_2, a,b,c;

	cout<<"Enter value a:";
	cin>>a; 
	// promts the user for the A value

	cout<<"Enter value b:";
	cin>>b;
	// promts the user for the B value

	cout<<"Enter value c:";
	cin>>c;
	// promts the user for the C value
	
	discriminant = ((b * b)- 4 * a * c);


	if (a==0)
	{
		cerr << " 'A' cannot be equal to Zero" << endl;
		exit (0); 
	}
	
	else if (discriminant >= 0)
	{
	real_1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x1, which is the positve root
	real_2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); //This gets the root of x2, which is the negative root
	
	}

	else if (discriminant < 0)
	{

	I_1 = sqrt(b * b - (4 * a * c)) / (2 * a);
	I_2 = sqrt(b * b - (4 * a * c)) / (2 * a);

	}

	cout << "X1: " << real_1 << endl; // displays value of X1 or the first root
	cout << "X2: " << real_2 << endl; // displays value of X2 or the second root

	cout << real_1 << " +- " << I_1 << "i" << endl;
	cout << real_2 << " +- " << I_2 << "i" << endl;

	cout << "Program Terminated" << endl;
	// informs to have the user to have a nice day

	return 0;	//returns zero to terminate program without error
}


my error is:

Run-Time Check Failure# 3- The variable 'real_1' is being used without being initialized.
If the discriminant is < 0 then you never assign anything to real_1 but try to print it anyways. Same with I_1 and I_2.
Hi MrHatchi87,

Floating point numbers (floats & doubles) are a bit of a pain because you shouldn't do comparisons like this:

(discriminant < 0)

The reason is floating point is represented with binary fractions, which cannot represent all real numbers. 0.1 is one of them, so this will always fail:

1
2
double a = 0.1; //probably == to 0.0999999999999997 or some thing similar
if (10 * a == 1.0) //fail == 0.9999999999999997 


The solution is to use numeric_limits<double>::epsilon( )

This is the 'distance' between 1.0 and the next representable number. The epsilon should be scaled up if your number is bigger than 1.0, so if the number is 1000.0 use 1000.0* numeric_limits<double>::epsilon( ).

I would assign this value to it's own variable to make things easier.

To compare with zero, calc the absolute value of your number minus zero and compare with epsilon.

Floating point is a pain, but you get used to it.

Google to find out more.

HTH
Topic archived. No new replies allowed.