Marie simulator looping when not meant to after storing inputs - loops

I have written this very basic Marie code for multiplying two numbers, X and Y. I built it without the first 6 lines and would just assign X and Y decimals to test the program but realized I need to allow the user to input the numbers. Now when I step through this or run it it just asks for input, stores X, asks for input, stores Y and then goes back to asking for Input, ie. the input for X. And it does this infinitely..... what?
Multiply_Subroutine, Dec 0
Input
Store X
Input
Store Y
multiply, Dec 0
Load Y
Skipcond 800
Jump end
Load temp
Add X
Store temp
Load Y
Subt One
Store Y
Skipcond 400
Jump multiply
Load temp
Store X
Output X
end, Halt
X, Dec 0
temp, Dec 0
Y, Dec 0
Null, Dec 0
One, Dec 1

It is because your multiply subroutine line name/variable has the operand 0 when it is line 8, it needs to be Dec 8 for it to work :)

Related

How to fill 4 SPSS variables with the values 1 to 4 using both VECTOR and LOOP?

I want to fill 4 variables, X1 to X4 with the values 1 to 4 (case 1 getting value, case 2 getting value 2, etc). Now I manage to fill only X1, I don't know why the code doesn't continue to also fill X2, X3 and X4.
I tried this with a vector and loop, but it only fills the first variable (X1) with the correct values and then stops. So X2 to X4 stay empty.
DATA LIST LIST / X1 X2 X3 X4.
BEGIN DATA
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
END DATA.
VECTOR V=X1 to X4.
LOOP #i=1 to 4.
LOOP #j=1 to 4.
COMPUTE V(#i)=#j.
END CASE.
END LOOP.
END LOOP.
EXECUTE.
I may not understand exactly what you need (or what you need it for), but to get a variable to contain the digits 1,2,3,4 in consecutive lines you don't need a loop - try this:
compute x1=mod($casenum,4) + 4*(mod($casenum,4)=0).
You can of course loop through a few variables to perform the same calculation:
do repeat x=x1 to x4.
compute x=mod($casenum,4) + 4*(mod($casenum,4)=0).
end repeat.

Store vectors that result form for loop and then compute the average of all these vectors

I have a 4 by 4 matrix:
A=[rand(1) 2 -1 rand(1);
rand(1) 3 rand(1) 0;
rand(1) -5 -2 5;
9 0 0 rand(1)];
Now I would like to form a vector b to be the first column of the matrix A. So the vector b is
b=[rand(1)
rand(1)
rand(1)
9 ];
I would like to write a for loop that compute b many times say 100 then store these vectors in matrix C ( which now has size of 4*100) and then compute the mean of all columns of C. So far I wrote:
for j=1:100
A=[rand(1) 2 -1 rand(1);...
rand(1) 3 rand(1) 0;...
rand(1) -5 -2 5;...
9 0 0 rand(1)];
b=A(:,1)
end
Every time the loop executed, it produces a vector, say b_1 then b_2,....,b_100. How to store them in matrix C=[b_1 b_2 ... b_100] and then compute the mean of matrix C over all columns so that the mean will be a vector of size 4 by 1 the same size as b.
I don't have Matlab on this laptop but the little script should be like this:
for jj=1:100
C(:,jj)=[rand(1) ;...
rand(1) ;...
rand(1) ;...
9 ];
end
The matrix C will contain all the column-vectors b. To access to any of them just use b(:,x) where x is the index-number or column that you want to use.
For the average you can do something like this:
b_average=[mean(C(1,:)); mean(C(2,:)); mean(C(3,:));mean(C(4,:))];
Of course the last mean upon a vector with only 9 values hasn't meaning: I leave the code as it is just for completeness.
Remember as well that the average of a vector with random numbers will be really close to the value zero if N is big enough (where N is the number of the sample in the vector of course).
Anyway, the for loop is not the best way to do this. Try to use something like this:
C=[rand(1,100);rand(1,100);rand(1,100);9*ones(1,100)];
or better (as it was point out by Adriaan)
C=[rand(3,100);9*ones(1,100)];
This line does the same of the for loop. Again: try to don't use the variable j and iin Matlab because they are reserved.

Specifying LINEST output in EXCEL

Excel LINEST function returns an array of outputs (statistic) such as se1, se2, r2, ssresid, etc. I am interested in getting only the ssresid output in one cell. My idea is following:
=CHOOSE(10, LINEST(y,x,,TRUE))
however this does not work. Internet search does not help, but I believe the problem is with my manipulation of the LINEST output array. Could someone please explain what I am doing wrong in the equation, and possibly explain how it works with arrays in this kind of setting (array obtained by function)? Thank you.
Linest returns a matrix. The matrix is n wide x 5 high where n is the number of dependent variables. The format of the output is:
https://support.content.office.net/en-us/media/e0d97b28-95d9-4cb2-888c-78db54378381.gif
Ssresid is in the fifth row, second column. To isolate it, do some matrix math: Eg if n=3 enter the following matrix in cells X21:Z25 or (anywhere else you please.)
X Y Z
21 0 0 0
22 0 0 0
23 0 0 0
24 0 0 0
25 0 1 0
Then enter
{= sum(LINEST(y,x,,TRUE) * x21:z25) }
When you enter the formula be sure to press Ctrl shift enter. This tells excel to evaluate the formula as a matrix formula.

C: how to store integers by reading line by line with a random layout?

I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?
I was thinking of using fgets() but I can't find a way to do what I want.
The input looks like this:
0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3
These are x y positions.
I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?

How does `Skipcond` work in the MARIE assembly language?

I am trying to understand the MARIE assembly language. I don't quite understand skipcond for
doing things like <, or >, or multiply or divide.
I am taking this simple program:
x = 1
while x < 10 do
x = x +1
endwhile;
What I don't understand is how to use certain skip conditions:
Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0
Now, I know I would subtract x from 10 and test using skipcond.
I am not sure which one and why. I guess if I knew how they really work maybe it would be easier to understand. Why is it used to compare to zero?
This is what I have:
100 load one
101 store x
102 subt ten
103 skipcond400 if x-10 = 0? // or skpcond000 x -10 < 0??
while x < 10 do
x = x + 1
will jump out of the loop as soon as x equals 10. If you subtract 10 from x, you'll get a negative value until x equals 10 (and the value is 0). So using skpcond000 would be wrong as it would jump out too soon. So skpcond400 is correct.
Perhaps it is easier to understand if you change the C code so it will be closer to the assembly code:
Original: while (x < 10) do
Subtract 10: while ((x - 10) < 0) do
Use != instead of <: while ((x - 10) != 0) do
Also note that you have to increase x after the condition to reproduce identical behaviour to the while loop.
This may help. There are many ways to write this but I think this is the easiest way to understand what is happening in the loop. Note: usually variables are placed at the bottom of the program.
while x<10
x = x+1
Org 100
Load One / loads accumulator = 1 from a decimal constant
Store X / initialize the var x = 1
loop, Load X / loads x into the accumulator
Subt Ten / compares x to 10
Skipcond 000 / if ac < 0 i.e. if x < 10 run rest of loop body
JUMP Endloop / if ac => 10 terminate loop
Load X / begin the Loop
ADD One / add 1 to x
Store X / store new value in X
JUMP loop / continue loop
Endloop Halt / ends loop
One = DEC 1 Constant
Ten = DEC 10 Constant
X = 0 Variable

Resources