C++ and solving quadratic Equations

Click For Summary

Discussion Overview

The discussion revolves around translating a Python program for solving quadratic equations into C++. Participants explore various coding errors, syntax issues, and programming practices while discussing the logic behind solving quadratic equations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial Python code for solving quadratic equations and attempts to translate it into C++, noting issues with variable declarations and syntax.
  • Another participant points out a specific error related to the expression "0.5b" and suggests that it should be "0.5 * b".
  • A participant identifies additional syntax errors, including the need for ">>" in input statements and the correct declaration of the main function.
  • One participant offers a corrected version of the C++ code, emphasizing the importance of avoiding spaces in certain contexts and correcting the input method.
  • Another participant argues against the advice to avoid spaces, advocating for readability in code formatting and suggesting alternatives to platform-specific commands like "system('pause')".
  • A later reply critiques the logic of the quadratic formula implementation, suggesting that the discriminant should be calculated separately and that the original variables should not be altered.
  • Concerns are raised about potential divide-by-zero errors if the coefficient 'a' is zero, and an alternative method for calculating roots is proposed using the discriminant.
  • One participant suggests using double instead of float for better precision in calculations.

Areas of Agreement / Disagreement

Participants express various viewpoints on coding practices and logic for solving quadratic equations, with no consensus reached on the best approach or coding style. Multiple competing views remain regarding the implementation details and logic of the program.

Contextual Notes

Some participants note that the Python version of the program provides clearer prompts for user input compared to the C++ version. There are also discussions about the implications of using platform-specific functions and the importance of handling edge cases in mathematical computations.

Mattara
Messages
347
Reaction score
1
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
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" #fail-safe 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
print "x(1) is:"
print x1
print
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;
count << "Solving quadratic equation" << endl << endl;
count << "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[/color],2) - c;
if (inside_sqrt < 0)
{
count << "Error";
}
else
after_sqrt = sqrt (inside_sqrt);

x1 = before_sqrt + after_sqrt;
x2 = before_sqrt - after_sqrt;

count "x1 =" << endl << endl << x1;
count "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 :smile:
 
Technology news on Phys.org
whhat is "0.5b" suppose to do? What other errors do you get?
 
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:

Expected ";" before string constant

on line 30 and 31.

Edit: as it turns out I forgot the ">>" in "
count >> "x1 =" << endl << endl << x1;"

problem solved.
 
Last edited:
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;
count<<"Solving quadratic equation";
count<<"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)
count<<"Error";
else
after_sqrt=sqrt (inside_sqrt);

x1=before_sqrt+after_sqrt;
x2=before_sqrt-after_sqrt;

count<<"x1 ="<<endl<<endl<<x1;
count<<"x2 ="<<endl<<endl<<x2;

return 0; //You need int main() for this statement

}
 
Pratibha_S said:
Try and avoid spaces.

Sorry, but that is bad advice. Sure, you can't add spaces just anywhere you want, but things should be spaced out to make it more readable. Squishing everything together like you did is not good.

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 non-standard.

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:

Code:
char c;
cin >> c;

That's formatting it like you seemed to want to, but I'm not sure why you want two newlines between "x1 = " and the variable value. Right now, if x1 is 42, you'll end up printing:

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.
 
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:
Code:
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?")

C++ version:
Code:
cout << "ax^2 + bx + c = 0";
cin >> a, b, c;
Aside from the errors already noted, the python version is superior, from the perspective of a naive user of your program, because each variable has its own prompt. The prompt in the C++ version, "ax^2 + bx + c = 0" might be decipherable for you, the author of the program, but it doesn't offer much information for someone who isn't as familiar with your program.

2. You are calculating the roots of the quadratic in a nonintuitive way.
Code:
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;
If someone enters 0 for a, you are going to have a divide-by-zero error right off the bat.
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.
Code:
disc = b * b - 4 * a * c;
Then your code can check to see if disc is less than zero and handle complex roots or not. Notice that I used b * b instead of the pow() function.

Assuming the discriminant was nonnegative, the two roots are:
Code:
x1 = 1/(2a)*(-b + sqrt(disc));
x2 = 1/(2a)*(-b - sqrt(disc));

One other thing - your variables are of type float. For better precision I would use double.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 29 ·
Replies
29
Views
10K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K