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 exponent of each element

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
$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

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

Students also viewed these Databases questions