Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can anyone help m e with code, I have attached the prompt, but m y code seems t o keep looping and doesn't stop a
Can anyone help with code, I have attached the prompt, but code seems keep looping and doesn't stop the amount elements the user enter:
data
# Prompt for n
li $v
la $a prompt
syscall
# Read n
li $v
syscall
move $s $v # $s will store n
# Check if n is within range
li $t
li $t
blt $s $t exitprogram
bgt $s $t exitprogram
# Call fillarrayfunction
move $a $s
jal fillarrayfunction
# Call findmedian
move $a $s
jal findmedian
move $s $v # $s will store the median
# Print the median
li $v
move $a $s
syscall
# Exit program
j exitprogram
fillarrayfunction:
# Loop to fill the array
li $t # i
la $t array # Address of array
fillloop:
beq $t $a sortarray # Exit loop if i n
# Prompt for element
li $v
la $a prompt
syscall
# Read element
li $v
syscall
sw $v$t # Store element in array
addi $t $t # Increment i
addi $t $t # Move to next array element
j fillloop
sortarray:
# Bubble sort
li $t # Assume sorted
la $t array
sortouterloop:
li $t # Assume unsorted for this pass
li $t # i
sortinnerloop:
bge $t $s sortdone # Exit inner loop if i n
lw $t$t # Load ai
lw $t$t # Load ai
ble $t $t noswap # If ai ai no swap needed
sw $t$t # Swap ai and ai
sw $t$t
li $t # Set sorted flag to
noswap:
addi $t $t # Move to next array element
addi $t $t # Increment i
j sortinnerloop
sortdone:
bne $t sortouterloop # If sorted flag still array is sorted
j findmedian
findmedian:
# Median calculation
li $t # index of median
la $t array
add $t $t $s # Point to last element
sll $t $s # Multiply n by to get number of bytes
sub $t $t $t # Point to middle element
lw $v$t # Load middle element as median
jr $ra
exitprogram:
li $v # Exit syscall
syscall
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started