/* Largest Smallest Array Variable - This program lets the user enter 10
values into an array. The program displays the largest and smallest
values stored in the array. */
#include "Utility.h"
/* Prototypes: Get numbers, Get largest, Get smallest */
void getNumbers(int[], int);
int getLargest(const int[], int);
int getSmallest(const int[], int);
int main()
{
/* Constant: User numbers */
const int USER_NUMBERS = 10;
/* Variables: Numbers, Largest number, Smallest number */
int numbers[USER_NUMBERS],
largestNumber = 0,
smallestNumber = 0;
/* Display: Introduction */
cout << "\tThe Largest And The Smallest Array Value\n\n"
<< "Enter 10 random numbers, and this program will display\n"
<< "the largest and the smallest number you have entered.\n\n";
/* Call: getNumbers */
getNumbers(numbers, USER_NUMBERS);
/* Call: getLargest, getSmallest */
largestNumber = getLargest(numbers, USER_NUMBERS);
smallestNumber = getSmallest(numbers, USER_NUMBERS);
/* Display: The largest number, The smallest number */
cout << "\nThe largest number you entered was: "
<< setw(6) << right << largestNumber << endl;
cout << "The smallest number you entered was: "
<< setw(4) << right << smallestNumber << endl;
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getNumbers
This function accepts the array and its size as arguments.
It asks the user to enter 10 numbers which are then stored
in the array.
********************************************************** */
void getNumbers(int numbers[], int arraySize)
{
/* Get: The numbers from the user */
for (int count = 0; count < arraySize; count++)
{
cout << "Enter number #" << (count + 1) << ": ";
cin >> numbers[count];
while (numbers[count] < 0)
{
cout << "Input failure. Only positive numbers are allowed.\n"
<< "Enter number #" << (count + 1) << ": ";
cin >> numbers[count];
}
}
}
/* **********************************************************
Definition: getLargest
This function accepts an integer array with 10 elements as
its argument. It determines and returns the largest number
entered by the user.
********************************************************** */
int getLargest(const int numbers[], int arraySize)
{
/* Variable: Largest number (gets numbers[0] as a first
value to compare the numbers that follow to) */
int largestNumber = numbers[0];
/* Determine the highest number */
for (int count = 0; count < arraySize; count++)
{
if (numbers[count] > largestNumber)
{
largestNumber = numbers[count];
}
}
/* Return: The largest number */
return largestNumber;
}
/* **********************************************************
Definition: getSmallest
This function accepts an integer array as its argument. It
determines and returns the smallest number entered by the
user.
********************************************************** */
int getSmallest(const int numbers[], int arraySize)
{
/* Variable: Smallest number (gets numbers[0] as a first
value to compare the numbers that follow to) */
int smallestNumber = numbers[0];
/* Determine the smallest number */
for (int count = 0; count < arraySize; count++)
{
if (numbers[count] < smallestNumber)
{
smallestNumber = numbers[count];
}
}
/* Return: The smallest number */
return smallestNumber;
}
Sunday, January 22, 2017
Programming Challenge 7.1 - Largest Smallest Array Variable
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment