Results 1 to 4 of 4

Thread: lowest variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    71

    lowest variable

    I have 5 numbers and each is stored in its own variable. How do I find the lowest number?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hint: Make an array of pointers, then loop through it to find the lowest.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    int a[5];
    a[0]=23; 
    a[1]=13;  ..... etc
    printf("%d\n",min(a,5) );
    ...............
    int min(int *whatever,int cnt){
    
           int *ptr;
           int result=0   // assume all numbers are positive
           for(ptr=whatever;cnt;cnt--,ptr++) 
                              if (*ptr>result) result=*ptr;
           return result;
                
    }

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    min is not a good choice for the name, there is a macro with that name.

    In C++ you can use the min_element algorithm.

    For parksie's suggestion, here's what you would do (in C++):
    Code:
    #include <algorithm>
    using std::min_element;
    
    int a, b, c, d, e; // your 5 integers
    // parksie's suggestion:
    int * ar[5] = {&a, &b, &c, &d, &e};
    // my suggestion:
    struct pred_minptr {
      bool operator ()(int *pa, int *pb) {
        return *pa < *pb;
      };
    };
    
    // find the minimum:
    int min = **min_element(ar, ar+5, pred_minptr());
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width