Thread: largest and smallest number

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Unhappy largest and smallest number

    This program is running good, the problem is that is suppose to print the smallest and largest, but sometimes it just prints the largest and sometimes it prints the smallest. I want both of them to be printed at the end. I want to do this program with if and if else... can one help me.

    Fell free to give me your opinions and what my mistakes are. Thank you Very Much

    Code:
    /*  Write a program that reads in four integers and determines and 
                prints the largest and the smallest integeers in the group.
    */
    #include <stdio.h>
    
    int main()
    {
    
    	int a,b,c,d,e;
    
    	printf( "Enter number for 'ONE':\n" );
    		scanf( "%d", &a );
    	printf( "Enter number for 'TWO':\n" );
    		scanf( "%d", &b );
    	printf( "Enter number for 'Three':\n" );
    		scanf( "%d", &c );
    	printf( "Enter number for 'Four':\n" );
    		scanf( "%d", &d );
    	printf( "Enter number for 'Five':\n" );
    		scanf( "%d", &e );
    
    	if (a > b && a > c && a > d && a > e)	
    	printf( "%d is the largest number\n", a);
    	else if(a < b && a < c && a < d && a < e)
    	printf( "%d is the smallest number\n", a);
    
    	else if(b > a && b > c && b > d && b > e)
    	printf( "%d is the largest number\n", b);
    	else if(b < a && b < c && b < d && b < e)
    	printf( "%d is the smallest number\n", b);
    
    	else if(c > a && c > b && c > d && c > e)
    	printf( "%d is the largest number\n", c);
    	else if(c < a && c < b && c < d && c < e)
    	printf( "%d is the smallest number\n", c);
    
    	else if(d > a && d > b && d > c && d > e)
    	printf( "%d is the largest number\n", d);	
    	else if(d < a && d < b && d < c && d < e)
    	printf( "%d is the smallest number\n", d);
    
    	else if(e > a && e > b && e > c && e > d)
    	printf( "%d is the largest number\n", e);
    	else if(e < a && e < b && e < c && e < d)
    	printf( "%d is the smallest number\n", e);
    
    	return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    In your program, Change the else if's to if's -

    if ( a largest conditions )
    else if ( a smallest conditions )

    if ( b largest conditions )
    else if ( b smallest conditions )

    etc


    EDIT - Also - "Write a program that reads in four integers [...]". You're reading in five integers
    Last edited by twomers; 10-03-2006 at 01:19 PM.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Use two additional vars for min and max. Set min and max to the first number you read in. Then, compare the min and max to the rest of the values.


    EDIT: And, by the way, search the forums. . . this has been asked quite a few times.

  4. #4
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Talking ok, i got it!

    Thank you twomers and Kennedy. As Twomers said what i had to do is change the code to this and it works:

    Code:
    #include <stdio.h>
    int main()
    {
    
    	int a,b,c,d,e;
    
    	printf( "Enter number for 'ONE':\n" );
    		scanf( "%d", &a );
    	printf( "Enter number for 'TWO':\n" );
    		scanf( "%d", &b );
    	printf( "Enter number for 'Three':\n" );
    		scanf( "%d", &c );
    	printf( "Enter number for 'Four':\n" );
    		scanf( "%d", &d );
    	printf( "Enter number for 'Five':\n" );
    		scanf( "%d", &e );
    
    	if (a > b && a > c && a > d && a > e)	
    	printf( "%d is the largest number\n", a);
    	else if(a < b && a < c && a < d && a < e)
    	printf( "%d is the smallest number\n", a);
    
    	if(b > a && b > c && b > d && b > e)
    	printf( "%d is the largest number\n", b);
    	else if(b < a && b < c && b < d && b < e)
    	printf( "%d is the smallest number\n", b);
    
    	if(c > a && c > b && c > d && c > e)
    	printf( "%d is the largest number\n", c);
    	else if(c < a && c < b && c < d && c < e)
    	printf( "%d is the smallest number\n", c);
    
    	if(d > a && d > b && d > c && d > e)
    	printf( "%d is the largest number\n", d);	
    	else if(d < a && d < b && d < c && d < e)
    	printf( "%d is the smallest number\n", d);
    
    	if(e > a && e > b && e > c && e > d)
    	printf( "%d is the largest number\n", e);
    	else if(e < a && e < b && e < c && e < d)
    	printf( "%d is the smallest number\n", e);
    
    	return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Just sort an array.
    Code:
    #include <stdio.h>
    
    void bubblesort ( int array[], int n )
    {
       int k, j, temp, flag;
       for (k = n - 1; k > 0; --k)
       {
         flag = 1;
         for (j = 0; k > j; ++j)
         {
            if (array[j] > array[j + 1])
            {
               temp = array[j];
               array[j] = array[j+1];
               array[j + 1] = temp;
               flag = 0;
            }
         }
         if (flag)  break;
       }
    }
    
    int main ( void )
    {
       int numbers[] = { 3, 45, 31, 13, };
       bubblesort(numbers, sizeof numbers  / sizeof numbers[0]);
       printf("largest number = %d, smallest number = %d\n", numbers[3], 
          numbers[0]);
       return 0;
    }
    $ ./a
    largest number = 45, smallest number = 3

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Here is a version which only uses if-else statements and 3 variables:

    Code:
    #include <stdio.h>
    
    int main(void) {
       int num, min, max;
       puts("Enter number:");
       scanf("%d",&num);
       min=max=num;
       puts("Enter number:");
       scanf("%d",&num);
       if(num<=min)
          min=num;
       else max=num;
       puts("Enter number:");
       scanf("%d",&num);
       if(num<=min)
          min=num;
       else max=num;
       puts("Enter number:");
       scanf("%d",&num);
       if(num<=min)
          min=num;
       else max=num;
       printf("The Largest is: %d. Smallest is: %d",max,min);
       return 0;
    }
    hope this helps...
    It is not who I am inside but what I do that defines me.

  7. #7
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    /*  Write a program that reads in four integers and determines and 
                prints the largest and the smallest integeers in the group.
    */
    #include <stdio.h>
    #include <limits.h>
    
    #define MAX_INTS 4
    
    int main()
    {
        int min = INT_MAX;
        int max = INT_MIN;
        int a[ MAX_INTS ];
        int i;
    
        for (i = 0; i < MAX_INTS; i++)
        {
            printf( "Enter number %d of %d : ", (i + 1), MAX_INTS);
            scanf( "%d", &a[i] );
    
            if ( a[i] < min) min = a[i];
            if ( a[i] > max) max = a[i];
        }
    
        printf("%d is the smallest number\n", min);
        printf("%d is the largest number\n", max);
    
        return 0;
    }

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This can also be done without an array.
    Code:
    /* ... */
       int value; /* instead of an array */
       /* ... */
       for ( i = 0; i < MAX_INTS; i++ )
       {
          /* prompt and user input into value */
          if ( value < min )
          {
             min = value;
          }
          if ( value > max )
          {
             max = value;
          }
       }
       printf("min = %d, max = %d\n", min, max);
    /* ... */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thumbs up Improving the min & max

    Thank you guys for your support, I have found that the program i have is not efficient and it has some bugs, because when i enter numbers equal numbers example: num1= 44, num2 =44, num3= 44, num4= 44. It wont print nothing.
    The best way to do it with out doing an array is how Dave_Sinkula posted. I have modified the code, but i have a problem it dosent print the minimun number. It just prints a crazy number but the maximum number works fine.

    can some one point out where is the error?

    Code:
    #include <stdio.h>
    int main()
    {
       
    	int i,MAX_INTS=4,value,min,max; /* instead of an array */
     
    	for ( i = 0; i < MAX_INTS; i++ )
       {
    	   printf("\nEnter a number: ");   /* prompt and user input into value */
    	   scanf("%d",&value);
    	   
          if ( value < min )
          {
             min = value;
          }
          if ( value > max )
          {
             max = value;
          }
       }
       printf("min = %d, max = %d\n", min, max);
       return 0;
     }
    Thank you guys
    Wise_ron
    One man's constant is another man's variable

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You're using min and max uninitialized. You should really learn to pass at least -Wall to the compiler to catch these things. The header file "limits.h" defines two constants, INT_MIN and INT_MAX, that are useful in situations such as this.

    Code:
     1 #include <stdio.h>
     2 #include <limits.h>
     3 
     4 int main()
     5 {
     6     int i,
     7         value,
     8         min = INT_MAX,
     9         max = INT_MIN;
    10 
    11     const int MAX_INTS = 4;
    12 
    13     for ( i = 0; i < MAX_INTS; i++ )
    14     {
    15         printf("\nEnter a number: ");   /* prompt and user input into value */
    16         scanf("%d",&value);
    17 
    18         if ( value < min )
    19         {
    20             min = value;
    21         }
    22         if ( value > max )
    23         {
    24             max = value;
    25         }
    26     }
    27     printf("min = %d, max = %d\n", min, max);
    28     return 0;
    29 }
    30 
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  11. #11
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    It works, Thanks

    Thanks zx-1 i got it! I learn something new about the #include <limits.h>

    I got it working and the code is really short compare to my first code. Thanks
    Wise_ron
    One man's constant is another man's variable

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All zx-1 did was mimic Skeane and put in pointless line numbers. If you actually read the replies people type to you it'll help you in the long run. Oh, and it saves us all time having to tell you again what you've already been told.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  3. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM
  4. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  5. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM