Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
I NEED MIPS CODE FOR THIS AS WELL AS THE C CODE.
LA is an unrecognized commandf in the mips we are using same with li

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Q. Give the synthesis of morphine to apomorphine with all steps

Answered: 1 week ago