I'm working on a project for my Assembly Language class and I can't seem to figure out what I'm doing wrong and why my code is being outputted as reversed order and why I'm having the -1 answer displayed as an output.
Here are the instructions for the project:
This final version should read in all values and AFTER the -1 is
entered, it should display all of the numbers entered followed by
printing out the sum of these numbers. The stack must be used for this
program.
Here is my code so far:
START: READ X
PUSH
LOAD X
STACKW 0
LOAD Z
ADD 1
STORE Z
LOAD X
BRNEG OUT
BR SV
SV: LOAD Y
ADD X
STORE Y
BR WAIT
WAIT: NOOP
BR START
OUT: LOAD X
ADD 1
BRZERO EH
BRNEG SV
EH: STACKR 0
STORE W
WRITE W
POP
BR ENDOUTCOUNT
ENDOUTCOUNT: LOAD Z
SUB 1
STORE Z
LOAD Z
BRZERO END
BR EH
END: WRITE Y
NOOP
STOP
Z 0
Y 0
X 0
W 0
Here is my current output: I'd like to display the output numbers in ascending order and also hide the -1 as the output. All help would be appreciated.
Related
So I'm defining a variable/function that takes two inputs and displays a series of 1's and 0's
bin 0 0 = '0'
bin 0 1 = '1'
bin 0 2 = '1'
bin 0 3 = '0'
bin 0 4 = '1'
now I want to create a duplicate of the bin variable except that at 0 3 there should be a 1 so I tried to achieve this in a new function
changeBin w z = binNew where
binNew w z = '1'
binNew x y = bin x y
yet if I do it like this it gives me a pattern match redundant warning and when I call changeBin 0 3 it gets into a loop but when I change the function to
changeBin w z = binNew where
binNew 0 3 = '1'
binNew x y = bin x y
this works but I want to do it the first way so I can change it anytime without writing a whole function but I dont know why it gives me a redundant error when I write the same just with the numbers it works
I am new to haskell bear with me thanks
any help is appreciated on what my error is on the first function
1 changeBin w z = binNew where
2 binNew w z = '1'
3 binNew x y = bin x y
The w on line 1 and the w on line 2 are different variables. w on line 2 does not become a pattern that only matches when it has the same value as the w given on line one –– instead it defines a new variable that shadows the old one. You need to explicitly compare:
changeBin w z = binNew
where
binNew w' z'
| w' == w && z' == z = '1'
| otherwise = bin x y
Let's say I get a list, and I want to iterate on three at a time. I.e.: I have a list with [1,4,5,6,7,8,-9,2,0] In TCL, I can just use (for example):
foreach { x y z } $list {
puts "x is ${x}"
puts "y is ${y}"
puts "z is ${z}"
}
How can I define more than 1 variable, using the for loop with the in (array name) in Python 3.3? The Python org wiki showed just example of 1 iteration variable. This is also easily done in RubyThanks.
EDIT: The expected output is:
x is 1
y is 4
z is 5
x is 6
y is 7
z is 8
x is -9
y is 2
z is 0
You can split this array into chunks, and then just do something like this:
for x,y,z in chunked_array:
print("x=", x)
print("y=", y)
print("z=", z)
I'm not sure how for loops work in Python 3:
l=6
for z in range(l):
print(z)
Can I change the value of l by setting l=10 from within the loop?
Will the value of l will be changed to 10 or will remain 6?
If not, how can I manipulate the range from within the loop?
No you cannot manipulate the range from within the loop.
range(l) will be evaluated once to a list containing numbers from 0 to l-1, when the code execution reaches the line with the for statement:
range(l) => [0, 1, 2, 3, 4, 5]
Then the for loop will assign the values in the list to z in order.
If you need more fine grained control you'd have to use a while loop and keep a counter manually:
l = 6
z = 0
while z < l:
print(z)
z = z + 1
l = 10
This would allow you to check on every iteration for a stopping criterion.
My book presents a simple example which I'm a bit confused about:
It says, "consider the following program, and assume that the fine-grained atomic actions are reading and writing the variables:"
int y = 0, z = 0;
co x = y+z; // y=1; z=2; oc;
"If x = y + z is implemented by loading a register with y and then adding z to it, the final value of x can be 0,1,2, or 3. "
2? How does 2 work?
Note: co starts a concurrent process and // denote parallel-running statements
In your program there are two parallel sequences:
Sequence 1: x = y+z;
Sequence 2: y=1; z=2;
The operations of sequence 1 are:
y Copy the value of y into a register.
+ z Add the value of z to the value in the register.
x = Copy the value of the register into x.
The operations of sequence 2 are:
y=1; Set the value of y to 1.
z=2; Set the value of z to 2.
These two sequences are running at the same time, though the steps within a sequence must occur in order. Therefore, you can get an x value of '2' in the following sequence:
y=0
z=0
y Copy the value of y into a register. (register value is now '0')
y=1; Set the value of y to 1. (has no effect on the result, we've already copied y to the register)
z=2; Set the value of z to 2.
+ z Add the value of z to the value in the register. (register value is now '2')
x = Copy the value of the register into x. (the value of x is now '2')
Since they are assumed to run in parallel, I think an even simpler case could be y=0, z=2 when the assignment x = y + z occurs.
I am attempting to take the weighted average of a field, lets call it "X", which works fine so long as the field lets call it "Y" that the weighted average is with is not 0. However, when Y is 0 then the value for X does not appear in the report as it shouldn't, since it is an undefined value.
What I would like to do is have it so that if Y is 0, then X will be = to 0 and display 0 in the report.
I've have tried several things to get this to work, however they have not succeded. Currently what I am trying to do is set up a Display String Formating Formula to try and get it so that if Y is 0 then X is 0 and display this, however this requires a string to be the end result.
stringvar s;
NumberVar x := Weighted Average ( fieldX)
numbervar y := fieldY
if Y = 0 then x = 0;
X;
So my question is how do I convert a numberVar into a stringVar, or is there a better method to go about doing what I am hoping to accomplish?
It doesn't seem to me that you need to involve stringvars at all here. There are some syntax errors in that code. If you put the formula you have written with the following small changes it should display fine in the report
numbervar x := weightedaverage(...);
numbervar y := {fieldY};
if y = 0 then x:= 0;
x
However, an even easier way would be to forego using variables at all
if {fieldY} = 0 then 0 else weightedaverage(...)