423,323 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,323 IT Pros & Developers. It's quick & easy.

calculating the min and max from a loop

P: 1
Hello, I am writing a program that asks the user how many integers they would like to enter.

You can assume they will enter a number >= 1. The program will then prompt the user to enter that many integers.

After all the numbers have been entered, the program should display the largest and smallest of those numbers.


I am having a couple problems.

For example, if I input that I want to enter 4 integers I can only input 3 numbers before it calculates the min and max.

Additionally, if I input that I want to enter 4 integers, and one of those integers is negative (-1 for instance), the output is shortened and is sometimes incorrect.

Any insight is much appreciated.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   int input;
  7.   int tmp;
  8.   int counter = 1;
  9.   int max_num = -1;
  10.   int min_num = -1;
  11.  
  12. //prompt user for integer amount
  13.   cout << "How many integers would you like to enter? " << endl;
  14.   cin >> input;
  15.  
  16.   cout << "Please enter " << input << " integers." << endl;
  17.   tmp = input;
  18.  
  19. //loop for requested amount with a test for each input
  20.   while (counter <= tmp){
  21.     cin >> input;
  22.  
  23. //if smaller than previous number it is the minimum
  24.   if (input < min_num || min_num == -1){
  25.     min_num = input;
  26.     counter++;
  27.   }
  28.  
  29. // if larger than previous number it becomes max number else
  30.   if (input > max_num){
  31.     max_num = input;
  32.     counter++;
  33.   }
  34.  
  35. //continue loop if number isn't bigger than max or smaller than min
  36.   else {
  37.     counter++;
  38.   }
  39. }
  40.  
  41. //display the max and min
  42. cout << "min: " << min_num << endl;
  43. cout << "max: " << max_num << endl;
  44.  
  45. return 0;
  46.  
  47. }
  48.  
Oct 8 '17 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,163
When you process the first number, the first "if" tests if min_num == -1. It is so increment the counter.

The second "if" tests the number > max_num. At this point max_num is -1 so the number becomes the new max. And increment the counter.

Therefore, the counter is incremented twice on the first number.
Oct 9 '17 #2

Frinavale
Expert Mod 5K+
P: 9,731
You aren't incrementing your counter properly.

Right now you have:
Expand|Select|Wrap|Line Numbers
  1. while (counter <= tmp){
  2.   cin >> input;
  3.  
  4.   if (input < min_num || min_num == -1){
  5.     //increment counter
  6.   }
  7.  
  8.   if (input > max_num){
  9.     //increment counter
  10.   }else{
  11.     //increment counter
  12.   }
  13. }
  14.  
This means that your counter gets incremented the fist time someone enters a number (because min_num is -1) and then it gets incremented again in the second if-block.

You should only be incrementing counter once per loop. And it should be incremented regardless of the user's input because it is simply used to count the number of integers that the user has entered so far.


You should have something like:
Expand|Select|Wrap|Line Numbers
  1. while (counter <= tmp){
  2.   cin >> input;
  3.  
  4.   if (input < min_num || min_num == -1){
  5.     //do stuff to set the minimum number entered
  6.   }
  7.  
  8.   if (input > max_num){
  9.     //do stuff to set the maximum number entered
  10.   }
  11.  
  12.   //increment counter that is tracking the number
  13.   //of integers the user has provided so far
  14.   //(regardless of what you have done above)
  15.   counter++;
  16. }
  17.  
Oct 10 '17 #3

Expert 100+
P: 2,378
You say the first entered value can be assumed to >= 0. Is there any reason why none of the subsequent entered numbers can't be negative? Using -1 as a sentinel value leaves your program unable to handle negative numbers. Instead of special sentinel values, consider a state variable; there are 2 states: no min/max yet, and got tentative min/max.

I wouldn't trust the user not to enter invalid values. You should range-check each input.
Oct 11 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.