largest and smallest

This is a discussion on largest and smallest within the C Programming forums, part of the General Programming Boards category; i need to make a program that reads five different numbers but tells me the largest number and the smallest ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    denver
    Posts
    1

    largest and smallest

    i need to make a program that reads five different numbers but tells me the largest number and the smallest number. i finished most of it but i have problems displaying the largest and the smallest number in a sentence. i need it to say for the largest number "the largest interger is:" and for the smallest one, i need it to display "the smallest integer is:". what i finished looks like this...

    Code:
    # include <stdio.h>
    int main (void)
    {
    int integer1;
    int integer2;
    int integer3;
    int integer4;
    int integer5;
    
    printf("Enter first integer: \n");
    scanf_s("%d", &integer1);
    
    printf("Enter second integer: \n");
    scanf_s("%d", &integer2);
    
    printf("Enter third integer: \n");
    scanf_s("%d", &integer3);
    
    printf("Enter forth integer: \n");
    scanf_s("%d", &integer4);
    
    printf("Enter fifth integer: \n");
    scanf_s("%d", &integer5);
    
    if((integer1 > integer2)&&
    (integer1 > integer3)&&
    (integer1 > integer4)&&
    (integer1 > integer5)){
    printf("%d", integer1);
    }
    
    if((integer2 > integer1)&&
    (integer2 > integer3)&&
    (integer2 > integer4)&&
    (integer2 > integer5)){
    printf("%d", integer2);
    }
    
    if((integer3 > integer1)&&
    (integer3 > integer2)&&
    (integer3 > integer4)&&
    (integer3 > integer5)){
    printf("%d", integer3);
    }
    
    if((integer4 > integer1)&&
    (integer4 > integer2)&&
    (integer4 > integer3)&&
    (integer4 > integer5)){
    printf("%d", integer4);
    }
    
    if((integer5 > integer1)&&
    (integer5 > integer2)&&
    (integer5 > integer3)&&
    (integer5 > integer4)){
    printf("%d", integer5);
    }
    
    if((integer1 < integer2)&&
    (integer1 < integer3)&&
    (integer1 < integer4)&&
    (integer1 < integer5)){
    printf("%d", integer1);
    }
    
    if((integer2 < integer1)&&
    (integer2 < integer3)&&
    (integer2 < integer4)&&
    (integer2 < integer5)){
    printf("%d", integer2);
    }
    
    if((integer3 < integer1)&&
    (integer3 < integer2)&&
    (integer3 < integer4)&&
    (integer3 < integer5)){
    printf("%d", integer3);
    }
    
    if((integer4 < integer1)&&
    (integer4 < integer2)&&
    (integer4 < integer3)&&
    (integer4 < integer5)){
    printf("%d", integer4);
    }
    
    if((integer5 < integer1)&&
    (integer5 < integer2)&&
    (integer5 < integer3)&&
    (integer5 < integer4)){
    printf("%d", integer5);
    }
    return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use a loop and an array. What you did is not the way to do it.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't really need more than three variables to solve this problem: One that is the current input, one to hold the largest value, one to hold the smallest value. [Ok, if you want to make it a loop over x number of inputs, you will also need a fourth variable as the loop count].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    int main()
    {
       int a;
       int lar, less, flag;
       
       lar = less = flag = 0;
       
       for(i=0;i<5;i++)
       {
          printf("Num &#37;d : ", i);
          scanf("%d",&a);
          
          if(!flag){ less=a; flag = 1; }
          
          if(res < a)
             res = a;
          
          if(less > a)
             less = a;
       }
       
       printf("Result -> %d %d\n", res, less);
       
       getchar();
       return 0;
    }
    Looked for a samll program which i wrote quite a while ago. Which was just the similar to what u have asked. And i guess Mats is speaking about it as well.

    ssharish
    Last edited by ssharish2005; 10-15-2007 at 12:09 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That won't compile, as "a" is not declared.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = 0, max = 0;
    
        while(scanf("&#37;d", &n) == 1)
        {
            if(n > max)
                max = n;
    
            printf("Current max: %d\n", max);
        }
        return 0;
    }
    Or with 2 variables

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, that doesn't read exactly 5 numbers, nor does it tell you the smallest.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Nor does it give away the answer

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by matsp View Post
    That won't compile, as "a" is not declared.

    --
    Mats
    Maps sorted. My bad, was editing few thing in the code before i posted and left that unchnaged.

    ssharish

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    i have a feeling that the person who posted this question is in the same uni and class as i am LOL because this is one of the question for today lab session

    anyway.. what i did :

    Code:
    #include <stdio.h>
    
    int main()
    {
       int largest;
       int smallest;
       int input;
       int counter = 0;
    
       printf( "Enter an integer\n" );
       scanf( "&#37;d", &input );
    
       largest = input;
       smallest = input;
    
       while( counter < 4)
       {
    
       printf( "Enter an integer\n" );
       scanf( "%d", &input );      
    
          if ( input > largest )
          {
             largest = input;
    
          }
    
          if (input < smallest )
          {
             smallest = input;
    
          }
    
          counter+=1;
       }
    
    
       printf( "The biggest integer is %d\n", largest );
    
       printf( "The smallest integer is %d\n", smallest );
    
       return 0;
    
    }
    btw.. flag is.... counter?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 06:44 PM
  2. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 04:25 PM
  3. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 04:40 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 09:58 PM
  5. Replies: 16
    Last Post: 09-22-2004, 12:08 AM

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