All pastes #179821 Raw Edit

Miscellany

public text v1 · immutable
#179821 ·published 2006-09-22 15:42 UTC
rendered paste body
/*	Lab2_2
	Solves Quadratic Equations
	Using the following definitions;
	
	ax^2 + bx + c = 0
	discriminant (d) = b^2 - 4ac

	Inputs; a, b, c
	Outputs; Root1, Root2

	P Norwood
	25 September 2006
*/

#include <iostream>
#include <math.h>// may require seperate library, math? for more complex math functions, ie exponents
using namespace std;

int main()
{
	double a, b, c, x, d, r1, r2;

	cout << "Press Return After Entering a Number\n";
	cout << "Enter a value for 'a'\n";
	cin >> a;
	cout << "Enter a value for 'b'\n";
	cin >> b;
	cout << "Enter a value for 'c'\n";
	cin >> c;

	cout << "The equation you have supplied is: "
		<< "("<< a << "x" << ")" << "^2 + " << b << "x" << " + " << c << "\n";

	d = (b*b) - (4*(a*c));

	if (d < 0)
	{
		cout << "The Discriminant Was Less Than 0. There Are No Real Roots, Only Imaginary.\n";
	}
	if (d == 0)
	{
		r1 = ((-b) / 2*a);
		cout << "There Is One Real Root: " << r1 << "\n";
	}
	if (d > 0)
	{
		r1 = ((-b) + sqrt (d)) / (2*a);
		r2 = ((-b) - sqrt (d)) / (2*a);
		cout << "There Are Two Real Roots.\n Root One: " << r1 << "\n Root Two: " << r2 << "\n";
	}

	return 0;
}

/*
Sample Run 1
Press Return After Entering a Number
Enter a value for 'a'
3
Enter a value for 'b'
1
Enter a value for 'c'
5
The equation you have supplied is: (3x)^2 + 1x + 5
The Discriminant Was Less Than 0. There Are No Real Roots, Only Imaginary.

Sample Run 2
Press Return After Entering a Number
Enter a value for 'a'
2
Enter a value for 'b'
4
Enter a value for 'c'
2
The equation you have supplied is: (2x)^2 + 4x + 2
There Is One Real Root: -4

Sample Run 3
Press Return After Entering a Number
Enter a value for 'a'
1
Enter a value for 'b'
5
Enter a value for 'c'
2
The equation you have supplied is: (1x)^2 + 5x + 2
There Are Two Real Roots.
 Root One: -0.438447
 Root Two: -4.56155
*/