Question: Given an array of at least one integer, write a program to create a new array with elements equal to the power of each element

Given an array of at least one integer, write a program to create a new array with elements equal to the power of each element in the original array raised to the index, i.e., P[i]= A[i]^i.
For this, write two functions that will be called in main function independently.
power
inputs: element (A[i]) and index (i)
task: returns the value of element raised to index (A[i]^i).
newElement
inputs: base address of new array P (*P), current size of P (variable k) 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,*P; // Base addresses of A and P
int n, k; // Lengths of arrays A and B
int pow; // Return value from power function
// Task of main function
P[0]=1; //0th element = A[0]^0=1
for (int j =1; j < n; j++){
k = j; // Current length of array B
pow = power(A[j], j);
newElement(P, k, pow);
}
k++;
}
int power(int a, int b){
int pow = a;
for (int l =1; l < b; l++){
pow = pow * a;
}
return(pow);
}
void newElement(int* P, int k, int pow){
P[k]= pow;
}
Registers Variables
$s0 A
$s1 n
$s2 P
$s3 k
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 P is:
Addresses Contents
80001
80045
800825
8012-8
80160
Grading: Manual score (1 point) will be added after the deadline.
4 automated tests each worth 1 points. The registers $s2, $s3 and the array P elements in memory will be checked by the automated tests.
Comments specifying your choice of registers and each line of your code (0.5 points).
Correct use of Single Procedure Calls (0.5 points). If your code does not use procedure execution, you will lose all the points from the attests as well

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!