Determine the largest and smallest of three integers

This is a discussion on Determine the largest and smallest of three integers within the C Programming forums, part of the General Programming Boards category; I have just been introduced to a new chapter in C and I'm a bit confused/lost. I'm constructing a program ...

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

    Determine the largest and smallest of three integers

    I have just been introduced to a new chapter in C and I'm a bit confused/lost.

    I'm constructing a program that will prompt the user for three integers and then determine the largest and the smallest.

    Code:
    #include <stdio.h>
    
    void largest();//determines the largest integer entered of 3 inputs
    void smallest();//determines the smallest integer of 3 inputs
    
    int main(void)
    {
    	int input1, input2, input3;
    	
    	printf("Enter 3 integers with a space in between : ");
    	scanf("%d %d %d", &input1, &input2, &input3);
    	
    	if((input1 >= input2) && (input1 >= input3))//if the first input is the largest
    	{
                   largest();
           
               if(input2 <= input3)//if the first input is the largest and the second input the smallest
    	      smallest();
    	   
    	   else if(input3 <= input2)//if the first input is the largest and the third input the smallest
    	      smallest();
    	}
    	else if((input2 >= input1) && (input2 >= input3))//if the second input is the largest
    	{
                   largest();
           
               if(input1 <= input3)//if the second input is the largest and the first input the smallest
                  smallest();
    	   
    	   else if(input3 <= input1)//if the second input is the largest and the third input the smallest
                  smallest();
    	}
    	else if((input3 >= input1) && (input3 >= input2))//if the third input is the largest
    	{
                   largest();
           
               if(input1 <= input2)//if the third input is the largest and the first input the smallest
    	      smallest();
    	   
    	   else if(input2 <= input1)//if the third input is the largest and the second input the smallest
    	      smallest();
    	}
    	return 0;
    }
    void largest()
    {
       int large;
       
       printf("Among the numbers you entered\n");
       printf("the largest was %d and the smallest was ", large);//should be printed when the largest intger is determined
    }
    void smallest()
    {
       int small;
       
       printf("%d.", small);//should be printed when the smallest integer is printed
    }
    Last edited by SilentPirate007; 03-20-2010 at 06:31 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Forgot to mention, I suspect the problem area to be:

    Code:
    }
    void largest()
    {
       int large;
       
       printf("Among the numbers you entered\n");
       printf("the largest was %d and the smallest was ", large);//should be printed when the largest intger is determined
    }
    void smallest()
    {
       int small;
       
       printf("%d.", small);//should be printed when the smallest integer is printed
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Maybe you should pass some parameters to those functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I'm sure that you realise your functions largest() and smallest() won't actually print any (valid) values as your main function stands? If you want them to, try passing the value to be printed into them as a parameter.

    BTW, your main logic is horribly convoluted. Have a Google at how ternary operators work in C.

  5. #5
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    i will give you some steps:

    1) declare your variables outside any function (after the #include part)
    2) put the following algorithm (translate it to C) in the largest function:
    int biggest
    if input1 is bigger than input2 then
    biggest is equal to input1
    else
    biggest is equal to input2

    if biggest is lesser than input3 then
    biggest = input3

    display biggest
    3) put the following algorithm (translate it to C) in the smallest function:
    int smallest
    if input1 is bigger than input2 then
    smallest is equal to input1
    else
    smallest is equal to input2

    if smallest is lesser than input3 then
    smallest = input3

    display smallest
    4) end of all the steps

    i hope the above steps help.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I assume this is for education? If so, don't continue with this code, it'd probably get a horrible grade. Start over... Use UltraKing's advice.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    yes it is for school, I have to use functions..(I could have used an easier method, but the focus is design)

    I will only be graded on whether it works or not (not what it looks like)...

    The problem is I'm not completely sure how it works...hence the reason that I haven't passed the values to it's corresponding function

  8. #8
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    you may get a good grade if it only works. but, there is one thing which
    must not be skipped. every c programmer, should have the following basis
    (at a minimum):

    s\he must write clean code
    s\he must not use magic numbers (numbers used without reference)
    s\he must use comments

    and the list goes on. if you ask me, it never hurts to have a clean code
    than a spagheti-code (spagheti-code means a code which cant be
    understanded by others). its better safe than sorry.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    You can sort three items with just three "compare and swap" operations. Then the first and last item will be your smallest and largest.
    Do you think that would have been easier?
    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"

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    THIS WILL WORK
    #include<stdio.h>
    #include <conio.h>
    void largest(int);//determines the largest integer entered of 3 inputs
    void smallest(int);//determines the smallest integer of 3 inputs

    int main(void)
    {
    int input1, input2, input3;

    clrscr();
    printf("Enter 3 integers with a space in between : ");
    scanf("%d %d %d", &input1, &input2, &input3);

    if((input1 >= input2) && (input1 >= input3))//if the first input is the largest
    {
    largest( input1);

    if(input2 <= input3)//if the first input is the largest and the second input the smallest
    smallest( input2);

    else if(input3 <= input2)//if the first input is the largest and the third input the smallest
    smallest( input3);
    }
    else if((input2 >= input1) && (input2 >= input3))//if the second input is the largest
    {
    largest(input2);

    if(input1 <= input3)//if the second input is the largest and the first input the smallest
    smallest(input1);

    else if(input3 <= input1)//if the second input is the largest and the third input the smallest
    smallest(input3);
    }
    else if((input3 >= input1) && (input3 >= input2))//if the third input is the largest
    {
    largest(input3);

    if(input1 <= input2)//if the third input is the largest and the first input the smallest
    smallest(input1);

    else if(input2 <= input1)//if the third input is the largest and the second input the smallest
    smallest(input2);
    }
    return 0;
    }
    void largest(int a)
    {


    printf("Among the numbers you entered\n");
    printf("the largest was %d and the smallest was ", a);//should be printed when the largest intger is determined
    }
    void smallest( int small)
    {


    printf("%d.", small);//should be printed when the smallest integer is printed
    }

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > THIS WILL WORK
    This hasn't bothered to read the forum rules on how to post code with code tags
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    > THIS WILL WORK
    This hasn't bothered to read the forum rules on how to post code with code tags
    Salem:

    Don't be unfair, now! "This" said he WILL work, but never said he was working at that moment!



    SilentPirate:

    it's common in sorting / ordering programs to have a function JUST for swapping two elements or variables.

    Why not use UltraKing's good advice, but you can call your swap() function with just the addresses of the two variables to be swapped:

    Code:
    swap(&num1, &num2);
    
    void swap(int * num1, int *num2) {
      int *temp;
      //you know the drill probably: ask if you need a hint
    
    }
    That is a good use for a simple function, and quite standard, and elegant.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    I didn't even try to read the unreadable to work out whether it might work or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Quote Originally Posted by magestrium View Post
    THIS WILL WORK
    1. Don't do other's homework for them; read the forum rules
    2. That's a horrible solution
    3. Use code tags when posting code, or don't post at all
    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"

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by iMalc View Post
    1. Don't do other's homework for them; read the forum rules
    2. That's a horrible solution
    3. Use code tags when posting code, or don't post at all
    Wow, thank you for setting this staight iMalc. Wanted to say something about NOT HELPING others by feeding them the solution myself.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to determine and print smallest and largest values
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 09-28-2010, 07:05 AM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. Determine the largest and the smallest
    By JC33 in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2007, 04:51 AM
  4. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  5. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM

Tags for this Thread


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