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
|
#include <iostream>
using namespace std;
//Template class
template <class T>
//sort prototype
void sort (int n, T a[]);
//Template class
template <class T>
//swap prototype
void swap (T a[], T a[]);
void main()
{
//declare variable n as int. Will store # of elements to be read.
int n;
//declare variable whichType. Will determine the type of array to be passed to sort function
int whichType;
//declare 3 arrays. One int, One double, and One char
int a[25];
double b[25];
char c[25];
//ask User what type of data they want to use for the array
cout << "Which type do you want for your array of numbers?\n";
cin >> whichType;
//Three different if statements will be used: One for each data type
//Each if statement will include:
//Entering a value for n
//Entering values for the array
//print out the list in original order - This might take place in the sort function
//sort function is called: arguments are int n and T a[]
if(whichType == 1)
{
cout << "Enter the number of elements.\n";
cin >> n;
cout << "populate the elements.\n";
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cout << a[i] << " ";
sort (n, a);
}
if(whichType == 2)
{
cout << "Enter the number of elements.\n";
cin >> n;
cout << "populate the elements.\n";
for(int i = 0; i < n; i++)
cin >> b[i];
for(int i = 0; i < n; i++)
cout << b[i] << " ";
sort (n, b);
}
if(whichType == 3)
{
cout << "Enter the number of elements.\n";
cin >> n;
cout << "populate the elements.\n";
for(int i = 0; i < n; i++)
cin >> c[i];
for(int i = 0; i < n; i++)
cout << c[i] << " ";
sort (n, c);
}
//numbers should be in order after the sort function
//cout this sorted list
}//main
//Template class
template <class T>
//sort function
void sort (int n, T a[])
{
//declare variables last and m as int
int last;
int m;
//May print out original order of arrays here. Not sure
//Will use for loop and nested for loop
//The first for loop will increment up to the # of elements that are in the array
for(last = 1; last < n; ++last)
//The second loop will run so eventually all numbers are tested and sorted properly
for(m = last; m > 0; --m)
{ //Inside this loop there is a specific if statement that tests two values
//If the value to the right is less than the value to the left, swap them
if(a[m] < a [m-1])
//The swap function is then called with arguments a[m] and a[m-1]
{ swap(a[m], a[m-1]); }
//If not, then break out of the loop
else break;
}
cout << T a[];
}// sort
//Template class
template <class T>
//swap function
void swap (T a[], T a[])
{
//Cout the input to swap
//declare variable temp as T
T temp;
temp = a[]; //Like previous project, temp will hold the value of a[m]
a[] = a[]; //a[m] will take the value of a[m-1]
a[] = temp; //a[m-1] will take the value in temp, which was the old value of a[m]
//Cout the output from swap.
}
|