Changing Text Color of Array index

I have an ascending bubble sort program that sorts through an un-ordered array for which I am trying to alter it such that for each swap that takes place within the un-ordered array, the program changes the text color of only the items within the array that swapped indices within each printed pass. So far, I have only found suggestions that change the color of all of the indices of the array regardless of if their indices swapped during that pass or not... I am not sure how to write this such that it will do what I am trying to get it to do. Any suggestions are greatly appreciated.

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

using namespace std;

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

int main()
{
    HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

     SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY);

    string hyphen;
	const string progTitle = "Array Sorting Program";
	const int numHyphens = 100;

	hyphen.assign(numHyphens, '-');

  const int size = 8;

    int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };

    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);

	cin.ignore(cin.rdbuf()->in_avail());
	cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
	cin.get();
}

void sortArrayAscending(int *array, int size)
{
    HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO Info;
    WORD defaultAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    WORD swapAttribute = FOREGROUND_RED | FOREGROUND_INTENSITY;
    GetConsoleScreenBufferInfo(handle, &Info);
    defaultAttributes = Info.wAttributes;

	const int regTextColor = 2;
	const int swapTextColorChange = 4;

//	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

	int temp;
	bool swapTookPlace;
	int pass = 0;

	do
	{
		swapTookPlace = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				swapTookPlace = true;
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;

				if (swapTookPlace)
				{
                    SetConsoleTextAttribute(handle, swapAttribute);
                }


					if (pass < 9)
					{
						cout << fixed << setw(2) << " Pass #  " << (pass + 1) << " : ";
						pass += 1;
						printArray(&array[0], size);
					}
					else if (pass >= 9)
					{
						cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
						pass += 1;
						printArray(&array[0], size);
					}

				}

			}



	} while (swapTookPlace);
	SetConsoleTextAttribute(handle, defaultAttributes);

}

void printArray(int *array, int size)
{
	for (int count = 0; count < size; ++count)
		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 << "----------------------------------------------------------------------------------------------------";
}
@McBraunie

Thanks for the idea. It was a fun project to do. I created another array, and passed it into your printArray function after filling it with the old array, before you swap numbers. In the printArray function, if the two numbers don't match in the same array locations, then a swap had been done, and the color of text is set for a swapped number, else it's set for an non swapped number.
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
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>

using namespace std;

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

int main()
{
	HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

	SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY);

	string hyphen;
	const string progTitle = "Array Sorting Program";
	const int numHyphens = 100;

	hyphen.assign(numHyphens, '-');

	const int size = 8;

	int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };

	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);

	cin.ignore(cin.rdbuf()->in_avail());
	cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
	cin.get();
}

void sortArrayAscending(int *array, int size)
{
	HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO Info;
	WORD defaultAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
	WORD swapAttribute = FOREGROUND_RED | FOREGROUND_INTENSITY;
	GetConsoleScreenBufferInfo(handle, &Info);
	defaultAttributes = Info.wAttributes;

	const int regTextColor = 2;
	const int swapTextColorChange = 4;

	//	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
	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;
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;

				if (swapTookPlace)
				{
					SetConsoleTextAttribute(handle, swapAttribute);
				}
				if (pass < 9)
				{
					cout << fixed << setw(2) << " Pass #  " << (pass + 1) << " : ";
					pass += 1;
					printArray(&array[0],old_array, size);
				}
				else if (pass >= 9)
				{
					cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
					pass += 1;
					printArray(&array[0],old_array, size);
				}
			}
		}
	} while (swapTookPlace);
	SetConsoleTextAttribute(handle, defaultAttributes);
}

void printArray(int *array, int *old_array, int size)
{
	HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO Info;
	WORD defaultAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
	WORD swapAttribute = FOREGROUND_RED | FOREGROUND_INTENSITY;
	GetConsoleScreenBufferInfo(handle, &Info);
	defaultAttributes = Info.wAttributes;

	const int regTextColor = 2;
	const int swapTextColorChange = 4;
	for (int count = 0; count < size; ++count)
	{
		if(array[count] != old_array[count])
			SetConsoleTextAttribute(handle, swapAttribute);
		else
			SetConsoleTextAttribute(handle, regTextColor);

		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 << "----------------------------------------------------------------------------------------------------";
}
Last edited on
Wow! This is exactly what I was trying to do! Thank you for putting time in this and showing how you got there. I appreciate it!
@McBraunie

You're very welcome.
Topic archived. No new replies allowed.