Samuel Svensson - 1 month ago 4
C++ Question

# Can't bubblesort these values

I'll try to keep this short and simple. Here's my assignment (too big to copy here):

Exercise picture 1
Exercise picture 2

So long story short, I've created a new data style that will hold 3 values (a,b and c) and these have to be put into an equation and then it will calc the roots and display it.
I'm not nearly finished yet but i've got some problems with sorting the values (which we have to do)

I figured that I have to sort my array that holds the three values a,b,c and put the eq with the lowest value of a first. For that I tried to use bubblesort but it's giving me weird answers.

If anyone could please take a look and help me see what's wrong and why it's giving me weird answers. I'm not sure what's wrong but I feel it has to do something with the bubblesort parameters?

Here's my entire code:

``````    struct Quadratic_equation
{
int a;
int b;
int c;
};
//declaration of functions

int main()
{
//DECLARE THE NEEDED CONSTANTS and VARIABLES HERE
const int MAX = 100;

int howMany = 0;
//1. INPUT
cout << "** Enter Quadratic Equations **" << endl;

//2. SORT

sort_equation_array(V, howMany);

//3. Display
print(V, howMany);

}

{
cin >> e.a;
cin >> e.b;
cin >> e.c;

}

{

int counter = 0;

while (e.a != 0)
{
V[counter] = e;
++counter;

if (counter == n) break;

}

return counter;
}

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

{

double x1; //roots
double x2;
int a = e.a;
int b = e.b;
int c = e.c;
double d = ( ( b * b ) - ( 4 * a * c ) );

cout << a << "x^2 + " << b << "x + " << c << "     ";

if (d == 0)
{
x1 = ( ( - b ) / ( 2 * a ) );
cout << setw(10) << "root 1 = " << fixed << setprecision(2) << x1 << endl;
}
else if (d > 0)
{
x1 = ( ( - b + sqrt ( d * 1.0) ) / ( 2 * a ) );
cout << "root 1 = " << fixed << setprecision(2) << x1 << "       ";
x2 = ( ( - b - sqrt ( d * 1.0 ) ) / ( 2 * a ) );
cout << "root 2 = " << fixed << setprecision(2) << x2 << endl;
}
else
{
x1 = ( ( - b ) / ( 2 * a ) );
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;

}

}
{
for ( int pass = 1; pass < howMany; pass++ )
{

for (int i = 0; i < howMany -1; i++)
{

if (V[i].a > V[ i + 1 ].a)
{
temp = V[i];
V[i] = V[i + 1];
V[i + i] = temp;
}
else if (V[i].a == V[i+1].a &&
V[i].b > V[i + 1].b)
{
temp = V[i];
V[i] = V[i + 1];
V[i + i] = temp;
}
else if (V[i].a == V[i + 1].a &&
V[i].b == V[i+1].b &&
V[i].c > V[i+1].c)
{
temp = V[i];
V[i] = V[i + 1];
V[i + i] = temp;
}

}
}
}
``````

As per my comment:

I believe that if you initialize pass to 0, initialize i to (pass + 1), replace all V[i] with V[pass], and all V[i+1] with V[i], your code should work as expected

``````void swap(Quadratic_equation arr[], int lowerIndex, int higherIndex) {
arr[lowerIndex] = arr[higherIndex];
arr[higherIndex] = temp;
}
{
for ( int pass = 0; pass < howMany; pass++ )
{

for (int i = (pass + 1); i <= howMany -1; i++)
{
if (V[pass].a > V[i].a)
{
swap(V, pass, i);
}
else if (V[pass].a == V[i].a &&
V[pass].b > V[i].b)
{
swap(V, pass, i);
}
else if (V[pass].a == V[i].a &&
V[pass].b == V[i].b &&
V[pass].c > V[i].c)
{
swap(V, pass, i);
}
}
}
}
``````

You should also clean up your input to make it easier to tell which values are going where. Here's what I did, you can change it to say whatever you'd like, but you get the idea.

``````void get_quadratic_equation(Quadratic_equation &e)
{
cout << "a: ";
cin >> e.a;
cout << "b: ";
cin >> e.b;
cout << "c: ";
cin >> e.c;
}

{

int counter = 0;
cout << endl << "Set values for equation " << counter << endl;

while (e.a != 0)
{
V[counter] = e;
++counter;

if (counter == n) break;
cout << endl << "Set values for equation " << counter << endl;
}
cout << endl;
return counter;
}
``````

# OUTPUT

```** Enter Quadratic Equations **

Set values for equation 0
a: 1
b: 2
c: 3

Set values for equation 1
a: 4
b: 5
c: 6

Set values for equation 2
a: 7
b: 8
c: 9

Set values for equation 3
a: 8
b: 7
c: 6

Set values for equation 4
a: 5
b: 4
c: 3

Set values for equation 5
a: 2
b: 1
c: 0

Set values for equation 6
a: 0
b: 0
c: 0

** NOT SORTED **
1x^2 + 2x + 3     root 1 = -1.00+i 1.41       root 2 = -1.00-i 1.41
4x^2 + 5x + 6     root 1 = 0.00+i 1.05       root 2 = 0.00-i 1.05
7x^2 + 8x + 9     root 1 = 0.00+i 0.98       root 2 = 0.00-i 0.98
8x^2 + 7x + 6     root 1 = 0.00+i 0.75       root 2 = 0.00-i 0.75
5x^2 + 4x + 3     root 1 = 0.00+i 0.66       root 2 = 0.00-i 0.66
2x^2 + 1x + 0     root 1 = 0.00       root 2 = -0.50

** SORTED BY A **
1x^2 + 2x + 3     root 1 = -1.00+i 1.41       root 2 = -1.00-i 1.41
2x^2 + 1x + 0     root 1 = 0.00       root 2 = -0.50
4x^2 + 5x + 6     root 1 = 0.00+i 1.05       root 2 = 0.00-i 1.05
5x^2 + 4x + 3     root 1 = 0.00+i 0.66       root 2 = 0.00-i 0.66
7x^2 + 8x + 9     root 1 = 0.00+i 0.98       root 2 = 0.00-i 0.98
8x^2 + 7x + 6     root 1 = 0.00+i 0.75       root 2 = 0.00-i 0.75```
Source (Stackoverflow)