compare input doubles

This is a discussion on compare input doubles within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that accepts an undetermined amount of doubles as input and compares those values ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    compare input doubles

    I am trying to write a program that accepts an undetermined amount of doubles as input and compares those values and determines a high, low and an average. I am using -1 as a sentinel for end of input.

    Attached is the code I have so far. It is not setting the minimum value and the average always includes the -1 as a value. I have compensated for the -1 in my average calculation by adding and subtracting 1.

    Any help is appreciated.

    Brian
    Attached Files Attached Files

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Well, you were checking for -1 too late, for one. Second, the comparison against max doesn't affect min. Consider that. Also, if the user enters 100 for the lowest number, the min will remain 0!
    Finally, the -1 shouldn't find it's way into the calculations. If you were using an array, for instance, you would simply stop calculating/iterating once you reached the sentinal.
    Attached Files Attached Files
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    still problems

    I looked at the code you sent and compiled and ran it. It works fine if the numbers are entered descending, however, if the user enters 1,2,3,4,5 as the values, the output for the minimum is 1e+07, or 10,000,000 as it is declared. The value for min doesn't get reset to the lowest value.

    Brian

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Just give it a go. Mentally trace through the program with acending input.

    If you see the mistake I made, let me know about it.
    Otherwise, if you know what printf() does, let me know.

    It's a riddle!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  2. Replies: 16
    Last Post: 01-04-2007, 02:38 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21