1
\$\begingroup\$

Here is a simple Learner practice piece of Code, which supposses to comply and give solutions to a Quadrant Equity problem (requires to give it values of a, b and c for a Quadrant function). Please any tips and Tricks highly welcome, and suggestions of Improving the code. Thanks.

#include <iostream>
#include <math.h>
 
using namespace std;
 
void main(){
    double  a, b, c;
    cin >> a >> b >> c;
 
    if(a == 0){
        if(b == 0){
            if(c == 0){
                cout<<"All real numbers are solutions!"; //always zero
                return 0;
            }
            cout<<"No solutions!"; //no soultion Found
            return 0;
        }
        cout<<"Solution x0 is "<< -c / b; // Line equity
        return 0;
    }
 
    double delta = b*b - 4*a*c; // calculate the derivative for Quadrant
 
    if(delta<0){
        cout <<"No real solutions!"; //none Found
    }
    else if(delta==0){
        cout <<"Solution x0 is "<<-b / (2*a); //Double solution
    }
    else {
        cout <<"Solution x1 is "<<( -b - sqrt( delta ) ) / (2*a) << endl;
        cout <<"Solution x2 is "<<( -b + sqrt( delta ) ) / (2*a);
    }
 
    return 0;
}
 
\$\endgroup\$
10
  • \$\begingroup\$ looks the same as your previous post ? \$\endgroup\$ Commented Jan 2 at 19:23
  • \$\begingroup\$ “Quadrant equivalent solution” – do you mean “quadratic equation solution”? \$\endgroup\$
    – Martin R
    Commented Jan 2 at 19:25
  • \$\begingroup\$ @MartinR Bad language, sorry my bad. \$\endgroup\$ Commented Jan 2 at 19:26
  • 1
    \$\begingroup\$ @BillalBegueradj I had a mistake, my Intention had to be update to previous post, but I did a new post, now the previous post is under deletion to fix the mistake. \$\endgroup\$ Commented Jan 2 at 19:27
  • \$\begingroup\$ @PaleBroccoli else if(delta==0){ Shouldn't this rather compare against std::numeric_limits<double>::epsilon? \$\endgroup\$ Commented Jan 2 at 21:33

1 Answer 1

0
\$\begingroup\$

Let's start with one formal advise: Your formatting is inconsistent. In particular, placement of spaces around operators is inconsistent. Use your IDE's autoformatter.

Then, there are a few cases where comments basically just reflect the code. For example, cout<<"No solutions!"; //no soultion Found. The comment contains two typos and removing it would improve your code. As a general advise, don't document what the code does. Sparingly document how the code does something (e.g. a link to a page explaining how to solve quadratic equations) somewhere would help. Always document why your code does something the way it does, though there is little to explain in your code.

There is one thing in your workflow that's probably wrong. You declare void main() (which in and of itself is wrong), but then return values from it. Usually, the compiler will tell you about such nonsense, but I guess you didn't compile with warnings, did you? Make sure you enable warnings and understand them.

Finally, some nitpicking:

  • In the beginning, you read some input. In case you target a human, they will never know what these values are. Actually, even reading the code, it's unclear what a, b, and c are. Only from looking deeper I see that they are polynomial factors.
  • Input can fail. Using e.g. if (!(cin >> a >> b >> c)) return EXIT_FAILURE; would make your program more robust.
  • delta could be a constant. Making things constant makes it clear to the reader that the value never changes and also guarantees that you don't change it accidentally.
  • using namespace std will pollute your namespace. All you need is cin, cout, endl and sqrt.
  • Put an endline at the end of every line of output. For that, just use '\n'.
\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.