-
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
-
Re: Big O
What happens when you compile and execute the code and print out the results?
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
-
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.
-
Re: Big O
Have you tried executing the code? what does it do?
-
Re: Big O
-
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.
[Edit] 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).
-
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