Results 1 to 13 of 13

Math Help - Choosing two lowest values in set of three

  1. #1
    Newbie
    Joined
    Sep 2009
    Posts
    7

    Choosing two lowest values in set of three

    I am taking a computer science course and have to write a short program that accepts the input of three integers and outputs the two lowest values. The logic of doing this, according to my professor, is pretty straightforward, but I am having trouble wrapping my head around it.

    For example, mathematically, how would I determine the two lowest values of 10, -10, 2? FYI, In doing this, I can only determine which numbers are higher or lower if the results of adding or subtracting them are non-negative or negative. Or I have to turn the number into an equation, something like (a<b)...that would become (a-b<b-b)....that would become (a-b<0). That's fine for two numbers, but for three I'm being thrown off. It also seems to matter if the first or second number is negative, but I'm not sure why.

    I am not looking for the answer; I want to learn on my own. But if someone can point me in the right direction on the way to approach this problem, I would appreciate it.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Aug 2009
    From
    Israel
    Posts
    976
    As I said in the first topic, you can do this with two "<" tests. For example, if you have integers a,b,c and you know a<b, then what can you say about a?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Sep 2009
    Posts
    7
    Sorry, I moved that topic here. I figured it was more appropriate for a logic forum.

    If I know a<b, then I know that a is one of the lowest two digits in the set of three.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Aug 2009
    From
    Israel
    Posts
    976
    Good. Can you finish it from here?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Sep 2009
    Posts
    7
    Then I would do the same with b and c and whichever is the lowest there, I'd output a and that number. My problem is putting this into a set of instructions a computer can process.

    For instance, I input a, b and c (10, -9 and 4);
    I subtract b from a;
    if the result is negative I output a;
    if the result is non-negative I output b;

    Then I subtract c from b;
    if the result is negative I output b;
    if the result is non-negative I output c;

    The program outputs the two lowest numbers, but it outputs the b-integer twice, basically saying y is less than both a and c. I might be straying to far from a pure math question, but somehow this formula isn't just outputting two digits.

    I think a possible solution might be to compare the results of a-b and b-c, for instance, subtracting (b-c) from (a-b).

    I'm at work and have to go out for the rest of the day. I appreciate your help. I'll keep working on this and hopefully won't need your help again. If I do, I'll be back tomorrow morning.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Junior Member
    Joined
    Jan 2009
    Posts
    32
    take a look here Select 2 min from 3

    The idea is quite simple. Use reduced bubble sort algorithm and than just print 2 lowest values.
    It takes 2 comparisons and 2 swaps (in worst cast).
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Sep 2009
    Posts
    7
    Quote Originally Posted by ialbrekht View Post
    take a look here Select 2 min from 3

    The idea is quite simple. Use reduced bubble sort algorithm and than just print 2 lowest values.
    It takes 2 comparisons and 2 swaps (in worst cast).
    get2min3 = function(a, b, c)
    {
    if(a > b) {
    t=b; b=a; a=t;
    }
    if(b < c)
    return [a, b];
    else
    return [a, c];
    }

    var mins = get2min3(13, 10, 12);
    print(mins);

    would this work? assuming the inputs are a=13, b=10 and c=12, it seems to say if 13>10 and 10<12, output 13 and 10, which would be wrong since it's not the two lowest digits or 13 and 12, which would also be wrong. maybe i'm reading it wrong?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Newbie
    Joined
    Sep 2009
    Posts
    7
    well, i executed that program and it does work, i just don't understand how.

    EDIT: ialbrekht, my computer science program is teaching java and i'm a total newb. but in this course, we're learning to write programs in assembly language. so i have to solve this problem using assembly language, meaning that when i add or subtract one number from another, the result (whether the result is less than zero or non-negative and negative) will determine the next step. without the shortcut of using sort algorithms or even the < or > signs, how could i approach this problem? i can only use addition, subtraction, multiplication, division...and based on those results i can jump locations to perform another set of calculations.

    thanks in advance for your help.
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Junior Member
    Joined
    Jan 2009
    Posts
    32
    Here is some points how to do this:

    So, using this links you can select max and min value without <, > and jumps.

    The basic idea of algorithm is:
    INPUT: a, b, c
    OUTPUT: m1, m2

    m1 = min(a,b)
    m2 = max(a,b)
    m2 = min(m2,c)

    PS. Maybe you can optimize it by merging first 2 operations
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Newbie
    Joined
    Sep 2009
    Posts
    7
    ialbrekht, your help is greatly appreciated. but even what you wrote is still too sophisticated. i only have certain operations available to me (+, -, *, /, jump if negative, jump if not negative, jump and halt to stop the program). that's it.

    so if i input x=8, y=-10 and z=9, i'd have to write a program that would output 8 and -10 using only basic arithmetic and the limited conditional programming provided by the jumps. these aren't hard numbers and they're entered at random so it could be any three integers, negative or non-negative.

    after the integers are entered, i'd start by

    load x
    subtract y
    jump if not negative to NEW_STEP (if the result is 0 or higher, i know y is smaller than or equal to x, so i will output y in the next step)
    output y
    NEW_STEP output x
    load y
    subtract z
    jump if not negative to NEW_STEP2
    output z
    halt program
    NEW_STEP2 output y
    halt program

    seems simple enough, but i'm missing an arithmetic step somewhere because running the program will sometimes output the two lowest digits and sometimes it won't (it changes depending on the order of the numbers and/or if a negative digit is input first, second or third).

    i'm sure the solution is so simple, i'll kick myself when i find it, but right now it's driving me crazy.
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Junior Member
    Joined
    Jan 2009
    Posts
    32
    After you compared x and y you have to compare z with the x if x<y and with y if x>y.
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Newbie
    Joined
    Sep 2009
    Posts
    6
    My program is slightly different, in that it outputs all three numbers. If you take a look at my nested "if statements", you can see how the program is trying to sort out the numbers. I don't know anything about assembly language, but since you know java, you probably understand this:

    Code:
    public class SortThree{
        public static void main (String[] args){
    
    //declares the variables
            String num1Str, num2Str, num3Str;
            int num1, num2, num3;
    
    //inputs the three numbers as strings and parses them to integers
            num1Str = JOptionPane.showInputDialog("Enter first integer:");
            num1 = Integer.parseInt(num1Str);
            num2Str = JOptionPane.showInputDialog("Enter second integer:");
            num2 = Integer.parseInt(num2Str);
            num3Str = JOptionPane.showInputDialog("Enter third integer:");
            num3 = Integer.parseInt(num3Str);
    
    //determines the order to print the numbers
            if ( num1 >= num2 ){
                if ( num2 >= num3 )
                    System.out.println ( num1 + ", " + num2 + ", " + num3 );
                else{
                    if ( num1 >= num3 )
                        System.out.println ( num1 + ", " + num3 + ", " + num2 );
                    else
                        System.out.println ( num3 + ", " + num1 + ", " + num2 );
                }
            }
            else{
                if ( num2 >= num3 ){
                    if ( num1 >= num3 )
                        System.out.println ( num2 + ", " + num1 + ", " + num3 );
                    else
                        System.out.println ( num2 + ", " + num3 + ", " + num1 );
                }
                else
                    System.out.println ( num3 + ", " + num2 + ", " + num1 );
            }
        }
    }
    There may be an easier way to work this out than what I did here, but I am also new to it, so I went with the diagram that I drew first.
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Newbie
    Joined
    Sep 2009
    Posts
    7
    Quote Originally Posted by ialbrekht View Post
    After you compared x and y you have to compare z with the x if x<y and with y if x>y.
    thanks for your help. this made it clearer. writing the actual program is still a challenge, but that's my cross to bear. i appreciate your time.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: October 2nd 2010, 03:09 PM
  2. lowest common multiple
    Posted in the Algebra Forum
    Replies: 7
    Last Post: June 27th 2010, 08:23 AM
  3. choosing lowest common denominator
    Posted in the Algebra Forum
    Replies: 3
    Last Post: April 15th 2009, 03:15 PM
  4. Lowest terms
    Posted in the Algebra Forum
    Replies: 7
    Last Post: April 8th 2009, 12:16 AM
  5. lowest terms
    Posted in the Algebra Forum
    Replies: 1
    Last Post: March 8th 2007, 05:03 PM

Search Tags


/mathhelpforum @mathhelpforum