Thread: Comparing several values.

  1. #1
    Novice
    Join Date
    Jul 2009
    Posts
    568

    Comparing several values.

    Never thought I'd be asking a "homework question", but this qualifies as one, since it is from the chapter review section of the book I'm reading (C Plus Primer, 5th ed., for the curious).

    "Write a function that returns the largest of three integer arguments."

    For two values it'd be an easy return a > b ? a : b;

    For three (or more, I suppose), I came up with this. Could someone tell me if I'm over-engineering or give me a pointer to a better solution? I know that I sometimes tend to miss the obvious one.
    Code:
    int compare(int a, int b, int c) {
      int max = 0;
      int tmp[3] = {a, b, c};
      
      for (int i = 0; i < 3; i++) {
        if (tmp[i] > max) {
          max = tmp[i];
        }
      }
      
      return max;
    }
    Thanks for you time!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suspect that the author was expecting a solution using a series of if-else statements. As for your loop: it works correctly unless all of the arguments are negative. You should actually set max's value to be INT_MIN, or to be one of the arguments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by laserlight View Post
    I suspect that the author was expecting a solution using a series of if-else statements. As for your loop: it works correctly unless all of the arguments are negative. You should actually set max's value to be INT_MIN, or to be one of the arguments.
    Thanks for the spot! I hadn't thought of the all-negative case.

    I took a peek at the answers section and, true enough, if-else series was the expected solution, which I found a little disappointing.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    The negative case is handled easily enough by priming max to the first value in the array rather than assuming zero:

    Code:
    int compare(int a, int b, int c) {
      int tmp[3] = {a, b, c};
      int max = tmp[0];
    
      for (int i = 1; i < 3; i++) {
        if (tmp[i] > max) {
          max = tmp[i];
        }
      }
      
      return max;
    }
    It starts by assuming the first element in the array is the maximum, then going from there. Note how the iterator now starts from array index 1 now, as the 0'th has been catered for already by that stage.

    If it's a simple test, then you can either use if...else or a single-line ternary operator to do it:

    Code:
    return a > b ? (a > c ? a : c) : (b > c ? b : c);
    but that's not so readable as if...else
    I think you can put a signature here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM