Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Write a Little Man program to accept an indefinite number of input values. The output
value will be the largest of the input values. You should use the value 0 as a flag to
indicate the end of input
This is what I have done so far.
INP
STA FIRST
BRZ ENDWH
BRA WHILE ENDWH HLT
ZERO
DAT
TEN
begin INP
BRZ end
STA input
SUB max
BRP nmax
BRA begin
nmax LDA input
STA max
BRA begin
end LDA max
OUT
HLT
input DAT
max DAT
Related
I need a bit of help to code how to divide a sum of numbers, divided by n numbers put in. For instance, the sum is 10, with 2 numbers, the answer should be 5. But if the sum is 14 and the total input (n numbers) are 3, it should write out 4, ignoring the 2 remainders, og for example, sum equals to 16 and total inputs equal to 3, the output should be 5, ignoring the 1 in remainder. But I don't know how to have these two in one code without interfering with each other.
This is my current code
START INP
BRZ SLUTT
ADD sum
STA sum
LDA antall
ADD en
STA antall
BRP START
SLUTT LDA sum
OUT
LDA linjeskift
OTC
OTC
OTC
LDA n
OTC
LDA likhetstegn
OTC
LDA antall
OUT
LDA linjeskift
OTC
OTC
OTC
LDA d
OTC
LDA likhetstegn
OTC
start LDA sum
BRZ slutt
SUB antall
STA sum
LDA resultat
ADD en
STA resultat
BRP start
slutt LDA resultat
OUT
HLT
sum DAT
resultat DAT 0
antall DAT 0
en DAT 1
a DAT 97
d DAT 100
n DAT 110
linjeskift DAT 13
likhetstegn DAT 61
The main problem is that your BRP instruction, in the division part of the code, comes too late. It is supposed to check whether the subtraction of antall from sum still was non-negative, but as your code continues first by loading the resultat into the accumulator, the detection of the negative result can no longer happen... BRP will find that the latest operation (the update of resultat) was non-negative.
In short, that second part of the code should perform a BRP right after SUB. In LMC this is a general rule for doing it right: always have a BRP immediately after a SUB.
Some other comments:
Once you make the above correction, the BRZ slutt isn't really necessary anymore, as surely the next SUB will then return a negative result and trigger the output anyway. (but see next point)
On the other hand, if the number of inputs (antall) is zero, we would be dividing by zero, which comes down to an infinite loop. This should be avoided by making an extra check.
I did not quite get why your code has the middle part, where you print characters using OTC. It seems unrelated to the goal of the challenge.
Labels should be unique. Although they work fine on Peter Higginson's simulator, some LMC simulators do not distinguish between uppercase and lowercase labels, so it is better practice to use really distinctive labels, irrespective of case.
LMC provides a way to reset the program counter, which does not clear the mailboxes to their initial values. So for instance, a reset would not set sum or resultat to zero. It is therefore good practice to start the program with the initialisation of such "variables", so that the program will still behave correctly when the user uses the reset feature.
The first part of the code has a BRP which really should always branch, so it would be more natural to use BRA. It also works with BRP, as antall will not get negative, but it seems more intuitive to use the unconditional jump instruction here.
Here is the updated code, with the above remarks taken into account:
init LDA zero # Initialise variables before starting
STA resultat
STA antall
STA sum
start INP
BRZ validate
ADD sum
STA sum
LDA antall
ADD en
STA antall
BRA start # use BRA instead of BRP
validate LDA antall # check against division by zero
BRZ slutt
divide LDA sum
SUB antall
BRP continue # Important: SUB followed by BRP!
slutt LDA resultat
OUT
zero HLT
continue STA sum
LDA resultat
ADD en
STA resultat
BRA divide
sum DAT
resultat DAT
antall DAT
en DAT 1
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc#v0.816/lmc.js"></script>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm new to MATLAB and coding, so any help is much appreciated. I wanted to make a program that calculates the sum of an arithmetic progressions of natural numbers where you input N as the total natural number of terms in the sequence and the common difference between the terms is 1 and calculating it using the while loop. I have no idea how to code this program so I will be much grateful if anyone could help me. Thanks in advance!
total number stands for N in your question. diff stands for difference of each term in arithmetic progression. start stands for initial value of given arithmetic progression.
total_number=5;
diff=3;
start=1;
sum=0;
i=1;
while i<=total_number
tmp=start+diff*(i-1);
sum=sum+tmp;
i=i+1;
end
I hope this answer helps.
I need help with making my program produce the correct output.
I am currently working with this code:
INP
STA NUMBER
SUB DIVISOR
BRP VERIFY
BRA CHECK
LOOP STA NUMBER
LDA RESULT
ADD ONE
STA RESULT
VERIFY LDA NUMBER
SUB DIVISOR
BRP LOOP
LDA RESULT
ODD LDA ONE
STA RESULT
EVEN LDA 60
STA RESULT
CHECK LDA RESULT
BRP ODD
BRZ EVEN
OUT
HLT
NUMBER DAT
DIVISOR DAT 2
ONE DAT 1
RESULT DAT 0
When I run the code above on little man computer simulator, it just loops and loops and prints no output. What I wanted to do is divide the inputted number into two and check if it has a remainder. If it has a remainder, then it is an odd number, else it is an even number. I know that the code has errors but I can't pinpoint where the problem lies, maybe you can help me with it. Thanks in advance!
The infinite loop occurs at BRP ODD. Be aware that BRP also branches when the accumulator is zero. So it is a "branch when not negative" instruction. And when the execution continues at ODD, it falls through to EVEN, which makes the code at ODD irrelevant. At EVEN the accumulator is loaded with zero, and so the BRP will branch again... infinitely.
There is also a missing check for 0: when the input is zero, you should not perform the subtraction at all.
Not a problem, but having a reference to mailbox 60 can be better replaced with a reference to a label, like ZERO.
The code includes logic that really isn't necessary:
You have included code to calculate the quotient, since the code adds ONE to the RESULT every time you subtract the DIVISOR from the NUMBER. However, that RESULT is finally overwritten with either ONE or zero (address 60), so that quotient was calculated for nothing. As you only want to output whether the input was odd or even, you should drop the quotient calculation from your code.
Also avoid code repetition. You currently perform the SUB at two different places. This should not be necessary as the logic should be the same in both instances.
Here is the code reduced to its basics:
#input: 11
INP
STA NUMBER
LOOP BRZ OUTPUT # remainder is zero! so output a zero
SUB DIVISOR
BRP LOOP
LDA ONE # when result is negative, input was odd
OUTPUT OUT
HLT
NUMBER DAT
DIVISOR DAT 2
ONE DAT 1
ZERO DAT 0
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc#v0.816/lmc.js"></script>
You can run the simulator in this snippet and then use the buttons to step through the code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This is the script:
n=input('Enter the number of rows: ')
PT=zeros(n);
row=1;
col=1;
while row~=n+1
for col=1:1:n
PT(row, col)=(factorial(row-1)/(factorial(col-1)*factorial(row-col)));
end
row=row+1;
col=1;
end
PT
When I run it, it says to enter the number of rows, so I enter '4'. Then it says
error: factorial: all N must be real non-negative integers
error: called from
factorial at line 40 column 5
hw6p2 at line 7 column 17
I don't understand what's wrong.
In the line for col=1:1:n, col can get up to the value n, even when row is still 1. Therefore when you call factorial(row-col), row can be 1 and col can be 2. This is where it can be negative.
Your factorial input should not be negative .....in the loop (row-col) is taking a negative value and error popped. Use abs to make it positive always.
n=input('Enter the number of rows: ')
PT=zeros(n);
row=1;
col=1;
while row~=n+1
for col=1:1:n
PT(row, col)=(factorial(row-1)/(factorial(col-1)*factorial(abs(row-col))));
end
row=row+1;
col=1;
end
PT
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So for my probability class, my professor asked the following question on a homework problem:
A fair coin is flipped 10,000 times. Let X correspond to the difference between the number of heads and the number of tails. Using MATLAB, compute the expected value of X.
This is what I wrote to answer the question:
N = 10000;
i =0;
r=1/2;
Q=nchoosek(N,(X+N)/2);
X=(1,N);
for i=-N:N
P=Q*r.^(X+N)/2*(1-r)^(N-(X+N)/2) % probability_mass_function
E=sum(abs(X).*P); % expected value
end
However, is there faster and quicker way to compute this expected value? Any help would be greatly appreciated. Thanks
You can put all tests results in single matrix in one line and then calculate X per test, then average over X:
clear
TAIL=0; HEAD=1;
NumTests=121;
NumRollsPerTest=10*1000;
AllTestsRolls= rand(NumTests,NumRollsPerTest)>0.5 ; %head when rand>0.5
XperTest=sum(AllTestsRolls==HEAD,2)-sum(AllTestsRolls==TAIL,2);%every row is test so calc per test
ExpectedX=sum(XperTest)/length(XperTest)