Like Tree4Likes
  • 1 Post By laserlight
  • 1 Post By whiteflags
  • 1 Post By Elysia
  • 1 Post By laserlight

Finding maximum value of dynamic parameter

This is a discussion on Finding maximum value of dynamic parameter within the C++ Programming forums, part of the General Programming Boards category; Hi, Could do with some advice here... My Aim/Motivation: I have a parameter which is constantly updated throughout the code. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Talking Finding maximum value of dynamic parameter

    Hi,

    Could do with some advice here...

    My Aim/Motivation:
    I have a parameter which is constantly updated throughout the code. I want to find the value for this parameter at its maximum.


    I want this to be as fast as possible and I dont want to store much data if I can help it.

    I am sure things like this have been done many a time before. So I am seeking the expert advice of this forum.

    Any Ideas/Suggestions/websites/Examples? most welcome!

    thanks!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    The most straightforward solution to me involves creating a class for which this variable is a member, and the largest encountered value of this variable is another member. So, you control the updating of this variable such that when a larger value is assigned to it, the other member is updated.
    jimblumberg likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    You need to provide some concrete example of what you mean. Is this "parameter" a global variable? A member of a class? I recommend making it a private member of a class, and have the set function test to see if is maximum and store the value in another private class variable if it is the maximum. By encapsulating this variable in a class you can enforce the checking for the maximum value whenever that variable is changed.

    Edit: too slow.

    Jim

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    He could mean parameter as in an argument to a function, in which case I think he's looking to find the maximum in the wrong place. It would be better to find out where the parameter came from and do some analysis, rather than try to do it from inside the function. How good your answer is depends on how many times the function was called, otherwise.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks to both!

    This parameter is relating to maximum position of a body (from a given reference) which is constantly being updated due to other equations being iterated which determine the bodies motion.

    So this parameter is constantly being updated within a while loop.

    Can you offer an alternative suggestion to using a class?

    thanks again

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by strokebow
    Can you offer an alternative suggestion to using a class?
    What do you find deficient about the suggestion that you would like an alternative?

    EDIT:
    Quote Originally Posted by strokebow
    So this parameter is constantly being updated within a while loop.
    Oh, then another possible solution is to keep track of the largest known value in the code surrounding the while loop, i.e., "constantly updated throughout the code" is not actually the case.
    Last edited by laserlight; 07-03-2012 at 10:11 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Well laserlight's idea still works, but instead of writing a class you would declare another variable for the maximal value and then call std::max() over and over in the loop. When the loop is done, just look at that maximal value.
    strokebow likes this.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by whiteflags View Post
    Well laserlight's idea still works, but instead of writing a class you would declare another variable for the maximal value and then call std::max() over and over in the loop. When the loop is done, just look at that maximal value.
    Thanks!!

    So basically, just keep a variable for the max.

    I suppose its quicker to just do:
    Code:
    			xMax = (X > xMax)?X:xMax;
    				yMax = (Y > yMax)?Y:yMax;
    Whereby, if X is greater than xMax then the value for X becomes the new value for xMax otherwise xMax keeps its current value. And similarly with Y?

    What do you think? OK?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    FYI, there is a function named std::max you can use if you want. Does the exact same thing.
    strokebow likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Furthermore,

    How would I do this in the fastest possible way to also take account of negative values.

    i.e. the maximum value irrespective of sign.

    Would it be just a case of:

    Code:
    xMax = (abs(X) > xMax)?abs(X):xMax;
    	yMax = (abs(Y) > yMax)?abs(Y):yMax;
    Or is there a faster way of doing this?

    thanks again

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by strokebow
    How would I do this in the fastest possible way to also take account of negative values.
    I don't know about fastest, but simplest:
    Code:
    xMax = std::max(xMax, std::abs(X));
    strokebow likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the 50% values around a maximum
    By strokebow in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2011, 01:13 PM
  2. finding the second maximum.
    By lbillys in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 08:23 PM
  3. Finding the maximum in an array
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-15-2006, 06:07 AM
  4. Finding maximum queue length
    By crepincdotcom in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-05-2004, 09:31 AM
  5. finding maximum munber
    By condorx in forum C Programming
    Replies: 5
    Last Post: 03-25-2002, 11:46 PM

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