Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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

2. ## 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.

3. ## 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.

4. ## Re: Big O

Have you tried executing the code? what does it do?

5. ## Re: Big O

program keeps running

6. ## Re: Big O

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).

7. ## 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