In line 1, if the register number #second_operand is not equal to zero, then set the ip to the value in register #first_operand. In instructions 7 and 8 (in the sample input, assuming instruction 299 is the 0th instruction) the values of registers 7-9 is (9; 3 999). This means that in line 1 above (which is executed when instruction #10 is decoded) the ip should be set to equal to the value in register 7 which is 9. Then on the next line when the ip is outputted its value is 10, not the correct 9.
Debug by printing out ip, ram[ip], and value of every register at the beginning of every iteration. Pause every x iterations, in case you have infinite loop.
Debug by printing out ip, ram[ip], and value of every register at the beginning of every iteration. Pause every x iterations, in case you have infinite loop.
I know where the problem is, I do not know what is causing it. From what I can see, this is happening:
Yes, I meant put comments in your code for those areas - it just make it easier to understand, and might have helped you see your problem:
1 2 3 4 5 6 7 8 9
case 0: // 0ds means goto the location in register d unless register s contains 0
case 2: // 2dn means set register d to n (between 0 and 9)
case 3: // 3dn means add n to register d
case 4: // 4dn means multiply register d by n
case 5: // 5ds means set register d to the value of register s
case 6: // 6ds means add the value of register s to register d
case 7: // 7ds means multiply register d by the value of register s
case 8: // 8da means set register d to the value in RAM whose address is in register a
case 9: // 9sa means set the value in RAM whose address is in register a to the value of register s
Did you see what I was saying about why your program isn't working as you expected?
The link works for me, if it doesn't for others then that is a good reason to put easy comments in. I just copy / pasted them from the web page.
You comment things like for loops //iterating through ram array and for things like initialising everything to zero. Might seem over kill & bad from some people's POV but it's easier for tired old baastards 8+) like me looking at your code at 1:00AM !! In industry it could be a maintenance programmer, who is likely to complain about no comments at all.
case 0: couts the ip which is 10, because these are probably all empty ram locations. Presumably the last op1 was a 9.
case 0: is the only one with a cout, except for line 76.
The other thing is, you are not doing anything with the results of the calculations (not even printing them) - shouldn't you be implementing an RPN stack to do the maths?
None of the other cases print anything. The only places in your program that prints anything is on lines 35 (case 0) and 76.
The cout on line 35 is happening thousands of times because of the nested for loops. There is nothing to stop looping when a 000 instruction is reached?
case 0 is for any goto instruction or empty ram. At this stage ip is 10 because you have processed 10 instructions, and ip1 is 9 because that is what it was set to last, should have been 7 though?
So, for now, put a cout statement in each case: so you can see what is going on.
Sorry , the stuff about RPN stack is all wrong - I read the problem properly just now.
So, you just need to print the number instructions executed - which will vary because of any goto's.
In the final program, perhaps you should not have a cout on line 35, because this will prints values thousands of times nested 3 levels of for loop. Line 76 should print the answer, which is what you intended.
I need to go to bed - hopefully I haven't been too much of a nuisance !!
In the final program, perhaps you should not have a cout on line 35, because this will prints values thousands of times nested 3 levels of for loop. Line 76 should print the answer, which is what you intended.
me wrote:
I have inserted a test at line 35
EDIT:
The cout on line 35 is happening thousands of times because of the nested for loops
Nested for loops?
There is only a while nested in a for loop, and a for nested in a for loop.
Hopefully this will shed some light on the subject. It goes through each instruction of the sample program, and give cometary as to what is happening.
0 299 reg#9=9
1 492 reg#9=18
2 495 reg#9=90
3 399 reg#9=99
4 492 reg#9=198
5 495 reg#9=990
6 399 reg#9=999
7 283 reg#8=3
8 279 reg#7=9
9 689 reg#8=(3+999)%1000=2
10 078 if(reg#8!=0) goto intruction pointed to in reg#7
at first iteration reg#8=2 and reg#7=9
so the ip should now be at instruction 9, but my program sets
it to 10 and thus the infinite loop
11 100 halt
1. How many empty lines are in the input between the number of cases and first case (aka program)? I think you should recount your getlines. Not related to OP issue.
2. "if(reg#8!=0) goto intruction pointed". Where do you go, if reg#8==0? This made you loop.
3. Will !(reg[op2]) be true, if reg[op2] != 0 ? No. This explains why the ip did not equal to value of register.
How many empty lines are in the input between the number of cases and first case (aka program)? I think you should recount your getlines. Not related to OP issue.
I was unclear as to how many lines there will be between the no. of test cases and the first test case.
2. "if(reg#8!=0) goto intruction pointed". Where do you go, if reg#8==0? This made you loop.
3. Will !(reg[op2]) be true, if reg[op2] != 0 ? No. This explains why the ip did not equal to value of register.
That seems to be the problem, let me quickly test it...