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
|
#include <iostream>
#include <fstream>
using namespace std;
//This class sorts arrays either ascending or descending
template<class T>
class Prob2Sort
{
private:
int *index; //Index that is utilized in the sort
public:
Prob2Sort(){index=NULL;}; //Constructor
~Prob2Sort(){delete []index;}; //Destructor
T * sortArray(const T*,int,bool); //Sorts a single column array
T * sortArray(const T*,int,int,int,bool); //Sorts a 2 dimensional array
};
template<class T>
T* Prob2Sort<T>::sortArray(const T* arry, int rows, int cols, int column, bool order)
{
// put into new array to sort and return
T* a_ray = new T[(rows*cols) + 1];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
a_ray[i*cols+j] = arry[i*cols+j];
}
cout << endl;
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout << a_ray[i*cols+j] ; // displays fine
}
cout << endl;
}
T temp;
bool test = true;
do
{
test = false;
for(int i = 0; i < rows - 1; i++)
{
cout << "Row " << i << endl;
if(a_ray[i*cols+column] > a_ray[(i+1)*cols+column])
{
cout << "ELEMENT 1 IS " << a_ray[i*cols+column] << endl;
temp = arry[i*cols+column];
cout << "TEMP IS " << temp << endl;
a_ray[i*cols+column] = a_ray[(i+1)*cols+column];
cout << "ELEMENT 1 NEW is " << a_ray[(i)*cols+column] << endl;
cout << "ELEMENT 2 is " << a_ray[(i+1)*cols+column] << endl;
a_ray[(i+1)*cols+column] = temp;
cout << "ELEMENT 2 NEW is " << a_ray[(i+1)*cols+column] << endl;
test = true;
}
}
}while(test);
return column ;
}
int main(){
cout<<"The start of Problem 2, the sorting problem"<<endl;
Prob2Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Problem2.txt",ios::in);
char *ch2=new char[(4*17)+ 1];
char *ch2p=ch2;
while(infile.get(*ch2)){ch2++;}
infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,4,17,column,ascending);
for(int i=0;i<4;i++)
{
for(int j=0;j<17;j++)
{
cout<<zc[i*17+j];
}
}
delete []zc;
cout<<endl;
return 0;
}
|