Quadratic equation with functions (and arrays, maybe)

Suppose we have a quadratic equation, ax^2 + bx + c = 0 (a, b and c are integers). The discriminant for the said equation is D = b*b - 4ac,
- If D = 0 --> one root displayed
- If D > 0 --> two roots displayed
- If D < 0 --> two complex roots displayed
This I have gotten so far, however now comes the tricky part (for me at least). The input consists of me writing three integers, "a b c" which is supposed to correspond to "ax^2 + bx + c = 0". The input is ended when the user inputs a as a zero, "0 0 0" or "0 8 4" for example. The program should cope with up to 100 different equations. Also if I enter 5 9 3 twice, the equation 5x^2 + 9x + 3 will only be displayed once.

The problem is that I don't know if I have to use arrays to store the equations or not, I mean I figure that I need up to 100 arrays with one array storing 3 integers right? For example
2 8 2
2 4 -4
8 3 19
and so on... that's up the user. And I get three different arrays with three integers, and index[1] can be a and so on...

Also I figured that need a data type "Quadratic_Equation" or something like that to actually stand for the quadratic equation, then a function "read_equation" or something like that which will 3 integers from cin which will create the second degree function and at last a last function which will show the user the equation and the equation's roots.

In the end, if the user inputs something like
-3 -5 -2 -
-4 12 -9 -
1 2 4 -
1 4 -4 -
10 -1 10 -
0 1 4

the output will be something like this

Equations with one root
-4x^2 + 12x -9 root 1.50

Equations with two roots
-3x^2 - 5x -2 root1 = -1.00 root2 = -0.67
1x^2 + 4x -4 root1 = 0.83 root2 = -4.83

Equations with complex roots
1x^2 + 2x + 4 root1 = -1.00+i1.73 root2 = -1.00-i.73
10x^2 - 1x + 10 root1 = 0.05+i1.00 root2 = 0.05-i1.00
> I don't know if I have to use arrays to store the equations or not,
that's the only question that I found in your wall of text.
Some things to consider:
- you need to discard repeated elements
- you need to classify them, but can't do it online
So, you need to use some kind of container. There is no obligation to use an array, there are other containers.
At least you could try it.

> And I get three different arrays with three integers, and index[1] can be a
> and so on
array indices goes from 0 to n-1.
`index' is quite a bad name for the coefficients.
you may use an `equation' struct instead of an array.
Thanks for the answer, I am kind of an amateur regarding programming so I have practically no idea what other containers there are.

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
#include <iostream>
#include <cmath>

using namespace std;

void get_quadratic_equation (int a, int b, int c)
{
    cout << "Enter Quadratic Equations" << endl;
    for (int i = 0; i < 100; i++)
    {
    cin  >> a;
    cin  >> b;
    cin  >> c;
    cout << endl;
    if (a == 0)
    {
        break;
    }
    }
    
}

int determinant(int a, int b, int c)
{
    
    return (b * b) - (4 * a * c);
}


How do I pass "data" from my cin >> in the void to the determinant to be calculated?
Just call the determinant function inside the get_quadratic_equation function
This is what I have come to so far. I am able to cin three integers into an array until a zero is met. However now a new problem rises, how to calculate the discriminant and the roots from the array. Could I maybe use modulus to let the program know which index is a, b and c?

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include <iostream>
#include <string>

using namespace std;

/****************************************
 * 1. Declaration of Quadratic-equation data type  *
 *****************************************/

struct Quadratic_Equation
{
    string name;
    int   a, b, c, discriminant;
    
};

/****************************************
 * 2. Function declarations *
 *****************************************/


int load_database(Quadratic_Equation V[], int n);

//void implementcalculatingfunction(Quadratic_Equation V[], int n, int k);


void display_database(const Quadratic_Equation V[], int n);


/**************************
 * 3. MAIN                 *
 ***************************/

int main()
{
    const int SIZE = 100;
    cout << "** Enter Quadratic Equations **" << endl;

    Quadratic_Equation DB[SIZE];
    int howMany = 0;
    
    howMany = load_database(DB, SIZE);
/*
    int amount = 0;
 
    
    implementcalculatingfunction(DB, howMany, amount);
*/
    
    display_database(DB, howMany);
    
    return 0;
}


/*************************************
 * 4. Function definitions            *
 **************************************/


