Those are apparently the correct answers but could I get an explaination as to h
ID: 3835592 • Letter: T
Question
Those are apparently the correct answers but could I get an explaination as to how to come to those answers?
Also, how many times do these parts below execute and why?
i <= N
j <= i
k<N
Consider the following code fragment. 01 for (int i 31 i N i++) 02 03 for int j 1 j i j++ 04 for (int k 31 k N k++) 05 06 c [k1 func (i, j, k) 07 08 09 10 Problem 1 Assuming that N is an integer greater than zero, how many times is each of the following operations performed when the code fragment above is executed? Give your answer in each box below as a closed- form function of N on line 01 i N NH j++ on line 03 on line 05 k++Explanation / Answer
We have 3 FOR loop, all are nested in each other
So, outermost FOR loop runs N times: i=1 to i<=N
and we are doing i++ in outer most FOR loop only,
so i++ will execute N times
In middle FOR loop: for each value of i from outer FOR loop:
It runs i times: j=1 to j=i
S0, for i=1
it runs = 1 times
for i =2, it runs 2 times
for i=3 , it runs 3 times
----
for i=N, it runs N times
Total execution: i + 2 + 3 + .. + n
= N(N+1)/2
So, j++ execute N(N+1)/2 times
Innermost FOR loop runs: (N-1) times for each value of j
k = 1 to (N-1)
Since middle FOR loop runs: N(N+1)/2 times
so, it will run (N-1) * N(N+1)/2
= (N-1)N(N+1)/2 times
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.