Hi, I am trying to write a program that finds the minimun and maximum number in an input set of numbers. I am using a loop to update the set as the program reads the cin.
Example code:

``````min = max = -1
cout << "Enter termination value that will signal the end of data entry :"
cin >> term_val ;
cout << "Enter data values :";
while (data != term_val){
cin >> data
if (data<min) min = data;
else if (data>max) max = data;
n++;
}``````

Min and max are initially set to -1, and somewhere I have to set min and max to the first data value. This loop should update the min and max values after they are set to the first data value. Where in the code do I have to do this?
Thanks

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by firstPerson

Hi, I am trying to write a program that finds the minimun and maximum number in an input set of numbers. I am using a loop to update the set as the program reads the cin.
Example code:

``````min = max = -1
cout << "Enter termination value that will signal the end of data entry :"
cin >> term_val ;
cout << "Enter data values :";
while (data != term_val){
cin >> data
if (data<min) min = data;
else if (data>max) max = data;
n++;
}``````

Min and max are initially set to -1, and somewhere I have to set min and max to the first data value. This loop should update the min and max values after they are set to the first data value. Where in the code do I have to do this?
Thanks

You don't have to initialize max and min to -1. You're going to initialize them to the first data elements. You have a variable n whose job is, I assume, to keep track of how many numbers have been inputted. Test whether n equals 0 at the top of your loop and if it is, that means it's the first value. If it is, do your initialization there. If not, do the comparisons of the inputted value as you do.

before the loop ask the user to enter a number and set min/max to
that number, then go onto the while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.