So in the following code I wrote. I was able to get the maximum value output after the user input 8 random integers, with a cap of 32000 on positive side and -32000 on negative side, but I can't seem to get the mimnimum value. For the minimum value it keep printing 0
Code:-
ORG 100
Main, LOAD Count
STORE Count
Loop, Input
STORE Value
LOAD Count
ADD One
STORE Count
CheckMin, LOAD Value
SUBT Smallest
SKIPCOND 800
JUMP Continue
LOAD Value
STORE Smallest
CheckMax, LOAD Value
SUBT Largest
SKIPCOND 400
JUMP Continue
LOAD Value
STORE Largest
Continue, LOAD Count
SUBT Seven
SKIPCOND 800
JUMP Loop
Print, LOAD Smallest
Output
LOAD Largest
Output
HALT
Value, Dec 0
Count, Dec 0
Smallest, Dec 0
Largest, Dec 0
One, Dec 1
Seven, Dec 7
END
I tried to change the values of the "Decimals"(Dec) but it didn't work. Kept them at 0 except for the "Seven, Dec 7" which is the loop counter set to 8 for 8 user inputs. This is homework so any help is appreciated!
You have a few logic errors, and I'd like to deal with them one at a time.
You have an assumption that the initial smallest value is 0, therefore, the code will only capture values smaller than 0 as the smallest — if you input all positive numbers, then the smallest value will be the initial value given that variable, which is 0.
Further, if you input only negative numbers, the largest will also come out as 0, which is your initial assumption of the largest.
There are three possible fixes:
This approach will work for any number of inputs where the count of inputs is >= 1 but if you wanted to handle a variable inputs count that supported a count of value 0 (a list of zero elements, for example, is a reasonable mathematic construct) it won't work since it demands at least one input. A simple check on the count to skip the whole thing including first input would mitigate that.
This is precise, but requires a bit more logic within the loop, and, that logic involves the short-circuit disjunction operator, so a bit more complex. Alternately it can be done with an if-then-else-if.
Since these initializations are out of bounds of your expectations, the first input will always be captured as the smallest and also the largest. (Using the max value will also work, e.g. 32000, -32000.)
The other problem is that when you do the if-then for the min check, it will skip the max check under certain circumstances, so your code looks a bit like this:
This is a control flow logic error. Should be checking for smallest and a largest independently as separate, sequential statements, not as nested statements.