This program will solve quadratic equations.
You can copy and paste it into an editor, save it and compile from there.I have compiled and run this on
a RedHat GNU/Linux machine using kernel 2.4.20-6 and it works. If you are using another system ( windows or
mac you may need to change the coding ).
To compile: name it quadsolv.cpp then type
< g++ -o quadsolv quadsolv.cpp >
(not the < or the >) then hit enter. When your command prompt returns
type < ./quadsolv > and hit enter then follow the directions.
You may need to use math.h like this: #include <math.h> if you are
using windows C++ programming software. (I tried it without the math.h and
got an "undeclared identifier" error)
Thanks to "Shahi" for pointing out some errors in my coding. I have fixed them.
update 2005-03-24:
added math.h. The program would not compile on Fedora core 3 without adding this.
Compiled ok on my machine after adding math.h again ( gcc -dumpversion 3.4.2 ; Fedora core 3 )
uname -a: Linux illiac.perpetualpc.net 2.6.9-1.667 #1 Tue Nov 2 14:41:25 EST 2004 i686 i686 i386 GNU/Linux
update: 2008-07-12: I recieved a rather caustic email from an instructor at Belcampo in Groningen, Holland
suggesting I remove this page because it should not be on the web: "IMHO the internet should not be cluttered by terrible code like this".
Apparently, the purpose of this code is not clear to all who read it - so, again, as clearly stated above:
THIS IS A LEARNING TOOL.
All programmers must start at the beginning in understanding a programming language - any language. This program is not intended to be
an example of "professional" C++ coding; it is meant to show basic ideas and practices necessary in writing a program that works
for the user. The most elegant code in the world is worthless if it does not work for the typical user. The typical user is NOT a professional coder,
or a geek, or an overcritical instructor, but they too, must be considered - that is why this note is here and why the first comment in the code shown below is there.
//copy the part below this
/*program to find the solutions to a quadratic equation in the standard form ax^+bx+c=0.
Author David Tarsi. Revised 2008-06-20. All other versions are obsolete and may cause
problems. The logic portion of this program was developed from
instruction. The coding is by the author. This code is long because it covers all possible inputs and allows for operation
from the command line. To be useful a program MUST allow for ALL reasonable user input - its not about the programmer or the program its about the
user. A program written for the programmer is only useful to the programmer.
This program could be shortened consideralbly by eliminating the "all possible combinations" written into it - if
you have a specific purpose for the output - keep in mind, however, that
"quadratic" equations can come in many forms. Often times the coefficents of a or b are 0, and solutions can be complex
numbers. There are many programs that solve only for the "real" solutions - or just for conditions when the coefficients
of a or b are non-zero numbers. These are incomplete programs. Many students have trouble with quadratic equations because they
do not understand these concepts.
This program compiles and runs on FC 6. (2.6.18-1.2798.) Any questions or comments welcome
at hal.daveNOSPAM@NOSPAM.gmail.nospam.com*/
#include <iostream>
#include <math.h>
#include <cctype>
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";
}
char g;
cout << "Solve another equation? (y for yes or n for no) \n";
cin >> g;
if(g !='y'){
exit(0);
}
return;
}
//keep output from vanishing before we can read it.
void two(){
char g ;
cout << "Press c and then Enter to continue...." "\n";
cout << "Press s and then enter to stop....." "\n";
cin >> g;
if (g == 'c')
for(int i=0; i<100; i++)// who wants to run this more that 99 times? if you are using this program for
//any thing other than some quick calculations (for example as part of another program)
// you may want to comment this part out. The for loop keeps the program from
//running away in an infinate loop, but may not be usable in another program.
//If 99 times is not enough, change the value in the for loop, but be careful
//with no limits the for loop can run the program in the background by accident
one();
}
int main(){
one();
two();
return 0;
}
//copy the part above this
Back to Link page |
Back to javquad page
Page by:
Perpetual PC's
Copyright (c) 2002 David Tarsi.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A copy of the license is included in the section entitled
"GNU GPL".