Big O

• January 5th, 2013, 04:20 PM
shanem
Big O
I got this question , it is a theory question on Big 0
1-if n is 2 what is the final value for counter
2-if n is 5 what is the final value for counter
3-in light of this derive a expression for the number of times the inner loop will run in terms of n
4- state the Big O complexity

int counter =0
for (int i = n; i <=3*n; i ++){
while(i >+n){
i --:
counter ++
}
}

can anyone tell me how to get the values for counter
• January 5th, 2013, 04:32 PM
Norm
Re: Big O
What happens when you compile and execute the code and print out the results?

[code=java]
[/code]
to get highlighting and preserve formatting.
• January 5th, 2013, 04:40 PM
shanem
Re: Big O
it is not a question about the code, it is a theory question on Big O complexity. It asks to figure out what the value for counter is when the values are put in for n.
• January 5th, 2013, 05:14 PM
Norm
Re: Big O
Have you tried executing the code? what does it do?
• January 5th, 2013, 05:21 PM
shanem
Re: Big O
program keeps running
• January 5th, 2013, 05:32 PM
pbrockway2
Re: Big O
Quote:

Originally Posted by shanem
program keeps running

Then it has infinite complexity.

If you suspect that it is intended to finish eventually then post the code you are using. And describe the program's behaviour: especially the output suggested by Norm in #2.

 I think a couple of values for n as suggested by the original question is insufficient to see anything much. Try getting the values of counter for a whole bunch of values of n - say from zero to ten - and then graphing them (counter vs n).
• January 5th, 2013, 05:35 PM
shanem
Re: Big O
The question is not on the code , it is not meant to be used to run a program, it is a theory question to figure out the value for COUNTER when you put the values in for firstly n=2 and then n=5