Answer to Question #172836 in C++ for Tripletunes

Question #172836

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.


1
Expert's answer
2021-03-18T07:12:24-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS