Can anyone help me with this? C Program Quadratic Equation.?

Every time I put in any calculations besides the ones I have define, I get gibberish. Also, I can't get my "Do More (Y/N)?" to come up.

#include <stdio.h>

#include <stdlib.h>

#include <cmath>

int main ()

{

double a, b, c, d, x1, x2;

char more = 'y';

do {

printf ("\n\n\t\tSolve quadratic equation for giving A, B, C coefficient");

printf ("\n\n\t\t\tPlease input A:");

scanf ("%lf", &a);

printf ("\n\t\t\t\t B:");

scanf ("%lf", &b);

printf ("\n\t\t\t\t C:");

scanf ("%lf", &c);

if (a==0&&b==0&&c==0){

printf ("\n\t\t\t\tInfinite Solutions");

break;

}

else if (a==2&&b==4&&c==2){

printf ("\n\t\t\t\tRepeated root, X=1");

break;

}

else if (a==2&&b==2&&c==2){

printf ("\n\t\tTwo Complex Roots, X1 = -0.5 + 0.866i, X2 = -0.5 - 0.866i");

break;

}

else if (a==0&&b==0&&c==2){

printf ("\n\t\t\t\tContradict Equation.");

break;

}

else if (a==0&&b==2&&c==2){

printf ("\n\t\t\t\tSingle Root, x = -1");

break;

}

else if (a==2&&b==4&&c==-6){

printf ("\n\t\t\t\tTwo Real Roots, x1 = 1, x2 = -3");

break;

}

else if (d > 0.0){

printf ("\n\t\t\t\t Two Real Roots, x1 = %12.3e x2 = %12.3e", x1, x2);

break;

}

d = sqrt((b*b)-4.*a*c);

x1 =(-b + d) / (2.*a);

x2 =(-b - d) / (2.*a);

printf ("\n\t\t\tDo more (Y/N)?");

scanf ("%s", &more);

}

while (more == 'y' || more == 'Y');

system("pause");

return 0;

}

3 Answers

Relevance
  • Jared
    Lv 7
    9 years ago
    Favorite Answer

    yeah, you are breaking out of the loop when you call break;

    Why are those break's in there, I'm pretty sure you don't want them.

    Furthermore, I'd be VERY surprised if you ever get to do this again (i.e. the loop will never run more than once).

    you shouldn't be using %s, you should be using %c to read in a single character...but, since your reading in a single character, it probably makes more sense to just use getc() (or whatever it is, I think you have to give it stdin).

  • henery
    Lv 4
    4 years ago

    it incredibly 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 issues that makes it screwy is that regularly you will attempt thrice to get a maximum appropriate fee of a earlier moving directly to get b. yet with a mixed blunders minimize of four you may no longer try this, you will desire to do it the way that i've got shown. additionally, you in all risk want some logic so as that once you get a maximum appropriate a and/or b, you bypass over soliciting for it returned. i'd use 2 bool variables like validA and validB, initialize them to faux earlier the loop. Oh heck, i will purely tutor you: bool valA = fake; bool valB = fake; do { if( !valA ) { ...getA ...if( a is valid ) valA = authentic; ...else errA++; } // do the comparable for b // something of the loop is the comparable. } whilst( ...comparable as shown earlier );

  • ?
    Lv 7
    9 years ago

    You are not even solving the equation!

    Review your maths first.

    And your "logic" is incorrect

    - Input the three coefficients.

    - Calculate d

    - Calculate the roots

    - Display results.

Still have questions? Get your answers by asking now.