Answered step by step
Verified Expert Solution
Link Copied!

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 me with code, I have attached the prompt, but my code seems to keep looping and doesn't stop at the n amount of elements the user enter:
.data
# Prompt for n
li $v0,4
la $a0, prompt1
syscall
# Read n
li $v0,5
syscall
move $s0, $v0 # $s0 will store n
# Check if n is within range
li $t0,0
li $t1,11
blt $s0, $t0, exit_program
bgt $s0, $t1, exit_program
# Call fill_array_function
move $a0, $s0
jal fill_array_function
# Call find_median
move $a0, $s0
jal find_median
move $s1, $v0 # $s1 will store the median
# Print the median
li $v0,1
move $a0, $s1
syscall
# Exit program
j exit_program
fill_array_function:
# Loop to fill the array
li $t0,0 # i =0
la $t1, array # Address of array
fill_loop:
beq $t0, $a0, sort_array # Exit loop if i == n
# Prompt for element
li $v0,4
la $a0, prompt2
syscall
# Read element
li $v0,5
syscall
sw $v0,($t1) # Store element in array
addi $t0, $t0,1 # Increment i
addi $t1, $t1,4 # Move to next array element
j fill_loop
sort_array:
# Bubble sort
li $t0,1 # Assume sorted
la $t1, array
sort_outerloop:
li $t0,0 # Assume unsorted for this pass
li $t2,0 # i =0
sort_innerloop:
bge $t2, $s0, sort_done # Exit inner loop if i >= n
lw $t3,($t1) # Load a[i]
lw $t4,4($t1) # Load a[i+1]
ble $t3, $t4, no_swap # If a[i]= a[i+1], no swap needed
sw $t4,($t1) # Swap a[i] and a[i+1]
sw $t3,4($t1)
li $t0,1 # Set sorted flag to 1
no_swap:
addi $t1, $t1,4 # Move to next array element
addi $t2, $t2,1 # Increment i
j sort_innerloop
sort_done:
bne $t0,1, sort_outerloop # If sorted flag still 0, array is sorted
j find_median
find_median:
# Median calculation
li $t0,2 # 2/2=1(index of median)
la $t1, array
add $t1, $t1, $s0 # Point to last element
sll $t2, $s0,2 # Multiply n by 4(to get number of bytes)
sub $t1, $t1, $t2 # Point to middle element
lw $v0,($t1) # Load middle element as median
jr $ra
exit_program:
li $v0,10 # Exit syscall
syscall
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. How can we use information and communication to generate trust?

Answered: 1 week ago

Question

recognise typical interviewer errors and explain how to avoid them

Answered: 1 week ago

Question

identify and evaluate a range of recruitment and selection methods

Answered: 1 week ago

Question

understand the role of competencies and a competency framework

Answered: 1 week ago