C program
/* quadratic.c */
/*
Program to evaluate real roots of quadratic equation
2
a x + b x + c = 0
using quadratic formula
2
x = ( -b +/- sqrt(b - 4 a c) ) / (2 a)
*/
#include
#include
int main()
{
double a, b, c, d, x1, x2;
/* Read input data */
printf("\na = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
/* Perform calculation */
d = sqrt(b * b - 4. * a * c);
x1 = (-b + d) / (2. * a);
x2 = (-b - d) / (2. * a);
/* Display output */
printf("\nx1 = %12.3e x2 = %12.3e\n", x1, x2);
return 0;
}
C++ program (with also imaginary roots)
#include
#include
using namespace std;
void one(){
float a = 0.0; //here we declare the variables and use float because we
float b = 0.0; //are dealing with square roots
float c = 0.0;
float x1 = 0.0;
float x2 = 0.0;
float x3 = 0.0;
float x4 = 0.0;
//this section gets user input and displays message
cout << "Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:\n";
cout << "Enter value for a:\n";
cin >> a;
cout << "Enter value for b:\n";
cin >> b;
cout << "Enter value for c:\n";
cin >> c;
//are all the coefficients 0? if so both roots are 0
if(a == 0 && b == 0 && c == 0){
x1 = 0;
x2 = 0;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//is c the only non-zero number? if so tell the user
if(a == 0 && b == 0 && c != 0){
c = c;
cout << "There are no roots" "\n"
<< "c = " << c << "\n";
}
//is a zero? if so solve the resulting linear equasion and notify user
if(a == 0 && b != 0 && c !=0){
cout << "The values entered do not make a quadratic expression" "\n"
<< "x = " << -c/b << "\n";
}
//if b is zero and c is zero tell user
if(a == 0 && b != 0 && c == 0){
x1 = 0;
x2 = 0;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//if b and c are equal to zero then ax^=0 and since a cannot be zero without x being
// zero also let user know
if(a != 0 && b == 0 && c == 0){
x1 = 0;
x2 = 0;
cout << "The values entered result in ax^= 0; so both roots are 0" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//factor out x from ax^+bx=0 and either x = 0 or ax + b =0
//then solve the linear equation
if(a != 0 && b != 0 && c == 0){
x1 = 0;
x2 = -b/a;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//now we get to use the square root function and let the user
//know they have some imaginary numbers to deal with
if(a < 0 && b == 0 && c < 0){
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);
cout << "The roots are not real numbers:" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}
if(a > 0 && b == 0 && c > 0){
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);
cout << "The roots are not real numbers:" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}
//now a and c are opposite signs so the answer will be real
if(a > 0 && b == 0 && c < 0){
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);
cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = "<< x2 << "\n";
}
if(a < 0 && b == 0 && c > 0){
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);
cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = "<< x2 << "\n";
}
//ok now if we end up not having to take the square root of a neg
// do the math
if(a != 0 && b != 0 && c != 0 && (4*a*c) <= pow(b,2)){
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);
cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = " << x2 << "\n";
}
//here we have to deal with non x intercepts ie: sqrt(-1)
// alter the formula slightly to give correct output and
// let the user know
if(a != 0 && b != 0 && c != 0 && (4*a*c)> pow(b,2)){
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);
cout << "The roots are not real numbers" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}
return;
}
//keep output from vanishing before we can read it.
void two(){
char c ;
cout << "Press c and then Enter to continue...." "\n";
cin >> c;
for(;;){
if ( c ){
break;
}
}
cout << "Done" "\n";
}
int main(){
one();
two();
return 0;
}
2006-12-08 18:49:12
·
answer #1
·
answered by M. Abuhelwa 5
·
0⤊
0⤋
Was gonna write one, but why reinvent the wheel?
/*
* quad_eq.c
* This program reads the coefficients of a quadratic equation and solves
* the equation.
*/
#include
#include
/* Prototypes: */
int solve_quad (double a, double b, double c,
double *x1, double *x2);
int main ()
{
double a, b, c; /* The equation coefficients. */
double x1, x2; /* Its solutions. */
int n_sols; /* The number of real solutions. */
/* Read the coeffcients. */
printf ("Please enter the three coefficients of the quadratic equation: ");
scanf ("%lf %lf %lf", &a, &b, &c);
/* Solve the equation. */
n_sols = solve_quad (a, b, c,
&x1, &x2);
/* Print the solutions. */
switch (n_sols)
{
case 0:
printf ("The equation has no real solutions.\n");
break;
case 1:
printf ("The equation has a single solution: (x = %g).\n", x1);
break;
case 2:
printf ("The equation has two solutions: (x1 = %g) (x2 = %g).\n", x1, x2);
break;
default:
printf ("You must be kidding!\n");
return (1);
}
return (0);
}
/* ------------------------------------------------------------------------
* Function: solve_quad
* Purpose : Solve the quadratic equation: (a*x^2 + bx + c = 0).
* Input : a, b, c - The equation coefficients.
* Output : x1, x2 - The solutions.
* Returns : The number of real solutions to the equation (0, 1 or 2).
*/
int solve_quad (double a, double b, double c,
double *x1, double *x2)
{
double disc;
double sqrt_disc;
/* Check if this is really a linear equation. */
if (a == 0)
{
/* The equation is: (b*x + c = 0). */
if (b == 0)
return (0);
*x1 = -c / b;
return (1);
}
/* Compute the discriminant and act according to its sign. */
disc = b*b - 4*a*c;
if (disc < 0)
{
return (0);
}
else if (disc == 0)
{
*x1 = -b / (2*a);
return (1);
}
/* -b +/- sqrt(b^2 - 4ac)
* Use the formula: x1,2 = ------------------------
* 2a
*/
sqrt_disc = sqrt (disc);
*x1 = (-b + sqrt_disc) / (2*a);
*x2 = (-b - sqrt_disc) / (2*a);
return (2);
}
2006-12-08 17:48:37
·
answer #2
·
answered by Jim Burnell 6
·
2⤊
1⤋
it relatively is a screwy task. yet i think of that right here loop captures the standards: int errA = 0; int errB = 0; do { getA if( a is in blunders ) errA++; getB if( b <= 2 ) errB++; whilst( errA<3 && errB<3 && (errA+errB)<4 ); between the flaws that makes it screwy is that many times you will attempt thrice to get a the terrific option value of a previously shifting directly to get b. yet with a blended blunders cut back of four you may no longer try this, you will possibly desire to do it the way that i've got shown. additionally, you in all probability decide for some good judgment so as that as quickly as you get a the terrific option a and/or b, you bypass over soliciting for it back. i might use 2 bool variables like validA and validB, initialize them to fake previously the loop. Oh heck, i will in basic terms coach you: bool valA = fake; bool valB = fake; do { if( !valA ) { ...getA ...if( a is valid ) valA = genuine; ...else errA++; } // do the comparable for b // the the remainder of the loop is the comparable. } whilst( ...comparable as shown previously );
2016-10-14 07:57:34
·
answer #3
·
answered by Anonymous
·
0⤊
0⤋
Here's the IRS's approach to the quadratic formula:
http://www.cs.amherst.edu/~djv/irs.pdf
2006-12-08 18:01:29
·
answer #4
·
answered by modulo_function 7
·
0⤊
0⤋
do you know how to program in c?
i'd go for c++ because i don't remember c any more. but basically it's the same, you just use scanf instead of cin and printf instead of cout.
#include
using namespace std;
void main()
{
double a,b,c,x1,x2;
cout<<"a x^2 + b x + c = 0"
cout<<"Enter a\n";
cin>>a;
cout<<"Enter b\n";
cin>>b;
cout<<"Enter c\n";
cin>>c;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<"\n\nx="x1<<", x="<
}
2006-12-08 17:51:07
·
answer #5
·
answered by Anonymous
·
2⤊
0⤋