473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

calculating the min and max from a loop

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
3 1914
weaknessforcats
9,208 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
donbock
2,426 Expert 2GB
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

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

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
3
by: John F. | last post by:
Hi all, I'm searching fulltime for days now, and I don't find the solution, so I'm thinking this is more a bug :( If i use following code in form&subforms:
19
by: Stephan Aspridis | last post by:
Hi folks, a happy new year. I have a little problem with a program I am writing at the moment. A loop doesn't behave the way I'd like to (namely, the "break" is ignored). This is the code in...
8
by: Dave Veeneman | last post by:
In a for-loop, is a calculated expression re-calculated on each pass through the loop, or only once, when the loop is initialized? For example, assume the following loop: for (int i = 0; i <...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
2
by: LostDeveloper via AccessMonster.com | last post by:
Relationship goes Certification can have multiple variations (any single variation satifies the certification), which require multiple course groups which require any one of the products assigned...
25
by: Blaize | last post by:
Hi, I'm having an issue trying to calculate time. Its okay if the value does not exceed 24 hours otherwise I get a date and hours listed. For example, I have a loop which looks through a table...
2
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the...
2
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'm making a quick little ftp uploader as a test and I'm trying to track upload speed. I'm using a loop to write 2048 bytes at a time to the ftp stream, so my logic was to run the loop a predefined...
3
by: Flibberdigibbet | last post by:
I am attempting to write a loop that will use rudimentary methods to solve an integral (as in the area under a curve) to a certain user-specified degree of accuracy by calculating the area of a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.