Register to reply 
C++ and solving quadratic Equations 
Share this thread: 
#1
Sep206, 06:09 AM

P: 398

Today, I tried to translate the very basic "solve quadratic equations" program I made a while back in Python to C++.
#Solving quadratic equations import math print "Please enter information in accordance with ax^2 + bx + c = 0" a = input("What is a?") b = input("What is b?") c = input("what is c?") b = float(b) / float(a) c = float(c) / float(a) before_the_square = 0.5 * b * 1 the_square = (0.5 * b)**2  c if the_square < 0: print "Imaginary" #failsafe for imaginary numbers. else: value_of_square = math.sqrt(the_square) x1 = before_the_square + value_of_square x2 = before_the_square  value_of_square print "x(1) is:" print x1 print "x(2) is:" print x2 I tried something like this in C++: // Solving quadratic equations #include <iostream> #include <string> #include <math.h> using namespace std; main () { float a, b, c, before_sqrt, inside_sqrt, after_sqrt, x1, x2; cout << "Solving quadratic equation" << endl << endl; cout << "ax^2 + bx + c = 0"; cin >> a, b, c; b = b / a; c = c / a; before_sqrt = (b/2)*1; inside_sqrt = pow(0.5b,2)  c; if (inside_sqrt < 0) { cout << "Error"; } else after_sqrt = sqrt (inside_sqrt); x1 = before_sqrt + after_sqrt; x2 = before_sqrt  after_sqrt; cout "x1 =" << endl << endl << x1; cout "x2 =" << endl << endl << x2; system("pause"); return 0; } The indent is wrong due to copy/pasting and I have not removed the scaffolding. I'm pretty sure I made some mistakes with the declaration in the beginning as well as the difficulty to use exponential with variables (red), altohugh I'm not sure how to solve it. Any help is greatly appreciated. Thank you for your time 


#2
Sep206, 09:05 AM

P: 1,373

whhat is "0.5b" suppose to do? What other errors do you get?



#3
Sep206, 10:17 AM

P: 398

It is suppose to do 0.5*b.
Oh, by writing 0.5*b, that particular error goes away. I've alos missed to add int to main() The only error messages I am getting now is: Edit: as it turns out I forgot the ">>" in " cout >> "x1 =" << endl << endl << x1;" problem solved. 


#4
Jun711, 01:13 PM

P: 1

C++ and solving quadratic Equations
This is the corrected way of writing the program...
I've not really checked the logic, but this is the corrected way of writing what you want to. Try and avoid spaces. #include<iostream.h> #include<conio.h> //I've put this for the clrscr() function. #include<string.h> #include<math.h> int main () { clrscr(); //It makes sure the screen becomes clear when you run the program again. float a, b, c, before_sqrt, inside_sqrt, after_sqrt, x1, x2; cout<<"Solving quadratic equation"; cout<<"ax^2 + bx + c = 0"; cin>>a>>b>>c; //You have to take the parameters like this b=b/a; c=c/a; before_sqrt = (b/2)*(1); inside_sqrt = pow(0.5*b,2)c; if (inside_sqrt < 0) cout<<"Error"; else after_sqrt=sqrt (inside_sqrt); x1=before_sqrt+after_sqrt; x2=before_sqrtafter_sqrt; cout<<"x1 ="<<endl<<endl<<x1; cout<<"x2 ="<<endl<<endl<<x2; return 0; //You need int main() for this statement } 


#5
Jun711, 01:39 PM

P: 296

Although the unportable system("pause") does hint he's using Windows, I suggest not even using that. Why write unportable programs when it's no harder to write it portably (in this case)? No need to clear the screen, and something like cin.get() at the end should work as well as the pause. Adding Windows conio stuff makes it more unportable and more nonstandard. Matara, this line still isn't quite right: cout >> "x1 =" << endl << endl << x1; It should be: cout << "x1 =" << endl << endl << x1; All the operators should be <<, not >>. They go the other way for input. For example:
x1 = 42 Most people would stick to: cout << "x1 = " << x1 << endl; But that's up to you. As Pratibha_S pointed out, this is wrong: cin >> a, b, c; And should be: cin >> a >> b >> c; When you enter them, they should be separated by spaces. Might not be a bad idea to point that out in a prompt. And I guess you know about the missing "int " in front of your main function. Lastly, when you post code, to preserve the rather important indentation, put your code between CODE tags. Put [ CODE] in front and [ /CODE] at the end, removing the spaces I put in after the opening square brackets. 


#6
Jun711, 02:04 PM

Mentor
P: 21,397

Some additional comments...
1. The way you did input in your python version is better than how you did it in your C++ version. python version:
2. You are calculating the roots of the quadratic in a nonintuitive way.
Instead of resetting b and c as you are doing, why not leave them as they are? The thing inside the square root is called the discriminant. It's useful to calculate it before taking its square root.
Assuming the discriminant was nonnegative, the two roots are:



Register to reply 
Related Discussions  
Quadratic equations and inequalities / applications of quadratic functions question  Precalculus Mathematics Homework  3  
Quadratic Equations and Inequalities question about properties of quadratic  General Math  2  
Solving quadratic congruences  Calculus & Beyond Homework  1  
Solving a quadratic  General Math  4 