Say I have three numbers A, B and C. How would I check if B lies between A and C without assuming that A < C or A > C?
Printable View
Say I have three numbers A, B and C. How would I check if B lies between A and C without assuming that A < C or A > C?
How would you do this without a computer? How do you do it in your head?
lol that's what im trying to figure out....What I've thought of so far was thinking of it as a distance or absolute value....Or maybe even finding the median between both numbers and going from there. But I'm still not sure what I should do...
A big part of figuring out an algorithm is taking things that we do in our heads without really thinking about and breaking them up into smaller individual steps. Pretend you have a really dumb friend who has no idea how to figure out whether a number is in between two other numbers. Write out directions for him that he could follow to accomplish the task. When you have that written out, you should have an algorithm that should be pretty easily translated into code.
hint: compute the lower/upper bounds
hint: you said you can't assume that a < c or a > c, and you're correct. But you CAN figure it out, and then work form there.
Let's say;
What willCode :
int a=5; int b=4;
results?Code :
int c=a/b;
I took it to mean that A could be less than C or A could be greater than C. Either could be true so you would have to test both cases.