# General programming , please help

Page 1 of 1## 14 Replies - 997 Views - Last Post: 23 August 2010 - 10:43 AM

### #1

# General programming , please help

Posted 20 August 2010 - 01:23 AM

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?

##
**Replies To:** General programming , please help

### #2

## Re: General programming , please help

Posted 20 August 2010 - 01:41 AM

### #3

## Re: General programming , please help

Posted 20 August 2010 - 02:15 AM

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

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.

### #4

## Re: General programming , please help

Posted 20 August 2010 - 03:30 AM

### #5

## Re: General programming , please help

Posted 20 August 2010 - 04:16 AM

HINT 1:

HINT 2:

MY SOLUTION:

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

### #6

## Re: General programming , please help

Posted 20 August 2010 - 11:26 AM

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

A-B->A

A*C->C

C+B->C

### #7

## Re: General programming , please help

Posted 20 August 2010 - 11:58 AM

*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

## Re: General programming , please help

Posted 20 August 2010 - 01:42 PM

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

### #9

## Re: General programming , please help

Posted 20 August 2010 - 03:45 PM

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

## Re: General programming , please help

Posted 21 August 2010 - 12:30 PM

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.

### #11

## Re: General programming , please help

Posted 21 August 2010 - 01:45 PM

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

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.

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.

### #12

## Re: General programming , please help

Posted 22 August 2010 - 02:46 AM

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

**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.

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

## Re: General programming , please help

Posted 22 August 2010 - 07:12 PM

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

## Re: General programming , please help

Posted 22 August 2010 - 09:41 PM

### #15

## Re: General programming , please help

Posted 23 August 2010 - 10:43 AM