Write a program with 3 functions:
void input(int vals[], const int MAX, int & size);
void maxmin(int vals[], int size, int& max, int & min);
void output(int vals[], int size);
In your main program, define the array as int values[MAXSIZE];
with const int MAXSIZE = 100;
Call input, then output, then call maxmin and display the max and min values.
Read the values into the array in the input function and display them in the output function. The maxmin function should “return” the largest and smallest values in the array. The input function should detect the end of input by reading the ctrl-z key combination from the keyboard.
Run the program for the values 1,2,3,4,5,6,7,8,9,10 as input.
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
|
#include <iostream>
using namespace std;
void input(int vals[], const int MAX, int& size);
void maxmin(int vals[], int size, int& max, int& min);
void output(int vals[], int size);
void main()
{
const int MAXSIZE = 100;
int values[MAXSIZE];
int size, max, min;
input(values, MAXSIZE, size);
maxmin(values, size, max, min);
output(values, size);
}
void input(int vals[], const int MAX, int& size)
{
size = 0;
int value;
cout << "enter values (ctrl-z to end): \n";
while ((cin >> value) && (size < MAX))
{
vals[size] = value;
size++;
}
return;
}
void maxmin(int vals[], int size, int& max, int& min)
{
max = vals[0];
min = vals[0];
for (int i = 1; i < size; i++)
{
if (min > vals[i])
{
min = vals[i];
}
if (max < vals[i])
{
max = vals[i];
}
}
}
void output(int vals[], int size)
{
for (int i = 0; i < size; i++)
{
cout<< vals[i] << " ";
}
cout<<endl;
return;
}
|
My output is the numbers occupying the array 1,2,3,4,... No surprise there as I just inserted some code from another array and function problem we did.
What I don't understand is how am I supposed to pass the min and max values to the output function if I can't list them in my formal parameters in the declaration or as variable in the argument in the call. What am I missing???