Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLZ READ ( also the code that i provided keaps looping on how many numbers youd like to enter ) If requirments do not reflect
PLZ READalso the code that i provided keaps looping on how many numbers youd like to enter If requirments do not reflect my code pls add to it or write a new one
Requirements:
use functions
paremeter to first function should be the size of the array, than you fill up the array
in main, pass size to find median function
Use passing and returning
must use this lines of code
add $a$$t # Passing to function by $a
add $a$$t # Passing to function by $a
add $t $v $
add $t$$a # Passing to function by $a
add $t$$a # Passing to function by $a
add $v $t $ # returning the result to main
Look at photo provided plz uses Mips Main prints the madian
How many number you like to enter:
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
The median is:
How many number you like to enter:
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
The median is:
How many number you like to enter:
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
Enter number :
The median is:
the middle is in the last case but since we only deal with integer we get the integer part
which is
PLZ READ
Requirements:
use functions
paremeter to first function should be the size of the array, than you fill up the array
in main, pass size to find median function
Use passing and returning
must use this lines of code
add $a$$t # Passing to function by $a
add $a$$t # Passing to function by $a
add $t $v $
add $t$$a # Passing to function by $a
add $t$$a # Passing to function by $a
add $v $t $ # returning the result to main
Code that i have:
data
array: space # Assuming a maximum size for the array
newline: asciiz
prompt: asciiz "How many numbers you'd like to enter:
text
main:
# Prompt user for the size of the array
li $v
la $a prompt
syscall
# Read the size of the array
li $v
syscall
move $s $v # $s contains the size of the array
# Fill up the array in main
jal fillArray
# Pass the size to the findMedian function
move $a $s
jal findMedian
# Print the result
li $v
move $a $v # $v contains the median
syscall
# Exit program
li $v
syscall
fillArray:
# $a: size of the array
# $s: base address of the array
# $t: loop counter
la $s array # Load base address of the array
fillloop:
# Prompt user for array elements
li $v
la $a newline
syscall
li $v
la $a prompt
syscall
li $v
syscall
# Store the entered number in the array
sw $v$s
addi $s $s # Move to the next array element
addi $t $t # Increment loop counter
blt $t $a fillloop # Repeat until the array is filled
jr $ra # Return to main
findMedian:
# $a: size of the array
# $s: base address of the array
# $v: median value
la $s array # Load base address of the array
move $t $a # Copy the size of the array to $t
# Sort the array in ascending order bubble sort
sortloop:
li $t # Set flag to to start a new pass
# Compare and swap adjacent elements
sub $t $t
move $t $t # Copy the current size of the array
elementloop:
lw $t$s # Load current element
lw $t$s # Load next element
bge $t $t noswap # If current element is not greater than next element, do not swap
# Swap elements
sw $t$s
sw $t$s
noswap:
addi $s $s # Move to the next array element
addi $t $t # Decrement loop counter
bnez $t elementloop # Repeat until all elements are compared
bnez $t sortloop # If a swap occurred in the pass, repeat the sorting
# Calculate the median
li $v # Initialize median value to
la $s array # Load base address of the array
li $t # Divide by integer division
divu $t $t # Divide by
mflo $t # Copy quotient to $t
add $s $s $t # Move to the middle element of the sorted array
lw $v$s # Load the median value
jr $ra # Return to main
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