In the following code segement, how many times is STMT executed?
for (I=N; i >1 ; i = i/2)
for (j = 1; j <= N; j++)
STMT
I can do normal summation formula, what throws me off is the first for loop. Any help would be appreciated. Thank you .
(Please do rate the answer if you found useful - Assuming program is in C++ - Output is attached - Please read comments for explanation)
We can solve this problem by analyzing the time complexity. Total running time complexity of statement will be O(N) - (pronounced Big-Oh of N)
Let us breakdown:
Outer Loop Executes Log2N times - Trace the value like first assume N=5 hence Outer loop executes Log25 = 2
next assume N=15 Outer Loop executes Log215 = 3 (Note - Example tracing is shown below as images)
Inner Loop Executes Count of outer loop times N - Trace value First assume N=5 hence Outer loop executes Log25 = 2 then Inner loop executes N*2 = 10, Now assume N=15 Outer Loop executes Log215 = 3 inner loop executes N*3=45
Hence lets call Outer loop executes Log2N times and inner loop executes N*Log2N times Hence complexity of total program is O(NLog2N)
Simple Program for Clear Understanding
#include <iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"Enter N:";
cin>>n;
int firstloop=0,secondloop=0; //count for outerloop and
innerloop
for (i=n; i >1 ; i = i/2)
{
firstloop++; //incrementing count
cout<<"First loop"<<i<<endl;
for (j = 1; j <= n; j++)
{
secondloop++; //incrementing count
cout<<"Secondloop"<<j<<endl;
}
}
cout<<"First Loop Count
:"<<firstloop<<endl<<"Second Loop Count
:"<< secondloop<<endl;
return 0;
}
Output


In the following code segement, how many times is STMT executed? for (I=N; i >1 ;...
What will be the value of x after the following code is executed? How many times will the following do-while loop be executed?
When the Java source code below is executed, how many times is the boolean expression (j > 0) evaluated? int i 1; do { int j 35; while (j > 0) { j = j / 2; = } i += 1; } while (i <= 3); 18 none of the other answers are correct 0 6 36
Question 68 (Mandatory) (1 point) When the Java source code below is executed, how many times is the boolean expression (j > 0) evaluated? = int i 1; int p 0; while (i <= 2) for(int j = 28; (j > 0); j /= 2) { p+= 5; { } i i + 1; مه none of the other answers are correct 12 6 18
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
Problem 1: Let W(n) be the number of times "whatsup" is printed by Algorithm WHATSUP (see below) on input n. Determine the asymptotic value of W(n). Algorithm WHATSUP (n: integer) fori1 to 2n do for j 1 to (i+1)2 do print("whatsup") Your solution must consist of the following steps: (a) First express W(n) using summation notation Σ (b) Next, give a closed-form formula for W(n). (A "closed-form formula" should be a simple arithmetio expression without any summation symbols.) (c) Finally,...
EJ int i- 60; while (i>0) printf("%din",i-); if (i 50) printf("Doneln"); break How many times is the first printf statement executed? How many times is the second printf statement executed? How many lines of output will be displayed? Would the previous answers change if -i were used instead of i--? Is i in scope outside of the for loop? Turn in a copy of this homework completed.
What will be the value of x after the following code is executed? int x = 5; while (x <50) { x += 5; } a) 50 b) 45 c) Infiniteloop d) 55 Given the following code segment: Scanner in = new Scanner (System.in); boolean done = false; while (!done) { int input = in.next(); if(input.equalsIgnoreCase("q")) done = false; } What will cause the loop to terminate? a) When the user enters -1 b) The loop will never terminate c)...
(1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....
How many times are the while and for loops run in the following code? Count the total number of iterations from both the while loop and for loop. p = 32; t = 8; while t <p t=t+ floor(p/pi); for k = 1:2:10 p = p+2; end end Select one: O a. 42 b. 5 c.o o d. 26 o e.30 me to search
whats the answers
For the following code, indicate how many times the loop body will execute for the following input values: 2 1 0? userNum - 3 while (userNum > 0) { // Do something // Get user um from input } 3 Given the following code, how many times will the inner loop body execute? int row: int col; for (row = 0; row < 2; row - row + 1) { for (col = 0; col < 3;...