template<class T> T* Prob2Sort<T>::sortArray(const T* arry, int rows, int cols,
ID: 3548509 • Letter: T
Question
template<class T>
T* Prob2Sort<T>::sortArray(const T* arry, int rows, int cols, int column, bool order){
//Put into new array to sort
T* a_ray
}
template<class T>
class Sort
{
private:
int *index; //Index that is utilized in the sort
public:
Sort(){index=NULL;}; //Constructor
~Sort(){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
};
And the Driver Program:
Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Array.txt",ios::in);
char *ch2=new char[10*16];
char *ch2p=ch2;
while(infile.get(*ch2)){cout<<*ch2;ch2++;}
infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,10,16,column,ascending);
for(int i=0;i<10;i++)
{
for(int j=0;j<16;j++)
{
cout<<zc[i*16+j];
}
}
delete []zc;
cout<<endl;
"Array.txt" includes:
Lbekoeddhoffbmg
Lkcmggjcdhhglif
Cgldjhcekjigcdd
Cgldjhcekjigcdn
Bffmdbkcenlafjk
Fggdijijegfblln
Jjlncnimjldfedj
Amliglfohajcdmm
Balgfcaelhfkgeb
Kmlhmhcddfoeilc
So using the given Class file and the Driver file, Write the implementation file that will read in an
array of characters from "array.txt" into your choice of a 1 or 2d array. Then allow the user to pick a
column of that array to sort. So if the user picked column 1 to sort,
it would spit out the entire array but it would be based upon the Letters in the corresponding column.
Lbekoeddhoffbmg
Lkcmggjcdhhglif
Kmlhmhcddfoeilc
Jjlncnimjldfedj
Fggdijijegfblln
Cgldjhcekjigcdd
Cgldjhcekjigcdn
Bffmdbkcenlafjk
Balgfcaelhfkgeb
Amliglfohajcdmm
^ Sort column: 1(Sorted in ascending order)
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
template<class T>
class Sort
{
private:
int *index; //Index that is utilized in the sort
public:
Sort(){index=NULL;}; //Constructor
~Sort(){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* Sort<T>::sortArray(const T* arr,int row,bool sortType)
{
T temp;
if(sortType == true)
{
for(int i=0; i< row; i++)
{
for(int j=i+1; j<=row; j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
else
{
for(int i=0; i< row; i++)
{
for(int j=i+1; j<row; j++)
{
if(arr[i]<arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
return arr;
}
template<class T>
T* Sort<T>::sortArray(const T* arrInput, int rows, int cols, int column, bool sortType)
{
// put into new array to sort and return
T* arr = new T[(rows*cols) + 1];
int index;
char ch1, ch2;
int n;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
arr[i*cols+j] = arrInput[i*cols+j];
}
}
cout<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout << arr[i*cols+j]; // displays fine
}
}
cout<<endl;
T temp;
if(sortType ==true)
{
cout<<"Sorting in Ascending Order"<<endl;
for(int i=0; i<cols; i++)
{
for(int j=i+1;j<cols;j++)
{
index = (column-1)*cols;
ch1 = tolower(arr[index+i]);
ch2 = tolower(arr[index+j]);
if((isalpha(ch1) || isdigit(ch1)) && (isalpha(ch2) || isdigit(ch2)))
{
if(ch1 > ch2)
{
temp = arr[index+i];
arr[index+i] = arr[index+j];
arr[index+j] = temp;
}
}
}
}
}
else
{
cout<<"Sorting in Descending Order"<<endl;
for(int i=0; i<cols; i++)
{
for(int j=i+1;j<cols;j++)
{
index = (column-1)*cols;
ch1 = tolower(arr[index+i]);
ch2 = tolower(arr[index+j]);
if((isalpha(ch1) || isdigit(ch1)) && (isalpha(ch2) || isdigit(ch2)))
{
if(ch1 < ch2)
{
temp = arr[index+i];
arr[index+i] = arr[index+j];
arr[index+j] = temp;
}
}
}
}
}
return arr;
}
int main()
{
Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("input.txt",ios::in);
char *ch2=new char[10*16];
char *ch2p=ch2;
while(infile.get(*ch2))
{
cout<<*ch2;
ch2++;
}
infile.close();
cout<<endl;
cout<<endl<<"Sorting on which column: "<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,11,16,column,ascending);
cout<<"After Sorting: "<<endl;
for(int i=0;i<11;i++)
{
for(int j=0;j<16;j++)
{
cout<<zc[i*16+j];
}
}
delete []zc;
cout<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.