Write overloaded function named Sort that takes array of integers, float numbers and
characters. Get the user input and display it in the sorted order.
#include <iostream>
#include <algorithm>
using namespace std;
void Sort(int x[10], int size, char order){
sort(x, x + size);
if(order == 'a'){
for(int i = 0; i < size; i++){
cout<<x[i]<<", ";
}
}
if(order == 'b'){
for(int i = size - 1; i >= 0; i--){
cout<<x[i]<<", ";
}
}
}
void Sort(float x[10], int size, char order){
sort(x, x + size);
if(order == 'a'){
for(int i = 0; i < size; i++){
cout<<x[i]<<", ";
}
}
if(order == 'b'){
for(int i = size - 1; i >= 0; i--){
cout<<x[i]<<", ";
}
}
}
void Sort(char x[10], int size, char order){
sort(x, x + size);
if(order == 'a'){
for(int i = 0; i < size; i++){
cout<<x[i]<<", ";
}
}
if(order == 'b'){
for(int i = size - 1; i >= 0; i--){
cout<<x[i]<<", ";
}
}
}
int main(){
int max = 10, n[max], count;
float x[max];
char v, c[max], order;
do {
cout<<"How many items do you wish to sort? (maximum of "<<max<<" items)\n";
cin>>count;
cout<<"Do you want to sort a) integers, b) floats or c) characters? (a, b or c)\n";
cin>>v;
cout<<"In what order do you wish to sort them? (a) ascending, b) descending)\n";
cin>>order;
cout<<"Enter the items \n";
}while(v != 'a' && v != 'b' && v != 'c' && count > 10);
if(v == 'a'){
for(int i = 0; i < count; i++)
cin>>n[i];
cout<<"The sorted list is array is ";
Sort(n, count, order);
}
else if(v == 'b'){
for(int i = 0; i < count; i++)
cin>>x[i];
cout<<"The sorted list is array is ";
Sort(x, count, order);
}
else if(v == 'c'){
for(int i = 0; i < count; i++)
cin>>c[i];
cout<<"The sorted list is array is ";
Sort(c, count, order);
}
return 0;
}
Comments
Leave a comment