Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6, 7, 9, 11, 14, 15, 18, 19, 21]. Have your 'main' function (not the merge function) print out the final array using a comma as a delimiter.
Program
Program
#Data declartion
.data
arr1: .word 3 7 9 11 15
21
#Test array1
arr2: .word 1 4 6 14 18
19
#Test array2
arr3: .word 0 0 0 0 0 0 0 0 0 0 0 0
#Result array
#Main
.globl main
.text
main:
#Get address of arrays and size
la $a0,arr1
la $a1,arr2
li $a2,6
la $a3,arr3
#CAll merge function
jal merge
#Set size of new array
mul $a2,$a2,2
#Call print function to display array
jal print
#End of the program
li $v0,10
syscall
#Merge functionimplementation
merge:
#Set passed arguments as temporary
move $t0,$a0
move $t1,$a1
move $t2,$a2
move $t3,$a3
#Loop to combine 2 arrays into new array
mergeLoop:
beqz $t2,sort
#First array read and write into new array
lw $t4,($t0)
sw $t4,($t3)
addi $t3,$t3,4
#Second array read and write into new
array
lw $t4,($t1)
sw $t4,($t3)
#Increment array addresses
addi $t3,$t3,4
addi $t0,$t0,4
addi $t1,$t1,4
#Decrement loop index
addi $t2,$t2,-1
j mergeLoop
#Sort the merged array into ascending order
sort:
move $t3,$a2
mul $t3,$t3,2
move $t1,$t3
sortArray:
li
$t0,0
#Set counter for array
bound
loop1:
#for (i = 0; i < n-1; i++)
addi
$t0,$t0,1
#Increment t0 for j
value
bgt
$t0,$t3,end
#check array bound
reach
add
$t1,$t3,$zero
#If not set t1=a1=array
size
loop2:
#for (j = 0; j < n-i-1; j++)
bge
$t0,$t1,loop1
#Check each round
completed
subi
$t1,$t1,1
#Subtract t1 to reduce count to reach size of the array
0
sll $t4, $t1,
2
#Shift left to
2
subi $t6, $t4,
4
#subtact t4 with 4 and store into
t3
add
$t4,$t4,$a3
#Get address of the
array
add
$t6,$t6,$a3
#Get address of the
array
lw
$t5,0($t4)
#Load values into t5
lw
$t7,0($t6)
#Load values into t6
#It's loop to swap values small into forward and large
backward
swap:
bgt
$t5,$t7,loop2
sw
$t5,0($t6)
sw $t7,0($t4)
j loop2
#return from the sort to main
end:
jr $ra
#Function to print the array elements as coma separated
print:
#Check array reach n-1 of size
beq $a2,1,ret
#Get and display list data
lw
$a0,0($a3)
li $v0,1
syscall
#Display coma
li $a0,44
li $v0,11
syscall
#Get next index
addi
$a3,$a3,4
addi $a2,$a2,-1
j
print
ret:
#Display last element
lw
$a0,0($a3)
li $v0,1
syscall
#Return to main
jr $ra
Output
1,3,4,6,7,9,11,14,15,18,19,21
Write a mips program that defines two integer array that are pre-sorted and the same size...
*c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C. For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2] then C=[1,2,4,4,5,6,7,8,8,9,10].
Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...
==> Please In C Language <== Insert a value in a sorted array: Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion. For example, when k=12 , the...
Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this linear search algorithm returns the indices of the longest sorted subset of numbers in an array of integers of size n. The longest sorted subset of numbers must satisfy three conditions. First, the subset consists of unique numbers (no duplicate); second, all the numbers in this subset is divisible by m, the minimum size of the subset is two elements. In the main method...
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...
Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...
Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....
Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, create a random integer array of size arraySize (15 OR LESS) with elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear multiple times in this array, has two accessor methods: public int getArraySize() that will...
1. Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...
10 L 1 22 Given the function below, that divides the original array in two arrays of half size, develop the Java code that combines two sorted arrays or clearly explain how to implement the merge function. (3 points) nt middle values.Length/2; // divide array into two arrays of half size nt left- new int[middle]; or Cint i-e; imiddle; i+) t Leftri] - values[iJ: ntD right- new int[values. length-middle]: or Cint i-a; i<values.length-middle; i++) t rightli] -values [middle+i]; ort(left); //recursively...