Write a program in C++ to find the largest and smallest of N numbers of any data type (use function overloading). The value of N is available only at the time of execution
#include <iostream>
#include <algorithm>
using namespace std;
short maxMin(short* array, int length) {
sort(array, array + length);
return array[length];
}
long maxMin(long* array, int length) {
sort(array, array + length);
return array[length];
}
int maxMin(int *array,int length) {
sort(array,array+length);
return array[length];
}
double maxMin(double *array,int length) {
sort(array, array + length);
return array[length];
}
float maxMin(float* array, int length) {
sort(array, array + length);
return array[length];
}
int main()
{
int arrayLength;
cout << "Enter array length: ";
cin >> arrayLength;
double *array = new double[arrayLength];
for (int i = 0; i < arrayLength; i++) {
cout << "Enter array["<<i+1<<"]= ";
cin >> array[i];
}
maxMin(array,arrayLength);
cout << "The largest element: " << array[arrayLength-1] << endl;
cout << "The smallest element: " << array[0] << endl;
return 0;
}
Comments
Leave a comment