Results 1 to 14 of 14

Thread: Min and Max value in array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2007
    Posts
    36
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: Min and Max value in array

    This is how I did it:

    _maxValue = _dataValues[0];
    _minValue = _dataValues[0];


    for(int i = 0; i<200; i++){
    _maxValue = qMax(_maxValue, _dataValues[i]);
    _minValue = qMin(_minValue, _dataValues[i]);
    }

    How can I know when max or min value is found, so that I can use index.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Min and Max value in array

    Quote Originally Posted by Benjamin View Post
    for(int i = 0; i<200; i++)
    Hard coded size is dangerous!

    Qt Code:
    1. _maxValue = _dataValues[0];
    2. _minValue = _dataValues[0];
    3. int _minValueIndex = 0;
    4. int _maxValueIndex = 0;
    5. for (int i=1; i<_dataValues.size(); i++)
    6. {
    7. if (_dataValues[i] < _minValue)
    8. {
    9. _minValue = _dataValues[i];
    10. _minValueIndex = i;
    11. }
    12. if (_dataValues[i] > _maxValue)
    13. {
    14. _maxValue = _dataValues[i];
    15. _maxValueIndex = i;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    (WITHOUT catching the case if there are two/more min/max values!)

  3. #3
    Join Date
    Dec 2007
    Posts
    36
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    2

    Default Re: Min and Max value in array

    Thank you for your replay.

    I am wondering one thing, can this way of finding min and max values work with mixed positive and negative numbers?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Min and Max value in array

    Quote Originally Posted by Benjamin View Post
    I am wondering one thing, can this way of finding min and max values work with mixed positive and negative numbers?
    Why not? (-1 > 2) or (2 > -1) are fine...

  5. The following user says thank you to Lykurg for this useful post:

    Benjamin (27th February 2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.