Thread: largest and smallest number

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.*

  2. #2
    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

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