Question

In: Computer Science

QUESTION 15 We need to write a function that calculates the power of x to n...

QUESTION 15

  1. We need to write a function that calculates the power of x to n e.g. power(5,3)=125.

    Which of the following is a right way to define power function?

    ______________________________________________________________

    int power(int a, int b)

    {

    int i=0, p=1;

    for(i=b;i>=1;i--)

      p*=a;

    return p;

    }

    ______________________________________________________________

    int power(int a, int b)

    {   

      int i=0, p=0;   

      for(i=1;i<=b;i--)       

        p=p*a;   

      return p;

    }

    ______________________________________________________________

    int power(int a, int b)

    {   

      int i=0, p=1;   

      for(i=b;i>=1;i--)       

        a*=p;   

      return p;

    }

    ______________________________________________________________

    int power(int a, int b)

    {   

      int i=0, p=1;   

      for(i=a;i>=1;i--)       

        p=p*b;   

      return p;

    }

    ______________________________________________________________

Solutions

Expert Solution

Answer:

The right function that calculates power is--(first function)

int power(int a,int b)
{
int i=0, p=1;
for(i=b;i>=1;i--)
p*=a;
return p;
}

Explanation: Firstly i is initialized to 0 and p to 1.In the for loop i reinitialized to b and until it greater than or equal to 1 and decreases one and The power value which is p=p*a is stored in p variable and finally it returns power value.

Example: power(5,3) Here a=5 and b=3

When the for loop i=3,p=p*a=1*5=5

i=2,p=p*a=5*5=25

i=1,p=p*a=25*5=125

And i=0 and loop exists and returns p which is 125.

The remaining three functions are wrong.

The second function which is--

int power(int a,int b)
{
int i=0, p=0;
for(i=1;i<=b;i--)
p=p*a;
return p;
}

It is wrong every time multiply p which is 0 with a always gives 0 and 0 is returned So,It is WRONG.

The third function which is--

int power(int a,int b)
{
int i=0, p=1;
for(i=b;i>=1;i--)
a*=p;
return p;
}

It is wrong because calculating power value into a variable and returning p variable.

The fourth function which is--

int power(int a,int b)
{
int i=0, p=1;
for(i=a;i>=1;i--)
p=p*b;
return p;
}

It is wrong because we are taking base value as b to calculate power but are taking a.

Hope you understand and like the answer..

Thank you..


Related Solutions

Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns...
Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function. Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0] ]...
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float)...
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float) and pow (int) > = 1, and it returns the result root raised to power pow. The recursion of the power function is defined as: Base case: rootpow = root, if pow = 1 Recursive case: rootpow = root* rootpow-1 , otherwise. b) Develop a greatestC( ) function that determines the greatest common divisor of two integer numbers. Test greatestC() in a simple main...
We define the Liouville function λ(n) by setting λ(1)=1. If n>1, we consider the prime power...
We define the Liouville function λ(n) by setting λ(1)=1. If n>1, we consider the prime power factorization n=p_1^(a_1 ) p_2^(a_2 )…p_m^(a_m ) and define λ(n)=(-1)^(a_1+a_2+⋯+a_m ) Prove that the summatory function of λ,Λ(n)=∑_d|n┤▒〖λ(d)〗 is multiplicative.
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
Write a function matrix_power(A, n) that computes the power An using Boolean arithmetic and returns the...
Write a function matrix_power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function. Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0] ] Then...
The coding must be formatted in Python. Write a function matrix_power(A, n) that computes the power...
The coding must be formatted in Python. Write a function matrix_power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function. Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0,...
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
The N-QUEENS PROBLEM Given a chess board having N x N cells, we need to place...
The N-QUEENS PROBLEM Given a chess board having N x N cells, we need to place N queens in such a way that no queen is attacked by any other queen. A queen can only attack horizontally, vertically and diagonally. Let’s go at this one step at a time. let’s place the first Queen at some cell, (I, j) and now the number of unattackable cells are reduced. And now, the number of the Queens to be placed are N...
In this question we show that we can use φ(n)/2. Let n = pq. Let x...
In this question we show that we can use φ(n)/2. Let n = pq. Let x be a number so that gcd(x, n) = 1. 1. show that xφ(n)/2 = 1 mod p and xφ(n)/2 = 1 mod q 2. Show that this implies that and xφ(n)/2 = 1 mod n 3. Show that if e · d = 1 mod φ(n)/2 then xe·d = 1 mod n. 4. How can we use φ(n)/2 in the RSA? Please explain answers...
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT