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?
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.
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.
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?
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.
Here is some points how to do this:
- Mathematically Find Max Value without Conditional Comparison - Stack Overflow
- Lair Of The Multimedia Guru Optimization
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
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.
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:
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.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 ); } } }