Bubble sort Print result help!

I got this far with the code below. I am looking to print the result a certain way for testing (see below picture result desired). I have absolutely no clue what I am doing and don't know how I got this far. Please help!

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
constexpr int MAXSIZE{ 10 };

void Display(int a[], int Pass)
{
    cout << "pass " << Pass << ":";

    for(int i = 0; i < MAXSIZE; i++)
    {
        cout << a[i] << " ";
    }
    std::cout << "   Press Enter: ";
    std::cin.ignore();
}

int main()
{
    //srand (time(0));
    srand(static_cast<size_t>(time(nullptr)));

    int a[10];

    for(int i = 0; i < 10; i++)
    {
        a[i] = rand() % 100 + 1;
    }
    for (int i = 0; i < MAXSIZE - 1; i++)
    {
        for (int j = 0; j < 10 - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                int t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }

         }
    Display(a, i);
    }
return 0;
}



Unsorted array
------------------------------
8 39 19 74 82 80 95 94 48 29

pass 1: 8 39 19 74 82 80 95 94 48 29 Press Enter:
pass 2: 8 19 39 74 82 80 95 94 48 29 Press Enter:
pass 3: 8 19 29 74 82 80 95 94 48 39 Press Enter:
pass 4: 8 19 29 39 82 80 95 94 74 48 Press Enter:
pass 5: 8 19 29 39 48 82 95 94 80 74 Press Enter:
pass 6: 8 19 29 39 48 74 95 94 82 80 Press Enter:
pass 7: 8 19 29 39 48 74 80 95 94 82 Press Enter:
pass 8: 8 19 29 39 48 74 80 82 95 94 Press Enter:
pass 9: 8 19 29 39 48 74 80 82 94 95 Press Enter:

Sorted array
------------------------------
8 19 29 39 48 74 80 82 94 95
So exactly what is wrong with the program?

It looks like you forgot to print the first three and last three lines.

Currently the program is working find, but output only shows each pass. What code should be added to not only show each pass but to also include Unsorted array and sorted array, just like displayed below, except my code does not do that.

How do I print the first three and last three lines?

Unsorted array
------------------------------
8 39 19 74 82 80 95 94 48 29

pass 1: 8 39 19 74 82 80 95 94 48 29 Press Enter:
pass 2: 8 19 39 74 82 80 95 94 48 29 Press Enter:
pass 3: 8 19 29 74 82 80 95 94 48 39 Press Enter:
pass 4: 8 19 29 39 82 80 95 94 74 48 Press Enter:
pass 5: 8 19 29 39 48 82 95 94 80 74 Press Enter:
pass 6: 8 19 29 39 48 74 95 94 82 80 Press Enter:
pass 7: 8 19 29 39 48 74 80 95 94 82 Press Enter:
pass 8: 8 19 29 39 48 74 80 82 95 94 Press Enter:
pass 9: 8 19 29 39 48 74 80 82 94 95 Press Enter:

Sorted array
------------------------------
8 19 29 39 48 74 80 82 94 95
@studentlearningcplusplus

Here is a program that shows an unsorted array, then, using colors, shows which numbers in the array were swapped. Hope this helps you in getting your program running as you want it to.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setw;

#define on , // So I can use the function - void text(text_color on background_color)

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

// My text color function. Use it if you wish.
void text(int text_color = 0 on int paper_color = 7)
{
	// defaults to black on light_gray
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

void sortArrayAscending(int *array, int size);
void printArray(int *array, int *old_array, int);
void printUnsortedArray(int *array, int size);

int main()
{
	text(light_green on black);

	const string progTitle = "Array Sorting Program";
	
	string hyphen(100, '-');

	const int size = 10;

	int values[size] = { 39,19,74,82,80,95,94,48,29,8 };
	//int values[size] = { 45, 76,2,67,83,12,9,34};
	cout << hyphen << endl;
	cout << "                                         " << progTitle << endl;
	cout << hyphen << endl;

	cout << "\n              This program will sort two identical arrays of numbers using a Bubble Sort"<< endl;


	cout << "\n                                       Array 1 -- Ascending Order:   \n" << endl;

	printUnsortedArray(values, size);

	cout  << endl;
	sortArrayAscending(values, size);
	text(light_green on black);
	cout << "\n\n\n\n\t\t\tPress only the 'Enter' key to exit program: ";
	cin.ignore(cin.rdbuf()->in_avail());
	cin.get();
}

void sortArrayAscending(int *array, int size)
{
	const int Non_SwappedColor = 2;
	const int SwappedColor = 4;

	int old_array[8];
	int temp;
	bool swapTookPlace;
	int pass = 0;

	do
	{
		swapTookPlace = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				for(int a=0;a<size;a++)
					old_array[a] = array[a];
				swapTookPlace = true;
				
				text(Non_SwappedColor on black);;
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
								
				cout << fixed << setw(2) << " Pass # " << (pass < 9 ? "  " : " ") << (pass + 1) << " : ";
				pass += 1;
				printArray(&array[0],old_array, size);
			}
		}
	} while (swapTookPlace);
	text(SwappedColor on black);
}

void printArray(int *array, int *old_array, int size)
{
	const int Non_SwappedColor = 10;
	const int SwappedColor = 12;
	for (int count = 0; count < size; ++count)
	{
		if(array[count] != old_array[count])
			text(SwappedColor on black);
		else
			text(Non_SwappedColor on black);

		cout << "   " << array[count] << " ";
	}
	cout << endl;
}

void printUnsortedArray(int *array, int size)
{
	cout << "  Unsorted    ";
	for (int count = 0; count < size; ++count)
		cout << "   " << array[count] << " ";
	cout << endl;
	cout << "--------------------------------------------------------------------------";
}
Hello studentlearningcplusplus,

In post http://www.cplusplus.com/forum/beginner/271885/#msg1172339 in the first block of code what part did you not understand?

You set up the function the way I showed you, but it is "main" that drives the program calling the functions that are needed.

It idea is not to have the function do more than 1 thing. That is why I set it up the way I did.

Andy

Edit:

P.S. Some what of a duplicate post.
Last edited on
Topic archived. No new replies allowed.