- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
Give a RAM program to read $n$ positive integers followed by an endmarker ($0$) and then print the $n$ numbers in sorted order.
I have done the following:
So far I tried to store each positive number that is read in a different register. Is this correct?? (Wondering)
Then I have to order the numbers.
Do I have to store the integer of register 1 in the register n+1 and compare it with the integers of registers 2 till n?? If any of these numbers is greater that the number of register n+1, we store this number in register n+1.
Is my idea correct?? (Wondering)
Give a RAM program to read $n$ positive integers followed by an endmarker ($0$) and then print the $n$ numbers in sorted order.
I have done the following:
Code:
Read 1
LOAD 1
STORE 1
LOAD =2
STORE 2
while: JZERO endwhile
READ *2
LOAD *2
STORE *2
LOAD 2
ADD =1
STORE 2
endwhile: ...
So far I tried to store each positive number that is read in a different register. Is this correct?? (Wondering)
Then I have to order the numbers.
Do I have to store the integer of register 1 in the register n+1 and compare it with the integers of registers 2 till n?? If any of these numbers is greater that the number of register n+1, we store this number in register n+1.
Is my idea correct?? (Wondering)