Question

In: Computer Science

Consider the following function: int counter( int n) { int s = 0;    for ( int...

Consider the following function:

int counter( int n)

{ int s = 0;

   for ( int i = 0; i < n; i++)

            for ( int j = i; j > 0; j--)

                       s = s + 1;

  return s;

}

Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work.

Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.

Solutions

Expert Solution

Solution:

===============

Here for ( int i = 0; i < n; i++) will be executed exactly n time from i=0 to i=n-1

for each value of i inner for ( int j = i; j > 0; j--) will be executed i times.

thus s=s+1 will be executed in the following way

when i=0 inner for loop will execute 0 times

when i=1 inner for loop will execute 1 times

when i=2 inner for loop will execute 2 times

when i=3 inner for loop will execute 3 times

-----------------

--------------

-------------

when i=n-1 inner for loop will execute n-1 times

Hence total number of times  s = s + 1; will be executed is 0+1+2+3+4+5+6...........(n-1)

This sum is in AP thus its sum is gievn by   = n/2(2a+ (n-1)d)

here a is 0

d is 1

n is n-1

Hence sum is

Ans

Part b) TIme complexity of this function depends on how many times  s = s + 1; statement is executed from above result number of times statement is executed is (n)(n-1)/2 = (n^2-n)/2 Here leading term is n^2 for very large value of n effect of (n)/2 can be neglacted thus time Complexity is given by


Related Solutions

Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n)...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n) { int sum; for (int i = 0; i < n; i++) ​ sum = sum + (i * n); return sum; } 2) int function(int n) { ​int sum; ​ ​for (int i = 0; i < n; i++) ​​if (n < 1000) ​​​sum++; ​​else ​​​sum += function(n); } 3) int function(n) { ​int s = 0; ​for (int i = 1; i...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · ·...
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · · · } and transition matrix (pij ) given by pij = 1 2 if j = i − 1 1 2 if j = i + 1, i ≥ 1, and p00 = p01 = 1 2 . Find P{X0 ≤ X1 ≤ · · · ≤ Xn|X0 = i}, i ≥ 0 . Q2. Consider the Markov chain given in Q1. Find P{X1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT