1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
/*
Problem description: Write a program that reads an unspecified number (no more than 20) of
positive integers, finds, and prints the maximal and minimal values using an array.
The input ends with the 0. For example, if you entered 1, 100, 20, 2, and 0,
the program should print 100 and 1.
This exercise is for learning the array concept with a proper use of reference and value parameters.
*/
#include <iostream>
using namespace std;
//function prototype of a function returning max & min values of an integer array
//function prototype of getBoundary function returning max, min values of an void void
void getBoundary(int value[], int size, int& max, int& min);
int main( )
{
const int CAPACITY = 20; //array capacity
int value[CAPACITY];
int max, min; // maximum and minimum values stored in the array value
int intNum; //stores the number of integer read
// read values from the input until it reads 0 and store the values in the array except 0
int i=0;
for(int j=0; j<CAPACITY; j++)
{
value[i]=0;
}
int num;
intNum=0;
cin>>num;
while(num!=0 && num<=20)
{
value[i]=num;
intNum++;
i++;
cin>>num;
}
getBoundary(value, intNum, max, min);
cout << intNum <<" integers have been read." << endl
<< "The maximal value is " << max << endl
<< "The minimal value is " << min << endl;
return 0;
}
void getBoundary ( int value[], int size, int&max, int&min)
{
min=0;
max=0;
for(int i=0;i<20;i++)
{
if(min>value[i])
{
min=value[i];
}
else if(max<value[i])
{
max = value[i];
}
}
}
}
|