Write a program which has a class template for determining the largest and the smallest number
from a list of numbers. Use a constructor for input and appropriate data members and member
functions in support of your answer.
#include<iostream>
using namespace std;
class tamplate{
public:
static int t;
int numbers[100];
tamplate(){ //constructor of class
cout<<"Enter how many no. in the list";
cin>>t;
cout << "Enter no. of numbers: " << endl;
for (int i = 0; i < t; ++i) {
cin >> numbers[i];
}
}
int largest(){
int n = sizeof(numbers) / sizeof(numbers[0]);
sort(numbers, numbers + n, greater<int>());
return numbers[0];
}
int smallest(){
int n = sizeof(numbers) / sizeof(numbers[0]);
sort(numbers, numbers + n, greater<int>());
return numbers[t-1];
}
};
int main(){
tamplate obj();
cout<<"largest no. in the list is"<<obj.largest()<<" and smallest no. in the list is"<<obj.smallest();
return 0;
}
Comments
Leave a comment