What is the number of operations(assignment statements) that occur in the follow
ID: 3617226 • Letter: W
Question
What is the number of operations(assignment statements) that occur in the following code fragmentin closed form in terms of n? In Big Onotation?
(Note: Ignore assignment operators insideloop headers and you may assume n is a power of2)
int d = 0;
for(int i = 1; i <= n; i++)
{ for(int j = 1; j <= i; j++)
d = d + i +j;
d = d + 2;
}
for(int k = 1; k < n; k = k*2)
{ d = d ? 5;
}
Can someone explain me how to get the closed form in terms ofn. The answer is:
Answers: Closed: ((n2 + n)/2 + n) +(log2 n)) + 1
BigO: O(n2)
but i dont undertand how to get it
Explanation / Answer
Any problem to uderstanding it then you can sendPrivate message to me and tell? Question Details:What is the number of operations (assignment statements) thatoccur in the following code fragment in closed form in terms ofn? In Big O notation?
(Note: Ignore assignment operators inside loop headers and you mayassume n is a power of 2)
int d = 0;
for(int i = 1; i <= n; i++) //runmaximum of n times
{
for(int j = 1; j <= i; j++) //Run maximum of n timesfor each iteration of outer loop
d = d + i +j;
d = d + 2;
}
for(int k = 1; k < n; k = k*2) //Run log n time as the k isincreased twice on each iteration
{ d = d ? 5;
}
so the complexity will be
=(n2+log2 n)
dominating term is n2 so it will take maximum of O(n2)time
Answers: Closed: ((n2 + n)/2 + n) + (log2 n))+ 1
BigO: O(n2)
Question Details: Question Details:
What is the number of operations (assignment statements) thatoccur in the following code fragment in closed form in terms ofn? In Big O notation?
(Note: Ignore assignment operators inside loop headers and you mayassume n is a power of 2)
int d = 0;
for(int i = 1; i <= n; i++) //runmaximum of n times
{
for(int j = 1; j <= i; j++) //Run maximum of n timesfor each iteration of outer loop
d = d + i +j;
d = d + 2;
}
for(int k = 1; k < n; k = k*2) //Run log n time as the k isincreased twice on each iteration
{ d = d ? 5;
}
so the complexity will be
=(n2+log2 n)
dominating term is n2 so it will take maximum of O(n2)time
Answers: Closed: ((n2 + n)/2 + n) + (log2 n))+ 1
BigO: O(n2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.