Explore BrainMass

Explore BrainMass

Find the Largest and Smallest and Sort values in array C++

Write two function in C++ with using arrays to find largest and smallest number.
For example you have 1,43,42,9,8,5. You need arrange them smallest to highest and highest to smallest.

Solution Summary

Two functions: findMin() and findMax() have been coded and tested in main().
Please find the code, Arr1.cpp and executable Arr1.exe attached.
The code is explained with comments. You should be able to use the two functions to achieve what you want including sorting of an array.

For sorting, you need to do a little bit more than just finding minimum and maximum from an array. Simply finding the smallest and/or largest does not automatically lend itself to a sorted array.
Specifically, for an array containing positive integers, you need to repeatedly find the largest number (or smallest number) from the array, print it and replace that largest number with a (say ) negative number, so that it doesn't get picked again in the next iteration. Continue doing this for n elements, where n is the length of the array.