/* Filename: templat1.cpp Author: Br. David Carlson Date: June 28, 1999 Last Revised: December 23, 2001 This program has the user enter integers into an array, prints it, sorts it, and then prints it again. Tested with: Microsoft Visual C++ 2019 */ #include using namespace std; const int ArrayMax = 10; typedef int IntArrayType[ArrayMax]; /* Given: DataArray Array, with data from index 0 to index Count - 1. Count The number of data items in DataArray. Task: To print the data in DataArray. Assumes: That << works for data of type T. Return: Nothing. */ template void PrintArray(T DataArray[], int Count) { int k; for (k = 0; k < Count; k++) cout << DataArray[k] << endl; cout << endl; } /* Given: First An item of type T. Second An item of type T. Task: To swap the values in First and Second. Return: First Second */ template void Swap(T & First, T & Second) { T Temp; Temp = First; First = Second; Second = Temp; } /* Given: DataArray An array to be sorted, with data from index 0 to index Count - 1. Count The number of items in DataArray. Task: To bubblesort DataArray into ascending order. Return: DataArray The sorted array. */ template void BubbleSort(T DataArray[], int Count) { int Top, k; bool Done; Top = Count - 1; Done = false; while ((! Done) && (Top > 0)) { Done = true; for (k = 0; k < Top; k++) if (DataArray[k] > DataArray[k + 1]) { Done = false; Swap(DataArray[k], DataArray[k + 1]); } Top--; } } /* Given: Nothing. Task: To have the user enter integers and place them in IntArray. Return: IntArray The array of integers. Count The number of integers placed into IntArray. */ void LoadArray(IntArrayType IntArray, int & Count) { int Item; Count = 0; cout << "Enter a positive integer (0 to quit): "; cin >> Item; while ((Item != 0) && (Count < ArrayMax)) { IntArray[Count] = Item; Count++; cout << "Enter a positive integer (0 to quit): "; cin >> Item; } } int main(void) { IntArrayType IntArray; int Size; LoadArray(IntArray, Size); cout << endl << "The original unsorted array:" << endl; PrintArray(IntArray, Size); BubbleSort(IntArray, Size); cout << endl << "The sorted array:" << endl; PrintArray(IntArray, Size); return 0; }