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).
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