Answer to Question #320115 in C++ for Ajay

Question #320115

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

1
Expert's answer
2022-03-29T11:41:01-0400
#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;
}

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