Thread: Program to find largest of 3 numbers

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Program to find largest of 3 numbers

    Is it possible to do this with only 2 variables?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why don't you try to do it with only two variables, and see if you can?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    I've come to the conclusion that it's not possible as you are comparing three numbers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps the challenge was 2 comparisons (which is possible), not 2 variables (which isn't).
    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.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Apparently you can use arrays.

  6. #6
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Quote Originally Posted by stuartbaggs View Post
    Apparently you can use arrays.
    What about packed data? ....You could do it with one variable
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    Perhaps the challenge was 2 comparisons (which is possible), not 2 variables (which isn't).
    I could post a counter example - albeit one that makes rather specific assumptions about where the program obtains the three values from, and what it does with the maximum. But I'll allow people to enjoy the challenge.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Or, if you count the three input values in an array as one variable, the trivial solution uses exactly two variables and two comparisons.

    Edit: Since qny posted one below, I'll post this one too.
    Code:
    double maximum_of_three(const double *numbers)
    {
        double max = *numbers;
    
        if (*(++numbers) > max)
            max = *numbers;
    
        if (*(++numbers) > max)
            max = *numbers;
    
        return max;
    }
    and for any number of values using just three variables and (numbers - 1) comparisons (of numbers; (2*numbers - 1) if you count the loop condition comparison too):
    Code:
    double maximum(const double *numbers, size_t count)
    {
        double max = *numbers;
    
        while (count-- > 1)
            if (*(++numbers) > max)
                max = *numbers;
    
        return max;
    }
    Last edited by Nominal Animal; 10-10-2012 at 08:57 AM.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by grumpy View Post
    I could post a counter example
    Allow me

    Code:
    input A                              // 1st number
    input B                              // 2nd number
    if A < B let A = B
    input B                              // 3rd number
    if A < B print largest is B
        else print largest is A

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by qny View Post
    Allow me
    Indeed
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find largest numbers
    By krakatao in forum C Programming
    Replies: 1
    Last Post: 02-04-2012, 09:58 AM
  2. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  3. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM