/********************************************************************************************
JM1206-1.cpp
Jim Millard
for CO211/Fall 1999
Program that finds the largest and smallest value from a set of inputs, then
echos those values to the user.
********************************************************************************************/
//included libraries
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//function prototypes
int largestOf(const int[], unsigned int count);
int smallestOf(const int[], unsigned int count);
//function definitions
int main()
{
int count = 0;
const int maxelements = 100;
int list[maxelements];
//inform and prompt the user
cout << endl << "This program will take a set of integers as input and " << endl
<< "return value of the largest and smallest values in the set." << endl
<< "The program will return the results when you enter [Ctrl-D] as input." << endl;
//prompt and extract the rest of the set
cout << "Enter a set of integers: ";
while ((count < maxelements) && (cin >> list[count]))
count++;
//output the results
cout << endl;
if (count > 0)
cout << "\nResults:" << endl
<< " Smallest: " << smallestOf(list, count) << endl
<< " Largest: " << largestOf(list, count) << endl;
else
cout << "No valid input was received; therefore, there are no results." << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Parses the array and returns the largest value of all elements in the array.
// count is the total number of valid elements in the array
int largestOf(const int A[], unsigned int count)
{
//check for a valid array
assert(count > 0);
//force-fit the first value as largest
int largest = A[0];
//check the rest of the set
for (int i = 1; i < count; i++)
if (A[i] > largest)
largest = A[i];
return largest;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Parses the array and returns the smallest value of all elements in the array.
// count is the total number of valid elements in the array
int smallestOf(const int A[], unsigned int count)
{
//check for a valid array
assert(count > 0);
//force-fit the first value as smallest
int smallest = A[0];
//check the rest of the set
for (int i = 1; i < count; i++)
if (A[i] < smallest)
smallest = A[i];
return smallest;
}