Page 1 of 1

## 14 Replies - 1007 Views - Last Post: 23 August 2010 - 10:43 AM

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 20 August 2010 - 01:23 AM

Suppose there is a virtual computer that uses Registers A, B and C only.

You can use 4 instructions: ADD Subtract, multiply and divide.

Each instruction takes 2 operands and one result. e.g. A+A -> A ; adds A to itself stores/overwrites it in A or A + B -> C; adds A and B and overwrites the result in C

Note:

*

Division by 0 is not allowed.
*

Use or literals are not allowed e.g. invalid statement: A + 2 -> B
*

Move is not allowed e.g. A -> B
*

virtual computer throws away the Remainder from the Division.
*

Only 2 operands and a result instruction is allowed.

Sample question:

Given that A = 5 , B = 3 and C = 4

write Steps to put 17 in C

Answer: A * C -> A A - B -> C

The question -

Q - A and B contain integers, C contains either 0 or 1, write steps to move A to C if C == 1 or B to C if C == 0. So write steps to move A or B depending on the value of C.

Can you find an answer to the above question in 3 steps, or more , but 3 if you can?

Is This A Good Question/Topic? 0

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

Posted 20 August 2010 - 01:41 AM

So what you have got till now? and what's the problem you are facing in it?

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 20 August 2010 - 02:15 AM

AmitTheInfinity, on 20 August 2010 - 12:41 AM, said:

So what you have got till now? and what's the problem you are facing in it?

Hey This problem, this question, is the problem im facing, I dont know what steps I need. I have tried a method like

lets say we have this case:

A = 5 , B = 2 , C = 1

then this is what I have tried:

A + B -> A // A = 7

A * C -> C // C = 7

C - B -> C // C = 5

now for the case when C = 0:

so we have A = 5, B = 2, C = 0:

A + B -> A // A = 7

A * C -> C // C = 0

C - B -> C // C = -2 but it should be 2

So as you can see my solution does not work, if you can find something else that would be great, I sat a test today for employment and it had this question , I still cant figure it out.

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Posted 20 August 2010 - 03:30 AM

There's no C question here...in fact, there's no PROGRAMMING question here really, so I'll just move this to the Student Lounge

### #5 guahguahmonster

Reputation: 68
• Posts: 209
• Joined: 29-August 07

Posted 20 August 2010 - 04:16 AM

Uhm, am I missing something? Isn't this doable in only one instruction for both cases?

HINT 1:
Spoiler

HINT 2:
Spoiler

MY SOLUTION:
Spoiler

This post has been edited by guahguahmonster: 20 August 2010 - 04:17 AM

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 20 August 2010 - 11:26 AM

gaugamonster you're wrong, it can't be done in one instruction because if you read the question it says that we dont know what C is.

Therefore the correct answer that I have worked out now is:

A-B->A

A*C->C

C+B->C

### #7 guahguahmonster

Reputation: 68
• Posts: 209
• Joined: 29-August 07

Posted 20 August 2010 - 11:58 AM

Oh, how embarrassing! I seem to have misinterpreted the question horribly. I just knew it couldn't be that easy. I assumed they wanted separate instructions for both cases, but it's supposed to be a series of instructions that works for both. Let's take your answer to be correct, then. It's unfortunate that it produces a side effect, but I don't think there's a way to avoid that in 3 instructions.

Edited. Of course, if you have another instruction, you can easily reverse the side effect with an A + B -> A

This post has been edited by guahguahmonster: 20 August 2010 - 04:07 PM

### #8 mojo666

Reputation: 408
• Posts: 882
• Joined: 27-June 09

Posted 20 August 2010 - 01:42 PM

You have figured out that if C==1 then you want to multiply, and if C==0 then you want to add. Since you cant do any kind of if-statement, you will have to do both. Start by trying the simple cases.
1.C*x->C, C+B->C
2.C*A->C, C+x->C
Where x is a number we still need to find.

Option 2 does not easily suite us. By the time we reach the last step, C is either A or 0. We have to add B to satisfy the 0 case and B-A to satisfy the A case. Thus x is not easily defined in option 2.

Option 1, however, is extremely easy to define x. When c is 0, B is moved to C no matter how we define x. Half the problem is already solved. Now we need to define x such that when C is 1, B will be added to it and produce A. In other words, just solve the equation A = x+B for x. We get x=A-B.

A-B->A
A*C->C
C+B->C

EDIT: Somehow I missed that you already solved it.

This post has been edited by mojo666: 20 August 2010 - 01:45 PM

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 20 August 2010 - 03:45 PM

Hi guys thanks for the answers, but there is another question and I didn't get this one right either in the test,

Q - you have 4 registers A , B , C and D. Contents of A and B are integers, while C and D are unknown.

Write steps to move the greater of A or B to C.

So here is what i did:

A/B -> D
A-B -> C
C*D -> C
B+C ->C

but this only works for positive integers, when one or both A and B are negative then this solution doesn't work.

