-1

1. int i=1;
2. while (i <= n) {
3. for (int j=1; j<10; j++)
4. sum++;
5. i++;
6. }
7. for( int j = 1; j <= n; j++ )
8. for( int k = 1; k <= n; k=k*2 )
9. sum++;

1. How many times is statement 4 executed?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)
E. none of the above

Here I chose A

2. How many times is statement 9 executed?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)
E. none of the above

Because of line 8 (k=k*2) I chose C

3. What is the running time of the entire code fragment?
A. O(n)
B. O(n2)
C. O(log n)
D. O(n log n)

Since O(n)+O(logn)=O(n) so I chose A

What do u think?

Votes + Comments
Do your OWN homework!
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by firstPerson
0

I can only help you with number 4.

4. How to do your homework?
A. By yourself
B. By yourself
C. Both A and B
D. Both A, B, C, and D

The correct answer is D

1

I just want to know if I am on the right track
It is an exercise to a past exam paper. for revision

0

Sorry, I did not see you already solved it. I am not sure about any of these because these is not C++ but computer science.
Next time post this to Software Engineering --> computer science

0

For 1, you choose A, which is correct.

For 2, your almost there but its not correct, because there are 2 nested loop you have to take into account both of them.

For 3, redo 2 then see what you get. But your almost there.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.