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 << "--------------------------------------------------------------------------";
}
|