Selection without decision branching

Sep 7, 2011 at 4:16am
In a basic c++ class, and our assignment is the write a program that does some calculations, then decides based on the results which item to choose. I'd have it done in a minute with some if/else stuff, but I can't use it.

I really have no idea what to use to make the selection.

An example of what I need to do. Calculate the weight of a load, then for example your weight is 90 pounds you use a wagon, 1000 pounds use a truck, 10 tons use a ship, etc. Any hints?
Sep 7, 2011 at 5:26am
Does your assignment say not to use ifs? You could do this with a map (or a simple array)..
Last edited on Sep 7, 2011 at 1:23pm
Sep 7, 2011 at 7:15am
Can use ternary operator?

Define p is a pointer, point to function. param is the weight. According to the param decide the process function.
p = (param<90)?function1:((param<1000)?function2:function3);
So sorry about my description.
Sep 7, 2011 at 7:23am
Now that's just cheating GMist... (I.e. I wish I'd though of that... ;)
Sep 7, 2011 at 9:56am
Another way to cheat:
1
2
3
4
5
6
7
8
#include <iostream>

int main(){
   int a = 1;
   bool c = true;
   c && (a = 2);
   std::cout << a;
}
(it makes use of short circuiting)
Sep 7, 2011 at 12:41pm
Thanks Mathhead200, but I really don't how to deal this problem without...

OK, in another way to think about it. Every high level language can't run before compile. But the machine language is hard to read, so I want to use the assembly language to explain the selection.

The first method, we compare two variables and through instructions JG, JZ, and so on, to control the program run in different branch. A simple code, can't not run, only for explain.
Goal: if x>0 then y = 1, else if x=0 then y = 0; else if x<0 then y = -1;
x db ?
y db ?
mov al, 0
cmp x, al
jg big
jz sav
mov al,0ffH
jmp short sav
big: mov al, 1
sav: mov y,al
Equal to the follow code:
if(x>0)
{
    y = 1;
}
else if(x == 0)
{
    y=0;
}
else
{
    y = -1; //0ffH
}
It's means...

The second method, we use the instruction xlat(Translate).
Goal: translate the decimal number 0-15 to the hexadecimal.
The base address + offset, access like the array.
tab db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
If we want to find the 11(decimal), we can:
mov ax, seg tab
mov ds,ax
mov bx,offset tab
mov al,11
xlat
Equal to the follow code:
char array[16] = ('0','1','2','3',...'A',...'F');
array[11] = A;
Also equal to:
if(x == 1)
{
    x = 1
}
...
else if(x == 11)
{
    x = A;
}
...
else if(x == 15)
{
    x = F;
}
It's means...

I still can't find a good way to solve the problem. Maybe you can use array. The element is address of the function.
Last edited on Sep 7, 2011 at 5:52pm
Sep 7, 2011 at 1:21pm
btw, it should be:
 
else if (x == 1)
Sep 7, 2011 at 6:01pm
Thanks for the attempt at help. I ended up figuring out I could strip the most significant figure off the result and use that for another calculation that solves the problem,
Sep 8, 2011 at 12:47am
c && (a = 2);
yes, but how can we know when to change it to true or false?
Sep 8, 2011 at 7:03am
Change what to true or false? c would be replaced with "mass < 90" or etc.
Last edited on Sep 8, 2011 at 7:03am
Topic archived. No new replies allowed.