Answered step by step
Verified Expert Solution
Link Copied!

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[0]=1; //0th element = A[0]^0=1
# for (int j =1; j n1; j++){
# n2= j; // Current length of array B
# exp = exponent(A[j], j);
# append(B, n2, exp);
# }
# n2++;
# }
#int exponent(int x, int y){
# int exp = x;
# for (int j =1; j y; j++){
# exp = exp * x;
# }
# return(exp);
#}
#void append(int* B, int n2, int exp){
# B[n2]= exp;
#}
# Write comments explaining each line of your code, all the registers and memory used
# Enter your MIPS code here
# Main function
main:
addi $t0, $zero, 1 # Initialize B[0] to 1
sw $t0,0($s2) # Store 1 at the beginning of array B
addi $t1, $zero, 1 # Initialize loop counter to 1
add $t3, $s2, $zero # Initialize the address of array B
loop:
bge $t1, $s1, end_loop # If loop counter >= n1, exit loop
lw $t2,0($s0) # Load A[i] from memory
move $a0, $t2 # Argument 1: A[i]
move $a1, $t1 # Argument 2: i
jal exponent # Call exponent function
sw $v0,0($t3) # Store the result at the end of array B
addi $t1, $t1,1 # Increment loop counter
addi $t3, $t3,4 # Increment the address of array B
j loop # Continue loop
end_loop:
addi $s3, $t1,1 # Increment the size of array B (n2)
# End of main function
# Exponent function
exponent:
addi $t3, $zero, 1 # Initialize exponent to 1
addi $t4, $zero, 1 # Initialize loop counter to 1
exp_loop:
bge $t4, $a1, end_exp # If loop counter >= i, exit loop
mul $t3, $t3, $a0 # Multiply exponent by A[i]
addi $t4, $t4,1 # Increment loop counter
j exp_loop # Continue loop
end_exp:
move $v0, $t3 # 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, i.e., B[i]= A[i]^i.
For this, write two functions that will be called in main function independently.
exponent
inputs: element (A[i]) and index (i)
task: returns the value of element raised to index (A[i]^i).
append
inputs: base address of new array B (*B), current size of B (n2) and the new element (A[i]^i)
task: add the new element at the end.
This function does not return any value (void).
Registers Variables
$s0 A
$s1 n1
$s2 B
$s3 n2
Addresses Contents
$s0 A[0]
$s0+4 A[1]
......
$s0+4*(n-1) A[n-1]
Example Test: If the values of $s1 through $s7 are initialized in the simulator as: (Use the '+' button under the Registers display to initialize register values for $s0, $s1, $s2 and the '+' button under the Memory display to initialize the A array elements.)
Registers Data
$s04000
$s15
$s28000
$s30
Addresses Contents
400010
40045
4008-5
4012-2
40160
The resultant registers will be:
Registers Data
$s28000
$s35
The resultant array B is:
Addresses Contents
80001
80045
800825
8012-8
80160
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions