Thread: Largest and smallest of 3 inputs

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Largest and smallest of 3 inputs

    Hello,

    I am having a problem with this particular program. The user is prompted to insert 3 numbers (integers). The program should check which of the 3 is the largest and which is the smallest.

    At the moment, the program prints one line endlessly:

    Code:
        printf("Among the numbers you entered");
    Code:
    #include <stdio.h>
    
    int largest(int large);
    int smallest(int small);
    
    int main()
    {
            int  a, /*first input*/
                 b, /*second input*/
                 c, /*third input*/
             large, /*largest of a, b and c*/
             small; /*smallest of a, b and c*/
        
    	printf("Enter 3 integers with a space in between : ");
    	scanf("%d %d %d", &a, &b, &c);
    	
        /*if a is the largest*/
        if((a > b) && (a > c))
    	{
                 a = large;
                 largest(large);
           	      
             /*if a is the largest and b the smallest*/
             if(b < c)
             {		
                 b = small;
                 smallest(small);
             }
             
             /*if a is the largest and c the smallest*/
             else if(c < b)
             {	
    	     c = small;
                 smallest(small);
             }
    	}
    	
        /*if b is the largest*/
        else if((b > a) && (b > c))
    	{
                 b = large;
                 largest(large);
                  
             /*if b is the largest and a the smallest*/
             if(a < c)
             {  
      	     a = small;
                 smallest(small);
             }
             
             /*if b is the largest and c the smallest*/
             else if(c < a)
             {  
    	     c = small;
                 smallest(small);
             }
    	}
    	
        /*if c is the largest*/
        else if((c > a) && (c > b))
    	{
                 c = large;
                 largest(large);	
           		
             /*if c is the largest and a the smallest*/
             if(a < b)
             {
    	     a = small;
                 smallest(small);
             }
             
             /*if c is the largest and b the smallest*/
             else if(b < a)
             {
    	     b = small;
                 smallest(small);
             }
    	}
    	return 0;
    }
    int largest(int large)
    {
        printf("Among the numbers you entered");
        printf("\nthe largest was %d and the smallest was ", largest(large));
    }
    int smallest(int small)
    {
        printf("%d.", smallest(small));
    }
    Last edited by SilentPirate007; 03-27-2010 at 09:15 AM. Reason: fix copy-paste indentations

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your function doesn't do anything, except call itself. I would expect your largest function to take three inputs, and provide as output the largest of them. (It certainly shouldn't print anything, nor call itself.) (Ditto smallest, with the obvious change.)

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    the functions you declared are supposed to return an integer while they are actually returning nothing, it's a really simple program why are you using all those if..else statements? you could do it by running a simple loop
    Last edited by rob90; 03-27-2010 at 09:19 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    I have to use 2 functions largest() and smallest()....

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why are you re-calling the same function when printing the value?? Just print large or small, the argument you are passing to the function.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, two functions, largest and smallest. Just idea code, not run at all.

    Code:
    
      int biggest;
    
      biggest = largest(arrayName); //or three int variable names
    
    int largest(int arrayName[NumOfElements] or three int variables: num1, num2, num3) {
    
      int bigNumber = num[0]   or num0;
    
      if(num[1] or num1 > bigNumber)
        bigNumber = num[1] or num1;
      else if(num[2] or num2 > bigNumber)
        bigNumber = num[2] or num2;
      else if(num[3] or num3 > bigNumber)
        bigNumber = num[3] (or num3);
        
      return bigNumber;
    }
    Smallest() would be an almost exact copy of the above.

    Why wouldn't something simple like this, do the job?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Haven't we just been over this?
    You have 12 comparisons, it can be done with three. The array can be sorted using just three comparisons, and then you have the smallest and largest at either end of the array, or in your case, in a and c.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the largest and smallest of three integers
    By SilentPirate007 in forum C Programming
    Replies: 15
    Last Post: 03-21-2010, 01:25 AM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM