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;
template <typename T>
class A
{
public:
int a[10];
A(T arr[], int s);
} ;
template<typename T>
A<T>::A(T arr[], int s)
{
cout<<"\nEnter elements of array ";
for(int i=0;i<s;i++)
{
cin>>arr[i];
}
for (int i = 0; i < s - 1; i++)
for (int j = s - 1; i < j; j--)
if (arr[j] < arr[j - 1])
swap(arr[j], arr[j - 1]);
cout<<"\nSmallest number of array is "<<arr[0];
cout<<"\nLargest number of array is "<<arr[s-1];
}
int main() {
int n,arr[10];
cout<<"Enter number of elements in the array ";
cin>>n;
A<int> b(arr,n);
return 0;
}
Comments
Leave a comment