Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please help! my code is failing the tests below: # Change your C code here ( if different ) #int main ( ) { #
please help! my code is failing the tests below: # Change your C code here if different
#int main
# B; th element A
# for int j ; j n; j
# n j; Current length of array B
# exp exponentAj j;
# appendB n exp;
#
# n;
#
#int exponentint x int y
# int exp x;
# for int j ; j y; j
# exp exp x;
#
# returnexp;
#
#void appendint B int n int exp
# Bn exp;
#
# Write comments explaining each line of your code, all the registers and memory used
# Enter your MIPS code here
# Main function
main:
addi $t $zero, # Initialize B to
sw $t$s # Store at the beginning of array B
addi $t $zero, # Initialize loop counter to
add $t $s $zero # Initialize the address of array B
loop:
bge $t $s endloop # If loop counter n exit loop
lw $t$s # Load Ai from memory
move $a $t # Argument : Ai
move $a $t # Argument : i
jal exponent # Call exponent function
sw $v$t # Store the result at the end of array B
addi $t $t # Increment loop counter
addi $t $t # Increment the address of array B
j loop # Continue loop
endloop:
addi $s $t # Increment the size of array B n
# End of main function
# Exponent function
exponent:
addi $t $zero, # Initialize exponent to
addi $t $zero, # Initialize loop counter to
exploop:
bge $t $a endexp # If loop counter i exit loop
mul $t $t $a # Multiply exponent by Ai
addi $t $t # Increment loop counter
j exploop # Continue loop
endexp:
move $v $t # Return the computed exponent
# End of exponent function
here are the instructions i was given: Given an array of at least one integer, write a program to create a new array with elements equal to the exponent of each element in the original array raised to the index, ie Bi Aii
For this, write two functions that will be called in main function independently.
exponent
inputs: element Ai and index i
task: returns the value of element raised to index Aii
append
inputs: base address of new array B B current size of B n and the new element Aii
task: add the new element at the end.
This function does not return any value void
Registers Variables
$s A
$s n
$s B
$s n
Addresses Contents
$s A
$s A
$sn An
Example Test: If the values of $s through $s are initialized in the simulator as: Use the button under the Registers display to initialize register values for $s $s $s and the button under the Memory display to initialize the A array elements.
Registers Data
$s
$s
$s
$s
Addresses Contents
The resultant registers will be:
Registers Data
$s
$s
The resultant array B is:
Addresses Contents
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