void get(Quadratic_Equation &e)
{
    
    while (cin >> e.a >> e.b >> e.c)
    {
        if (e.a == 0)
        {
            break;
        }
    }
    
}

void put(const Quadratic_Equation& e)
{
    cout << e.a << e.b << e.c << endl;
}


int load_database(Quadratic_Equation V[], int n)
{
    Quadratic_Equation e;
    int counter = 0;
    
    get(e);
    
    while (e.a != 0)
    {
        V[counter] = e;
        ++counter;
        
        get(e);
    }
    
    return counter;
}


void display_database(const Quadratic_Equation V[], int n)
{
    cout << "\n** Calculating **" << endl;
    
    for(int i = 0; i < n; i++)
    {
        put(V[i]);
        
        cout << endl;
    }
}


/*
void implementcalculatingfunction(Quadratic_Equation V[], int n, int k)
{
    Quadratic_Equation e;
    for(int i = 0; i < n; ++i)
    {
        
    }
    
}
*/
I was thinking to define a new data type to represent the equation however I am not so used to the "struct" function...
that's awful. Forget about the array, try to solve it for just one equation.
The user would only input 3 numbers, that are the a,b,c coefficients, output the roots.

If you do that, I'll show you how to adapt it to handle several equations.
I fixed it somehow however now I can't get the equations to sort themselves in the three categories, "one root", "two roots", "complex roots".
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

/*****************************
 * Data types declarations    *
 ******************************/

struct Quadratic_equation
{
    int a;
    int b;
    int c;
};

/*****************************
 * Function declarations *
 ******************************/

void get_quadratic_equation(Quadratic_equation &e);

int load_equation(Quadratic_equation V[], int n);

void display_equation(Quadratic_equation &e);

void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex);

void sort_equation_array(Quadratic_equation V[], int howMany);

void print(Quadratic_equation V[], int howMany);

/******************************
 * INT MAIN *
 ******************************/
int main()
{
    const int SIZE = 100;
    Quadratic_equation A[SIZE] = {0};
    
    int howMany = 0;
    cout << "** Enter Quadratic Equations **" << endl;
    howMany = load_equation(A, SIZE);
    
    
    sort_equation_array(A, howMany);
    
    
    cout << "** Quadratic equations with one root **" << endl;
    
    
    cout << "** Quadratic equations with two roots **" << endl;
    
    
    cout << "** Quadratic equations with complex roots **" << endl;
    
    
    // Display
    print(A, howMany);
}

/*************************************
 * Function definitions *
 **************************************/

void get_quadratic_equation(Quadratic_equation &e)
{
    cin >> e.a; //Here we input the three integers to represent the quadratic equation
    cin >> e.b;
    cin >> e.c;
    
}

int load_equation(Quadratic_equation V[], int n)
{
    Quadratic_equation e;
    
    int counter = 0;
    
    get_quadratic_equation(e);
    
    while (e.a != 0) // when a is 0, the array won't continue
    {
        V[counter] = e;
        ++counter;
        
        if (counter == n) break;
        get_quadratic_equation(e);
    }
    
    return counter;
}


void display_equation(Quadratic_equation &e)
{
    float x1; //the actual roots, x1 and x2
    float x2;
    int a = e.a;
    int b = e.b;
    int c = e.c;
    int d = (( b * b ) - ( 4 * a * c )); // the discriminant
    
    
    cout << a << "x^2 ";
    if (b < 0)
    {
        cout << b << "x";
    }
    else if (b > 0)
    {
        cout << "+" << b << "x";
    }
    if (c < 0)
    {
        cout << " " << c << "      ";
    }
    else if (c > 0)
    {
        cout << " +" << c << "     ";
    }
    
    if (d == 0)
        {
            x1 = ( ( - b ) / ( 2 * a ) );
            cout << " " << "root 1 = " << fixed << setprecision(2) << x1 << endl;
        }
    else if (d > 0)
        {
            
            x1 = ( ( - b + sqrt ( d * 1.0) ) / ( 2 * a ) ); //* 1.0 because else the answer would be an int
            cout << "root 1 = " << fixed << setprecision(2) << x1 << "                ";
            x2 = ( ( - b - sqrt ( d * 1.0 ) ) / ( 2 * a ) );//* 1.0 because else the answer would be an int
            cout << "root 2 = " << fixed << setprecision(2) << x2 << endl;
        }
    else
        {
            x1 = ( ( - b ) / ( 2 * a * 1.0) ); //* 1.0 because else the answer would be an int
            x2 = ( sqrt(-d * 1.0) / ( 2 * a ));
        
            cout << " root 1 = " << fixed << setprecision(2) << x1
            << "+i" << x2 << "       ";
            cout << " root 2 = " << fixed << setprecision(2) << x1
            << "-i" << x2 << endl;
        
        }
}

void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex)
{
    Quadratic_equation foo = V[lowerIndex];
    V[lowerIndex] = V[higherIndex];
    V[higherIndex] = foo;
}

void sort_equation_array(Quadratic_equation V[], int howMany)
{
    for ( int sort = 0; sort < howMany; sort++ )
    {
        for (int i = (sort + 1); i <= howMany -1; i++)
        {
            if (V[sort].a > V[i].a)
            {
                _swap(V, sort, i);
            }
            else if (V[sort].a == V[i].a &&
                     V[sort].b > V[i].b)
            {
                _swap(V, sort, i);
            }
            else if (V[sort].a == V[i].a &&
                     V[sort].b == V[i].b &&
                     V[sort].c > V[i].c)
            {
                _swap(V, sort, i);
            }
        }
    }
}

void print(Quadratic_equation V[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        display_equation(V[i]);
    }
}
One alternative to an array is a vector. Unlike an array, a vector does not have a fixed length. Every time you add (push_back) something into the array it grows a bit longer.
http://www.cplusplus.com/reference/vector/vector/push_back/
http://www.cplusplus.com/reference/vector/vector/operator[]/

If you use vectors, you don't have to think about the user that might list 101 equations.

Also, you could use multiple vectors, for example one to store all equations with D=0, one with all equations with D > 0 and one with all equations with D < 0.

This would of course influence the flow of your program a bit. You would have to change it to:
1
2
3
4
5
6
7
8
9
10
do
{
      get the values for an equation
      determine D
      store the equation in the appropriate vector
}
while (the user is not done inputting equations yet)
output all equations in the first vector using for-loop and vector.size
output all equations in the second vector using for-loop and vector.size
output all equations in the third vector using for-loop and vector.size
Last edited on
I have fixed it now, it's a bit longer and "uglier" so to speak however it works now ;). The only thing that I need to fix now is the fact that I have to skip repeated equations which I frankly have no idea how to implement.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

/*****************************
 * Data types declarations    *
 ******************************/

struct Quadratic_equation
{
    int a;
    int b;
    int c;
};

/*****************************
 * Function declarations *
 ******************************/

void get_quadratic_equation(Quadratic_equation &e);

int load_equation(Quadratic_equation V[], int n);

void display_equation(Quadratic_equation &e);

void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex);

void sort_equation_array(Quadratic_equation V[], int howMany);

void print(Quadratic_equation V[], int howMany);

void print1(Quadratic_equation V[], int howMany);

void print2(Quadratic_equation V[], int howMany);



/******************************
 * INT MAIN *
 ******************************/
int main()
{
    const int SIZE = 100;
    Quadratic_equation A[SIZE] = {0};
    
    int howMany = 0;
    cout << "** Enter Quadratic Equations **" << endl;
    howMany = load_equation(A, SIZE);
    
    
    sort_equation_array(A, howMany);
    
    
    cout << "** Quadratic equations with one root **" << endl;
    // Display
    print(A, howMany);
    
    cout << "** Quadratic equations with two roots **" << endl;
    print1(A, howMany);
    
    cout << "** Quadratic equations with complex roots **" << endl;
    print2(A, howMany);
    
    
}

/*************************************
 * Function definitions *
 **************************************/

void get_quadratic_equation(Quadratic_equation &e)
{
    cin >> e.a; //Here we input the three integers to represent the quadratic equation
    cin >> e.b;
    cin >> e.c;
    
}

int load_equation(Quadratic_equation V[], int n)
{
    Quadratic_equation e;
    
    int counter = 0;
    
    get_quadratic_equation(e);
    
    while (e.a != 0) // when a is 0, the array won't continue
    {
        V[counter] = e;
        ++counter;
        
        if (counter == n) break;
        get_quadratic_equation(e);
    }
    
    return counter;
}


void display_equation(Quadratic_equation &e)
{
    float x1; //the actual roots, x1 and x2
    int a = e.a;
    int b = e.b;
    int c = e.c;
    int d = (( b * b ) - ( 4 * a * c )); // the discriminant
    
    if (d == 0)
    {
        cout << a << "x^2 ";
        if (b < 0)
        {
            cout << b << "x";
        }
        else if (b > 0)
        {
            cout << "+" << b << "x";
        }
        if (c < 0)
        {
            cout << " " << c << "      ";
        }
        else if (c > 0)
        {
            cout << " +" << c << "     ";
        }
        x1 = ( ( - b ) / ( 2 * a ) );
        cout << " " << "root 1 = " << fixed << setprecision(2) << x1 << endl;
    }
    
}

void display_equation1(Quadratic_equation &e)
{
    float x1; //the actual roots, x1 and x2
    float x2;
    int a = e.a;
    int b = e.b;
    int c = e.c;
    int d = (( b * b ) - ( 4 * a * c )); // the discriminant
    
        if (d > 0)
        {
        cout << a << "x^2 ";
        if (b < 0)
        {
            cout << b << "x";
        }
        else if (b > 0)
        {
            cout << "+" << b << "x";
        }
        if (c < 0)
        {
            cout << " " << c << "      ";
        }
        else if (c > 0)
        {
            cout << " +" << c << "     ";
        }
        x1 = ( ( - b + sqrt ( d * 1.0) ) / ( 2 * a ) ); //* 1.0 because else the answer would be an int
        cout << " root 1 = " << fixed << setprecision(2) << x1 << "               ";
        x2 = ( ( - b - sqrt ( d * 1.0 ) ) / ( 2 * a ) );//* 1.0 because else the answer would be an int
        cout << "root 2 = " << fixed << setprecision(2) << x2 << endl;
        }

}

void display_equation2(Quadratic_equation &e)
{
    float x1; //the actual roots, x1 and x2
    float x2;
    int a = e.a;
    int b = e.b;
    int c = e.c;
    int d = (( b * b ) - ( 4 * a * c )); // the discriminant
            
    if (d < 0)
    {
        cout << a << "x^2 ";
        if (b < 0)
        {
            cout << b << "x";
        }
        else if (b > 0)
        {
            cout << "+" << b << "x";
        }
        if (c < 0)
        {
            cout << " " << c << "      ";
        }
        else if (c > 0)
        {
            cout << " +" << c << "     ";
        }
    x1 = ( ( - b ) / ( 2 * a * 1.0) ); //* 1.0 because else the answer would be an int
    x2 = ( sqrt(-d * 1.0) / ( 2 * a ));
            
    cout << " root 1 = " << fixed << setprecision(2) << x1
    << "+i" << x2 << "       ";
    cout << " root 2 = " << fixed << setprecision(2) << x1
    << "-i" << x2 << endl;
    }
        
}

void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex)
{
    Quadratic_equation foo = V[lowerIndex];
    V[lowerIndex] = V[higherIndex];
    V[higherIndex] = foo;
}

void sort_equation_array(Quadratic_equation V[], int howMany)
{
    for ( int sort = 0; sort < howMany; sort++ )
    {
        for (int i = (sort + 1); i <= howMany -1; i++)
        {
            if (V[sort].a > V[i].a)
            {
                _swap(V, sort, i);
            }
            else if (V[sort].a == V[i].a &&
                     V[sort].b > V[i].b)
            {
                _swap(V, sort, i);
            }
            else if (V[sort].a == V[i].a &&
                     V[sort].b == V[i].b &&
                     V[sort].c > V[i].c)
            {
                _swap(V, sort, i);
            }
        }
    }
}

void print(Quadratic_equation V[], int howMany)
{
    
    for (int i = 0; i < howMany; i++)
    {
        display_equation(V[i]);
    }
}

void print1(Quadratic_equation V[], int howMany)
{
    
    for (int i = 0; i < howMany; i++)
    {
        display_equation1(V[i]);
    }
}

void print2(Quadratic_equation V[], int howMany)
{
    
    for (int i = 0; i < howMany; i++)
    {
        display_equation2(V[i]);
    }
}

Topic archived. No new replies allowed.