Can anyone suggest an answer for both positive and negative integers?

Thank you

### #10 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

Posted 21 August 2010 - 12:30 PM

What class is this for? I don't understand what number you are required to obtain in the first question. Are you allowed to multiple outside numbers against the variables?

Ex. Can you do A*2 +B/C+=X

Perhaps if I knew what this was being applied to I may have more insight on the subject.

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 21 August 2010 - 01:45 PM

jpconleyiv, on 21 August 2010 - 11:30 AM, said:

What class is this for? I don't understand what number you are required to obtain in the first question. Are you allowed to multiple outside numbers against the variables?

Ex. Can you do A*2 +B/C+=X

Perhaps if I knew what this was being applied to I may have more insight on the subject.

No it says in the notes that you are not allowed to use literals, but you can make it e.g. u can say A/A -> A so A = 1, then say A + A -> A so A will be 2.

This was for a job interview for a programmers role.

I am stuck on this final question.

For those who are wondering what the question is, it is based on the same virtual computer described in first post, but now you can use 4 register:

Q - you have 4 registers A , B , C and D. Contents of A and B are integers, while C and D are unknown.

Write steps to move the greater of A or B to C.

For those who are wondering what the question is, it is based on the same virtual computer described in first post, but now you can use 4 register:

Q - you have 4 registers A , B , C and D. Contents of A and B are integers, while C and D are unknown.

Write steps to move the greater of A or B to C.

Reputation: 0
• Posts: 8
• Joined: 11-September 07

Posted 22 August 2010 - 02:46 AM

[quote name='ayad001' date='21 August 2010 - 12:45 PM' timestamp='1282423525' post='1096930']

jpconleyiv, on 21 August 2010 - 11:30 AM, said:

What class is this for? I don't understand what number you are required to obtain in the first question. Are you allowed to multiple outside numbers against the variables?
For those who are wondering what the question is, it is based on the same virtual computer described in first post, but now you can use 4 register:

Q - you have 4 registers A , B , C and D. Contents of A and B are integers, while C and D are unknown.

Write steps to move the greater of A or B to C.

I'm assuming that A and B are > 0.

can you solve it now?

i do see that if i say -

A = 10
B = 4

A/B - > C // C = 2
C/C -> C // C = 1

A-B -> D // D = 6

C * D -> D // D = 6
D+ B -> D // D = 10

That is the way i can think of it although the problem is that, if you have A = 4 and B = 10 ,

A = 4
B = 10

A/B - > C // C = 0
C/C -> C // C = ? division by zero

A-B -> D // D = -6

C * D -> D // D = 0
D+ B -> D // D = 10

so there is a division by zero and so it doesnt work

any thoughts?

### #13 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

Posted 22 August 2010 - 07:12 PM

I have a feeling that either not all of what needs to be doen is clarified or this is one of those situation sthat are extremely easy yet appears to be extremely difficult. I also stand a great chance of not understanding what you want to do at all. However I have given this some thought and this is what I have came up with.

If A and B both contain integers and we do not know what the integers are than there is no way of saying that both A and B do not contain the same integer. Thus, you can you use one as temp place holder just like in programming when passing variables. Now, what I am unsure of is how you want to propose the instructions (Add,Subtract, Multiply, Divide). Do you have to show steps for each instruction?

Here is what I am thinking you can do based on if i'm understanding this at all and based on passing values.

```//C=1 then A passes value to C:

B=A           0=5

C=B           1=5

C=A           C(1)=A(5)  //I'm guessing that C's original value is now dismissed now that C equals 5.

//C=0 then B passes value to C:

A=B

C=A

C=B

```

I wouldn't assume that how you obtained the value whether it be by adding, dividing, etc would matter. I would think that only how you are getting the value to another holder would. I hope this helps you in some way, I could be very off here but I looked at what you want to do with rearranging values and compared it to programming. Also keep in mind that (depending on the language) you would assign these variables a value and setup an array.

In Java you could do this.
```int temp = numbers[5];

numbers[5] = numbers[6];

numbers[6]= temp;

```

Again I hope this helps you out, I'm not 100% sure as to what else you are looking for. Is this by chance for a logic design course??

### #14 guahguahmonster

Reputation: 68
• Posts: 209
• Joined: 29-August 07

Posted 22 August 2010 - 09:41 PM

jpconleyiv, on 22 August 2010 - 06:12 PM, said:

I looked at what you want to do with rearranging values and compared it to programming.

I think this topic is titled incorrectly... this isn't really a programming question at all; it's a logic and thinking question.

### #15 mojo666

Reputation: 408
• Posts: 882
• Joined: 27-June 09

Posted 23 August 2010 - 10:43 AM

ayad001, on 20 August 2010 - 12:23 AM, said:

virtual computer throws away the Remainder from the Division.

A/B->C
B/A->D
C+D->D
C/C->C

If A is greater than or equal to B, C is 1. If B is greater than A, C is 0. Now just apply your solution to the previous problem.