IDNLearn.com is the perfect place to get detailed and accurate answers to your questions. Ask your questions and receive comprehensive and trustworthy answers from our experienced community of professionals.
Any help would be appreciated, tia! 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]ᶦ. 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]ᶦ). append inputs: base address of new array B (*B), current size of B (n2) and the new element (A[i]ᶦ) task: add the new element at the end. This function does not return any value (void). Following is a sample C code to perform the required task. You may modify the code for the functions, but the task performed should not be changed. int main() { // Variable Declaration int* A, B; // Base addresses of A and B int n1, n2; // Lengths of arrays A and B int exp; // Return value from exponent function // Task of main function 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; } 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 $s0 4000 $s1 5 $s2 8000 $s3 0 Addresses Contents 4000 10 4004 5 4008 -5 4012 -2 4016 0 The resultant registers will be: Registers Data $s2 8000 $s3 5 The resultant array B is: Addresses Contents 8000 1 8004 5 8008 25 8012 -8 8016 0 This is what I have, but I keep getting an erorr saying Line 26: Unrecognized Syntax near: .data # 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 .data A: .word 10, 5, -5, -2, 0 # Example array A n1: .word 5 # Length of array A B: .space 20 # Allocate space for array B (assuming 5 elements) n2: .word 0 # Initialize n2 to 0 .text .globl main main: la $s0, A # $s0 = base address of A lw $s1, n1 # $s1 = n1 (length of A) la $s2, B # $s2 = base address of B lw $s3, n2 # $s3 = n2 (initialize n2 to 0) # Initialize B[0] to 1 (A[0]^0) li $t0, 1 # $t0 = 1 sw $t0, 0($s2) # B[0] = 1 li $t1, 1 # $t1 = 1 (index j) loop: bge $t1, $s1, end # if j >= n1, exit loop # Calculate address of A[j] sll $t2, $t1, 2 # $t2 = j * 4 (byte offset) add $t3, $s0, $t2 # $t3 = &A[j] lw $a0, 0($t3) # $a0 = A[j] move $a1, $t1 # $a1 = j # Call exponent function jal exponent # Store result of exponent in B[j] sll $t4, $t1, 2 # $t4 = j * 4 (byte offset) add $t5, $s2, $t4 # $t5 = &B[j] sw $v0, 0($t5) # B[j] = result of exponent function # Increment n2 and j addi $s3, $s3, 1 # n2++ addi $t1, $t1, 1 # j++ j loop end: # Update n2 in memory sw $s3, n2 # Exit program li $v0, 10 # syscall for exit syscall # Exponent function: calculates x^y exponent: # Input: $a0 = A[j], $a1 = j # Output: $v0 = A[j]^j move $t3, $a0 # $t3 = A[j] li $v0, 1 # $v0 = 1 (initial result) exponent_loop: beqz $a1, exp_end # if j == 0, exit loop mul $v0, $v0, $t3 # $v0 *= A[j] addi $a1, $a1, -1 # j-- j exponent_loop exp_end: jr $ra # Return to caller
Sagot :
Thank you for using this platform to share and learn. Keep asking and answering. We appreciate every contribution you make. Find clear and concise answers at IDNLearn.com. Thanks for stopping by, and come back for more dependable solutions.