Question

Let T(n) be the runtime of the following RaiseToPower() function that computes baseVal raised to the n-th power. int RaiseToP

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q->1:

Given code:

  1. int RaiseToPower(int baseval, int n){
  2.     int resultval;
  3.     if (n == 0){
  4.         resultval = 1;
  5.     }
  6.     else{
  7.         resultval = baseval * RaiseToPower(baseval, n - 1);
  8.     }
  9. return resultval;
  10. }

Options:

a. T(n) = C0, if n=0, (C0 is a small constant variable)

True, this is because of condition at line 3 which sets resultval to 1 and at line 9 it will be returned.

b) T(n) also varies with the first parameter, namely int baseVal

False, in programming we assume that multiplication is a constant time operation.

c) T(n) = C1 + T(n-1), if n>1 (C1 is a small constant)

True, this is because this function calls itself recursively n-1 times, and nth time it returns in C2 time.

So, a and c are correct

.

Q->2: How many times will highlighted line need to be executed?

Code:

  1. for (int i = 0; i < n - 1; i++)
  2.     for (int j = 0; j < n; j++)
  3.         cout << i + j << endl;

Answer:

a. n*(n-1)

This is because,loop at line 1 executes n-1 times

Then loop at line 2 executes n times

Add a comment
Know the answer?
Add Answer to:
Let T(n) be the runtime of the following RaiseToPower() function that computes baseVal raised to the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1. Select the running time of each function. void print_array (int* A, int n) for...

    Problem 1. Select the running time of each function. void print_array (int* A, int n) for (int í 0; i < n; ++i) cout << A[i] << endl; void print_array pairs (int* A, int n) for (inti 0; i < n; ++i) for (int j 0; j < n; ++j) cout << Ai] ALj]< endl; void print_array_start(int* A, int n) for (int i 0; i < 100 ; ++i) cout << A[i] << endl; void print_array_alt (int* A, int n)...

  • 14.3 How many times is the statement cout<<]<<","<</<<","; executed in the following program? int I, );...

    14.3 How many times is the statement cout<<]<<","<</<<","; executed in the following program? int I, ); for (I = 2; I >= 0; I--) { for (] = 1; }<2; J++) cout << ) << ", << I << cout << endl; 11 } Select one O 2.3 0 6.6 O c. 2 d. None of the above are correct

  • Find the worst case runtime f(n) for the following algorithms. Specify the number of operations executed...

    Find the worst case runtime f(n) for the following algorithms. Specify the number of operations executed for an input size n, for the worst case run time as a function of n. Circle statement(s) and draw a   line to the right side specifying the number of operations. If statement(s) are a part of an iteration of n, specify the total number of iterations as a function of n. Algorithm-01 int sum = 0; int j = 1; while ( <=...

  • Write a function Transpose that transposes a matrix T with M rows and N colums. The...

    Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • What are the valid runtime bounds for the method mystery with respect to argument n? Select...

    What are the valid runtime bounds for the method mystery with respect to argument n? Select all that apply. public static int mystery(int n) { for (int i = 1; i < n; i = i + i) { for (int j = 0; j < i; j++) { for (int k = 0; k < n; k++) { f0(n); } } } return -1; } public static void f0(int n) { int[] arr = new int[200]; for (int i...

  • Determine the runtime complexity of the following. Use O() notation, but the function that you place within the parenthe...

    Determine the runtime complexity of the following. Use O() notation, but the function that you place within the parentheses of O() should be the upper bound on the runtime of the algorithm or function. Unless otherwise specified, N is the problem size or the number of elements in the container (even if N does not appear in the code or algorithm you are given), and all other values are constants. For graphs, use |V| and |E| for the size of...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

  • The following function prints a natural number sequence across the diagonal of an n by n...

    The following function prints a natural number sequence across the diagonal of an n by n square matrix. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the num value passed to the function is 5, the output would look like the following. 1**** *2*** **3** ***4* ****5 void naturalDiagonal(int num); for (int i=0; i < num - 1; i++) { for (int j=1; j <= number;...

  • B) draw the runtime stack fall the steps (a) write the output of the following program....

    B) draw the runtime stack fall the steps (a) write the output of the following program. The output must include everything that a computer may produce on its screen, including the prompts, input, output, and their positions. Suppose the input integers are 1 and 2, in this order. (b) Draw the run time stack for every step of execution on the next page. You need to mark the return address(es) yourself. #include <iostream> using namespace std; int a; int b;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT