473,385 Members | 1,730 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,385 software developers and data experts.

Finding min & max numbers

Hi all,

I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard (?) C++ in our class, not ANSI/ISO.

Anyway....

The problem is to do the following:
1. Ask for input of up to 10 numbers
2. Return the number of entries, the total, the average, the minimum and maximum values
3. Allow user to enter a sentinel of 9999 to end the program.

The part I'm having trouble with is the minimum/maximum. Here's part of the code I have so far:

Expand|Select|Wrap|Line Numbers
  1.    double num, total = 0, avg, min, max;
  2.    int count = 0;
  3.  
  4.    min = num;
  5.    max = num;
  6.  
  7.    while ( count < 10 )
  8.    {
  9.       cout << "Enter value #" << count + 1 << ": ";
  10.       cin >> num;
  11.  
  12.       if ( num != SENTINEL )        // Check for the SENTINEL value 9999
  13.       {
  14.          total += num;                  // total = total + num
  15.          count++;                        // Raise count by 1
  16.       }
  17.       else
  18.          break;                           // End program if SENTINEL is entered.
  19.  
  20.    // Evaluate min and max.  
  21.       if ( min > num )
  22.          min = num;                 // Change min only if num is lower
  23.       else if ( max < num )
  24.          max = num;                 // Change max only if num is higher
  25.    }
  26.  
If I put the min = num; max = num; part inside the while loop, min and max are reset every time the loop repeats. If I put it outside the loop, I keep getting a return of 0 for min.

Can someone please nudge me in the right direction? Thank you so much!
Oct 22 '07 #1
4 13416
Laharl
849 Expert 512MB
Lines 4 and 5 are unnecessary, as double defaults to 0.0 anyway.

As to your problem with min, you set it to 0.0 then only allow it to be changed if the input is less than that. You probably want to initialize to DOUBLE_MAX, which is C++'s maximum possible value for a double.

Basically, min should be initialized to something greater than any possible input and max should be initialized to something less than any possible input. Or you could use an array, if you've learned how to use them.
Oct 22 '07 #2
Thanks, Laharl! I had forgotten that double defaulted to 0. I didn't take as good notes last week as I usually do, so I don't remember exactly how the prof did it. I had thought he had assigned num to min and max, but I obviously got that wrong! He didn't use DOUBLE_MAX, though, that much I do remember.

We haven't learned arrays yet. We're moving on to functions next. :-)

Lines 4 and 5 are unnecessary, as double defaults to 0.0 anyway.

As to your problem with min, you set it to 0.0 then only allow it to be changed if the input is less than that. You probably want to initialize to DOUBLE_MAX, which is C++'s maximum possible value for a double.

Basically, min should be initialized to something greater than any possible input and max should be initialized to something less than any possible input. Or you could use an array, if you've learned how to use them.
Oct 22 '07 #3
Ganon11
3,652 Expert 2GB
Using DOUBLE_MAX is certainly one way to do it, but it's kind of messy. Your professor probably assigned min and max to the first number entered, and then proceeded with the loop, which is the approach I would take. So you did see "min = num; max = num;" but must have missed the separation between the first input and the looped input.
Oct 22 '07 #4
I was thinking that's what he did. I was trying to avoid having a separate cin for the first entry, but I couldn't figure out how that could be done.

(coming back to this reply 30 min. later...)
I got it! I wish I could say I figured it out logically, but mostly I got it to work by moving parts around before and after the SENTINEL, and inside and outside of the loop. I put a test for the first entry (if (count==0)) before the SENTINEL, and the min/max comparisons right after it.

Expand|Select|Wrap|Line Numbers
  1.    while ( count < 10 )
  2.    {
  3.       cout << "Enter value #" << count + 1 << ": ";
  4.       cin  >> num;
  5.  
  6.       if ( count == 0 )              // Set initial values of min and max
  7.       {
  8.          min = num;
  9.          max = num;
  10.       }
  11.  
  12.       if ( num != SENTINEL )        // Check for the SENTINEL value 9999
  13.       {
  14.          total += num;              // total = total + num
  15.          count++;                   // Raise count by 1
  16.       }
  17.       else
  18.          break;                     // End program if SENTINEL is entered.  
  19.  
  20.       if ( min > num )
  21.          min = num;                 // Change min only if num is lower
  22.       else if ( max < num )
  23.          max = num;                 // Change max only if num is higher 
  24.     }
  25.  
Thanks, Ganon11, for your help!

Your professor probably assigned min and max to the first number entered, and then proceeded with the loop, which is the approach I would take. So you did see "min = num; max = num;" but must have missed the separation between the first input and the looped input.
Oct 22 '07 #5

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

Similar topics

4
by: Han | last post by:
Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences of the following pattern: score=9999999999&amp; The number shown can be 5-10 characters...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
6
by: GSpiggle | last post by:
I have a database with records that should have a consecutive number ID (Check Register). Also has other records (deposits and adjusting entries) that don't fit in the number range so...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
3
by: Stephen | last post by:
Hi, I have a table consists which cosists of batch numbers, and assosiated dates and times..ie the columns are batch, date and time. The data within the table is not in any particular order. ...
12
by: e271828 | last post by:
Hi, I'm helping to work a developer tool that verifies a given HTML element has a given attribute (e.g., that all LABEL elements have a FOR attribute, all INPUT elements have an ID attribute,...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
10
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.