Write a program using class template to arrange N numbers of type int and float in descending order.
Sep 2, 2019Source code:
//program to find the maximum of array template
#include <iostream>
using namespace std;
const int N = 7;
template <class Type>
class Array{
private:
Type arr[N];
public:
void read(){
for(int i = 0; i < N; i++){
cin>>arr[i];
}
}
void sortArr(){
Type temp;
int SIZE = sizeof(arr)/sizeof(Type);
for(int i = 0; i < SIZE - 1; i++){
for(int j = i + 1; j < SIZE; j++){
if(arr[i] < arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void display(){
int SIZE = sizeof(arr)/sizeof(Type);
for(int i = 0; i < SIZE; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
};
int main(){
Array <int> i_obj;
Array <float> f_obj;
//reading integer array
cout<<"Enter integer array:";
i_obj.read();
//reading floating number array
cout<<"Enter floating number array:";
f_obj.read();
i_obj.sortArr();
f_obj.sortArr();
cout<<"Sorted integer array:"<<endl;
i_obj.display();
cout<<endl<<"Sorted floating number array:"<<endl;
f_obj.display();
return 0;
}
Sample run:
Enter integer array:3 66 5 88 2 1 77 Enter floating number array:4.4 55.55 99.77 66.66 33.33 22.2 80.0 Sorted integer array: 88 77 66 5 3 2 1 Sorted floating number array: 99.77 80 66.66 55.55 33.33 22.2 4.4