Results 1 to 5 of 5

Thread: checking the largest and smallest #

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    checking the largest and smallest #

    I want a loop that asks the user for integers, wehn the user types 0, It displays the biggest and smallest integer s/he typed. And then it shows the difference under it. how would I, the easiest way.
    asdf

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    int max int *);
    int min(int *);
    int main(){
    int a[100];
    fir (int i=0;i<100;i++)a[i]=0;
    // loop for reading user input
    // blah
    
    printf("smallest %d\n", min(a));
    printf("largest %d\n", max(a));
    
    
    }
    
    // assumes 32 bit integers
    
    int max(int * a){
         int tmp = 0xefffffff; // smallest possible integer (negative)
         while( *a){
              if (*a > tmp) tmp = *a;
              a++;
         }
         return tmp;
    }
    
    int min(int * a){
         int tmp = 0x7fffffff; // biggest possible integer (postrive)
         while( *a){
              if (*a < tmp) tmp = *a;
              a++;
         }
         return tmp;
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Isn't the smallest possible integer 0xe0000000?
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW, the <algorithm> header contains functions to do that for you.
    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.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    #include <iostream>
    using namespace std;
    int main(){
     int x;
     cin>>x;
     in max=x,min=x;
     while (x){
      max=x>max?x:max;
      min=x<min?x:min;
      cin>>x;
     }
     cout<<min<<","<<max<<endl;
     return 0;
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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