Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By qny

Program to find largest of 3 numbers

This is a discussion on Program to find largest of 3 numbers within the C Programming forums, part of the General Programming Boards category; Is it possible to do this with only 2 variables? Thanks...

  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,264
    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%.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    121
    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,264
    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.
    qny likes this.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    838
    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    Salem likes this.

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

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, 08: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, 10:21 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM

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