Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLZ READ Must be in mips and ran through MARS ( The code i provided keaps looping asking the question how many numbers i want

PLZ READ Must be in mips and ran through MARS(The code i provided keaps looping asking the question how many numbers i want to enter(If my code does not meet the requirements plz add on to it or make a new one)
Requirements:
use 2 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 $a1,$0,$t5 # Passing to function by $a1
add $a2,$0,$t6 # Passing to function by $a2
.add $t0, $v1, $0
.add $t5,$0,$a1 # Passing to function by $a1
add $t6,$0,$a2 # Passing to function by $a2
.add $v1, $t7, $0 # returning the result to main
Look at photo provided plz uses Mips Main prints the madian
How many number you like to enter: 9
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
Enter number 6: 60
Enter number 7: 70
Enter number 8: 80
Enter number 9: 90
The median is: 50
How many number you like to enter: 6
Enter number 1: 11
Enter number 2: 15
Enter number 3: 16
Enter number 4: 18
Enter number 5: 212
Enter number 6: 220
The median is: 17
===============================
How many number you like to enter: 6
Enter number 1: 11
Enter number 2: 15
Enter number 3: 18
Enter number 5:23
Enter number 6: 220
Enter number 7: 221
The median is: 20
===============================
the middle is 20.5 in the last case but since we only deal with integer we get the integer part
which is 20.
PLZ READ
Requirements:
use 2 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 $a1,$0,$t5 # Passing to function by $a1
add $a2,$0,$t6 # Passing to function by $a2
.add $t0, $v1, $0
.add $t5,$0,$a1 # Passing to function by $a1
add $t6,$0,$a2 # Passing to function by $a2
.add $v1, $t7, $0 # returning the result to main
My Code:
.data
array: .space 100 # 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 $v0,4
la $a0, prompt
syscall
# Read the size of the array
li $v0,5
syscall
move $s0, $v0 # $s0 contains the size of the array
# Fill up the array in main
jal fillArray
# Pass the size to the findMedian function
move $a0, $s0
jal findMedian
# Print the result
li $v0,1
move $a0, $v0 # $v0 contains the median
syscall
# Exit program
li $v0,10
syscall
fillArray:
# $a0: size of the array
# $s0: base address of the array
# $t0: loop counter
la $s0, array # Load base address of the array
fill_loop:
# Prompt user for array elements
li $v0,4
la $a0, newline
syscall
li $v0,4
la $a0, prompt
syscall
li $v0,5
syscall
# Store the entered number in the array
sw $v0,0($s0)
addi $s0, $s0,4 # Move to the next array element
addi $t0, $t0,1 # Increment loop counter
blt $t0, $a0, fill_loop # Repeat until the array is filled
jr $ra # Return to main
findMedian:
# $a0: size of the array
# $s0: base address of the array
# $v0: median value
la $s0, array # Load base address of the array
move $t5, $a0 # Copy the size of the array to $t5
# Sort the array in ascending order (bubble sort)
sort_loop:
li $t1,1 # Set flag to 1 to start a new pass
# Compare and swap adjacent elements
sub $t5, $t5,1
move $t6, $t5 # Copy the current size of the array
element_loop:
lw $t2,0($s0) # Load current element
lw $t3,4($s0) # Load next element
bge $t2, $t3, no_swap # If current element is not greater than next element, do not swap
# Swap elements
sw $t2,4($s0)
sw $t3,0($s0)
no_swap:
addi $s0, $s0,4 # Move to the next array element
addi $t6, $t6,-1 # Decrement loop counter
bnez $t6, element_loop # Repeat until all elements are compared
bnez $t1, sort_loop # If a swap occurred in the pass, repeat the sorting
# Calculate the median
li $v0,0 # Initialize median value to 0
la $s0, array # Load base address of the array
li $t7,2 # Divide by 2(integer division)
divu $t7, $t7,2 # Divide by 2
mflo $t7 # Copy quotient to $t7
add $s0, $s0, $t7 # Move to the middle element of the sorted array
lw $v0,0($s0) # Load the median value
jr $ra # Return to main
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

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions