We were asked to build a program where the computer receives the amount of integers and then we get asked each time to enter a number depending on the amount I entered and the program must give me the max number, min number and the sum.
For example,
Please enter how many integers you want to compute: 7
Please enter integer: 9
Please enter integer: 7
Please enter integer: 8
Please enter integer: 99
Please enter integer: 89
Please enter integer: 8
Please enter integer: 555555
The sum is 55575. The minimum is 7. The Maximum is 55555.
My question is, how can I make the program ask me to enter a number depending on the amount I entered? (like to ask me 7 times to enter an integer if I entered the amount of integers as 7).
we were also asked to solve this whole problem using this function: int sum_min_max (int amount, int& min, int& max) and to use 'reference' to solve this.
How to ask the user for something 'x' times?
A for-loop is how.
1 2 3 4
for (int i=0; i<input_number; i++) {
cout << "Please enter integer";
cin >> integer_array[i];
}
Where input_number is the inputted number and integer_array is the array that will store all inputs.
Basically "amount" in your function stands for input_number, which you have to retrieve in main function before calling the function. min and max are the variables that will hold the output, declare these in main function and pass to function. So the min and max needs to be computed inside the function itself.
Look up 'C++ pass by reference' if you don't know what the & in (int amount, int& min, int& max) stand for.