programming a Quadratic Equation Solver

Pages: 12
Hi, I'm having issues figuring out a few issues with my program. It seems to somewhat work correctly minus the few details i was given to enact.

One issue im having is, i was supposed to have the program terminate if a=0. I cant seem to have it terminate.

Also if the program had imaginary roots all i had to do was tell the user that there are only imaginary roots, i didn't have to evaluate those ones. But i cannot seem to make that portion work either.
Please help, i know its last minute but i have to have this finished for tomorrow. I've been lost for days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Objective: To find and check the real roots of a quadratic Equation.
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double input(double& a, double& b, double& c);
void checkRoots(double a, double b, double c, bool roots);
void findRootsAndPrint(double a, double b, double c);


int main ()
{
    double a,b,c,d;
    int roots;
    
    input(a,b,c);
    checkRoots(a,b,c,roots);
        
    
    return 0;
       
}

double input(double& a, double& b, double& c)
{
     cout << "Enter the numbers to be respresented in the equation below.\n";
     cout << "a=";
     cin  >> a;
     cout << "\nb=";
     cin  >> b;
     cout << "\nc=";
     cin  >> c;
     
     if (a==0)
        {
              cout << "I'm Sorry 'a' cannot be = to 0.\n";
              return 0;
        }
       else
          return 0;
}

void checkRoots(double a, double b, double c, bool roots)
{
      
           
       roots=(b*b)-(4*a*c);
       switch (roots)
       {
              case 0:
                   {
                         cout << "There is one real root for the equation."<< endl << endl;
                         findRootsAndPrint(a,b,c);
                         break;
                   }
              case 1:
                   {
                         cout << "There are two real roots for the equation."<< endl << endl;
                         findRootsAndPrint(a,b,c);
                         break;
                   }
              default:
                     {
                          cout << "The Quadratic Equation with the variables of a=" 
                            << a << ", b=" << b << ", c=" << c << " have no real roots, only imaginary."<< endl;
                              
                               system("pause");
                                   break;
                     }
       }
}

void findRootsAndPrint(double a, double b, double c)
{
     double first, second, d, det;
     
     d=(b*b)-(4*a*c);
     det=sqrt(d);
     if (d==0)
        {
              first=(-b/(2*a))+(det/(2*a));
              cout << "The Root of the equation " << a << "(x*x) - " << b << "x + " 
              << c << " is:  " << first << endl << endl;
              } 
        else 
             {
                 first=(-b/(2*a))+(det/(2*a));
                 second=(-b/(2*a))-(det/(2*a));
                 cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + " 
                  << c << " are...\n";
                  cout << "First Root: " << first << endl;
                   cout << "Second Root: " << second << endl << endl;
             }
     system("pause");
     
     return;
}
Last edited on
Ditch the return 0 in input() and say void. The return obviously means nothing.
You're performing the imaginary switch on a boolean. There are only two (normal) values for a boolean - 0 and 1, both of which are cases. It's also a problem that you are assigning a number to a boolean. You should be using an if/else and three possibilities: >0, <0 and 0. Not a boolean that's being stylishly misused (not stylishly actually, sarcasm).
What you should do is only perform checkroots() if a != 0. Surely you know how to limit that.
Hi tummychow thanks for the quick response. Heres the only issue. the professor is requiring that we use at least 1 if/else statement and at least 1 switch/case statement. She hinted to us that we should possibly use the switch on the number of roots. I also fixed the first problem with the program terminating by taking the if/else statement and bring it to the main and not allowing checkroots to perform if a=0. Now im just stuck on the part where im attempting to get the default of my switch/case statements to be for the imaginary roots route
Here is my updated code with the a=0 termination fix. Thats out of the way. But as my above post says im still stuck on my switch/case statements not getting to the default when its <0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Objective: To find and check the real roots of a quadratic Equation.
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double input(double& a, double& b, double& c);
void checkRoots(double a, double b, double c, bool roots);
void findRootsAndPrint(double a, double b, double c);


int main ()
{
    double a,b,c,d;
    int roots;
    
    input(a,b,c);
    if (a==0)
        {
              cout << "\nI'm Sorry 'a' cannot be = to 0.  This is not Quadratic.\n\n";
              system("pause");
              return 0;
        }
       else checkRoots(a,b,c,roots);
        
  
       
}

double input(double& a, double& b, double& c)
{
     cout << "Enter the numbers to be respresented in the equation below.\n";
     cout << "a=";
     cin  >> a;
     cout << "\nb=";
     cin  >> b;
     cout << "\nc=";
     cin  >> c;
     
     
          return 0;
}

void checkRoots(double a, double b, double c, bool roots)
{
      
           
       roots=(b*b)-(4*a*c);
       switch (roots)
       {
              case 0:
                   {
                         cout << "There is one real root for the equation."<< endl << endl;
                         findRootsAndPrint(a,b,c);
                         break;
                   }
              case 1:
                   {
                         cout << "There are two real roots for the equation."<< endl << endl;
                         findRootsAndPrint(a,b,c);
                         break;
                   }
              default:
                     {
                          cout << "The Quadratic Equation with the variables of a=" 
                            << a << ", b=" << b << ", c=" << c << " have no real roots, only imaginary."<< endl;
                              
                               system("pause");
                                   break;
                     }
       }
}

void findRootsAndPrint(double a, double b, double c)
{
     double first, second, d, det;
     
     d=(b*b)-(4*a*c);
     det=sqrt(d);
     if (d==0)
        {
              first=(-b/(2*a))+(det/(2*a));
              cout << "The Root of the equation " << a << "(x*x) - " << b << "x + " 
              << c << " is:  " << first << endl << endl;
              } 
        else 
             {
                 first=(-b/(2*a))+(det/(2*a));
                 second=(-b/(2*a))-(det/(2*a));
                 cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + " 
                  << c << " are...\n";
                  cout << "First Root: " << first << endl;
                   cout << "Second Root: " << second << endl << endl;
             }
     system("pause");
     
     return;
}
Last edited on
I don't see how you'd perform a switch on the discriminant. If the discriminant is zero, there is one root. (Yes, that's switch-able.) But if the discriminant is positive, there are two roots. Now you're going to go to hell trying to write a case for (if num > 0). And then you'd also have to code for negatives. Hell, and back, and then back again.
What I think your teacher meant is a switch on, literally, the number of roots. switch(number of roots), not switch(discriminant's value). You'd only have to switch three values: 0, 1 and 2. This is more than doable.
EDIT: There is no need to PM me with your response. It has nothing to do with me personally. What if I was asleep at this moment in time and you had PMd me, but not posted the response? Nobody would be able to add to the situation (unless, and this is quite possible, I missed something in your original code). My policy over my PM box is that you should only send me something if it particularly has to do with me, or something I've done or stated. (Generally not concerning help. Help is better off in threads.)
Last edited on
How do figure out the number of roots from the discriminant and apply it to the switch? (my apologies this is my first programming class and there is plenty haven't learned yet)
Hm. Use an if/else if/else to determine how many roots there are and return that from checkRoots, and ditch that silly boolean argument that serves no purpose. Store the returned value in main, and add an argument in your findRoots() for that number. Pass the root count into the findRoots function. Perform a switch on the number therein, as opposed to recalculating the discriminant as you have now in that function.
See how that goes.
That makes so much more sense. Im implementing it now.. hopefully youll be around when i repost in a few minutes. Thanks for you help.
Im getting a switch quanity is not an integer error during compiling process. any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Objective: To find and check the real roots of a quadratic Equation.
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void input(double& a, double& b, double& c);
double checkRoots(double a, double b, double c, double noOfRoots);
void findRootsAndPrint(double a, double b, double c, double noOfRoots);


int main ()
{
    double a,b,c,d;
    double noOfRoots;
    
    input(a,b,c);
    if (a==0)
        {
              cout << "\nI'm Sorry 'a' cannot be = to 0.  This is not Quadratic.\n\n";
              system("pause");
              return 0;
        }
       else checkRoots(a,b,c,noOfRoots);
        
  return 0;
       
}

void input(double& a, double& b, double& c)
{
     cout << "Enter the numbers to be respresented in the equation below.\n";
     cout << "a=";
     cin  >> a;
     cout << "\nb=";
     cin  >> b;
     cout << "\nc=";
     cin  >> c;
     
     
          
}

double checkRoots(double a, double b, double c, double noOfRoots)
{
      double d;
      
      d=(b*b)-(4*a*c);
      
      if (d=0)
       noOfRoots=1;
       
        else if (d>0)
             noOfRoots=2;
            
             else
               noOfRoots=0;
   
    return noOfRoots;
      
}

void findRootsAndPrint(double a, double b, double c, double noOfRoots)
{
     double first, second, d, det;
     
     
     d=(b*b)-(4*a*c);
     det=sqrt(d);
     switch (noOfRoots)
     {
            case 0:
                 {
                       cout << "There are no real roots for this equation.  Only Imaginary.";
                       break;
                       }
            case 1:
                 {
                       first=(-b/(2*a))+(det/(2*a));
                       cout << "The Root of the equation " << a << "(x*x) - " << b << "x + " 
                       << c << " is:  " << first << endl << endl;
                       break;
                 }
            case 2:     
                  {
                     first=(-b/(2*a))+(det/(2*a));
                     second=(-b/(2*a))-(det/(2*a));
                     cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + " 
                      << c << " are...\n";
                     cout << "First Root: " << first << endl;
                     cout << "Second Root: " << second << endl << endl;
                     break;
                   }
     }
system("pause");
     
     return;
}
Last edited on
You are switching on Noofroots, which is a double for some reason. It should be a integer.
And there's no point in recalculating the determinant/discriminant. You're passing in the number of roots, so what's the use?
Well.. you figured i would have realized that. ok fixed that .. so far so good. I'm using a few checkers to see if its working properly now
ok guys i know im probably making this string terribly long but it looks like i have one last thing to fix and im finished.. for some reason .. and its probably a silly mistake. noOfRoots doesnt seem to be passing through the program. no matter what values i give for a b or c it defaults to case 0: somewhere its not passing properly, is it obvious?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*Brian Notte
Objective: To find and check the real roots of a quadratic Equation.
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void input(double& a, double& b, double& c);
double checkRoots(double a, double b, double c, int noOfRoots);
void findRootsAndPrint(double a, double b, double c, int noOfRoots);


int main ()
{
    double a,b,c,d;
    int noOfRoots;
    
    input(a,b,c);
    if (a==0)
        {
              cout << "\nI'm Sorry 'a' cannot be = to 0.  This is not Quadratic.\n\n";
              system("pause");
              return 0;
        }
       else checkRoots(a,b,c,noOfRoots);
    findRootsAndPrint(a,b,c,noOfRoots);
        
  return 0;
       
}

void input(double& a, double& b, double& c)
{
     cout << "Enter the numbers to be respresented in the equation below.\n";
     cout << "a=";
     cin  >> a;
     cout << "\nb=";
     cin  >> b;
     cout << "\nc=";
     cin  >> c;
     
     
     return ;     
}

double checkRoots(double a, double b, double c, int noOfRoots)
{
      double d;
      
      d=(b*b)-(4*a*c);
      
      if (d==0)
       noOfRoots=1;
       
        else if (d>0)
             noOfRoots=2;
            
             else
               noOfRoots=0;
   
    return noOfRoots;
      
}

void findRootsAndPrint(double a, double b, double c, int noOfRoots)
{
     double first, second, d, det;
     
     
     d=(b*b)-(4*a*c);
     det=sqrt(d);
     switch (noOfRoots)
     {
            case 0:
                 {
                       cout << "There are no real roots for this equation.  Only Imaginary.";
                       break;
                       }
            case 1:
                 {
                       first=(-b/(2*a))+(det/(2*a));
                       cout << "The Root of the equation " << a << "(x*x) - " << b << "x + " 
                       << c << " is:  " << first << endl << endl;
                       break;
                 }
            case 2:     
                  {
                     first=(-b/(2*a))+(det/(2*a));
                     second=(-b/(2*a))-(det/(2*a));
                     cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + " 
                      << c << " are...\n";
                     cout << "First Root: " << first << endl;
                     cout << "Second Root: " << second << endl << endl;
                     break;
                   }
                   }
     system("pause");
     
     return;
}
That's incorrect. You should be assigning the return from checkroots to the value noofroots. You are passing in noofroots, but that means nothing because you aren't passing by reference - and, in my opinion, passing by reference with only one value is generally less elegant than a return.
1
2
3
noofroots = checkroots(a,b,c); // NO NOOFROOTS ARG
findroots(a,b,c,noofroots); // do not perform any modifications to noofroots whatsoever
// in this function 

Ok the program works now.. but i am not sure that the answers its giving are correct.. does anyone have a test for 1 root 2 roots and no roots?
Try (x+3)^2 for one root (at 0,-3), (x^2)-4 for two roots (at 0, + or - 3) and (x^2)+5 (no roots).
If you have a graphing calculator (or use some online program, there has to be one) you could scribble down your own quadratics and figure out the roots pretty easy... but my graphing unit has not yet abandoned me so there are some tests.
Last edited on
I know this is going to make me sound like an idiot, but ive been out of school for 10 years, ... how would i extract the a, b, and c values from those expressions you gave me?
Damn. You want full expansion?
OK, well that should still be pretty easy. There was only one that wasn't in standard form anyhoo.
first: x^2 + 6x + 9
second: x^2 - 4
third: x^2 + 5
(all in my head. so difficult, I know. /joke)
A is the quadratic coefficient (of the x^2 term; in all cases, 1), b is the linear coefficient (of the x term, zero for 2 and 3, you can figure it out for #1) and c is the constant term, the one not being multiplied by anything.
How the hell are you supposed to program a solution machine for a quadratic equation when you don't remember the coefficients? <ranting>
Last edited on
Ok .. you had me laughing, I think i got the answers correct.. hopefully i don't show my age by these posts. Everyone thank you for your help, especially you tummychow. I will be certain to check back as i learn more to see if i can help someone in return. I believe the program is finally working properly. At least i hope. I am going to post one lats copy of it for review if anyone feels so inclined :) Will be turning project in in the morning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Objective: To find and check the real roots of a quadratic Equation.
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void input(double& a, double& b, double& c);
double checkRoots(double a, double b, double c);
void findRootsAndPrint(double a, double b, double c, int noOfRoots);


int main ()
{
    double a,b,c,d;
    int noOfRoots;
    
    
    input(a,b,c);
    if (a==0)
        {
              cout << "\nI'm Sorry 'a' cannot be = to 0.  This is not Quadratic.\n\n";
              system("pause");
              return 0;
        }
       else noOfRoots=checkRoots(a,b,c);
    
    findRootsAndPrint(a,b,c,noOfRoots);
        
  return 0;
       
}

void input(double& a, double& b, double& c)
{
     cout << "Enter the numbers to be respresented in the equation below.\n";
     cout << "a=";
     cin  >> a;
     cout << "\nb=";
     cin  >> b;
     cout << "\nc=";
     cin  >> c;
     system("cls");
     
     return ;     
}

double checkRoots(double a, double b, double c)
{
      double d;
      int x;
      
      d=(b*b)-(4*a*c);
      
      if (d==0)
       x=1;
       
        else if (d>0)
             x=2;
            
             else
               x=0;
   
    return x;
      
}

void findRootsAndPrint(double a, double b, double c, int noOfRoots)
{
     double first, second, d, det;
     
     
     d=(b*b)-(4*a*c);
     det=sqrt(d);
     switch (noOfRoots)
     {
            case 0:
                 {
                       cout << "There are no real roots for this equation.  Only Imaginary.\n";
                       break;
                       }
            case 1:
                 {
                       first=(-b/(2*a))+(det/(2*a));
                       cout << "The Root of the equation " << a << "(x*x) - " << b << "x + " 
                       << c << " is:  " << first << endl << endl;
                       break;
                 }
            case 2:     
                  {
                     first=(-b/(2*a))+(det/(2*a));
                     second=(-b/(2*a))-(det/(2*a));
                     cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + " 
                      << c << " are...\n";
                     cout << "First Root: " << first << endl;
                     cout << "Second Root: " << second << endl << endl;
                     break;
                   }
     }
     system("pause");
     
     return;
}
Pages: 12