I dont understand!!

Hey everyone i don't understand why counter < responses.length;

includes the 40th number in the array shouldn't it stop at the 39th number?

int responses [] ={1,2,6,4,8,5,9,7,8,10,1,6,3,8,6,10,3,8,2,7,6,5,7,6

,8,6,7,5,6,6,5,6,7,5,6,4,8,6,8,10};

int frequency [] =newint[11];

for(int counter = 0;counter < responses.length;++counter)

{

++frequency[responses[counter]];

}//end for loop

[789 byte] By [Timmy_The_Turtlea] at [2007-11-26 21:56:22]
# 1
You're pre-incrementing your counter variable. Change it to: for(int counter = 0;counter < responses.length;counter++)and see how it behaves then.
kevjavaa at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 2
If length is 5, then the indices in the array are 0, 1, 2, 3, 44 < 5, so arr[4] (the 5th and last one) shows up.
jverda at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 3

> You're pre-incrementing your counter variable.

>

> Change it to:

> [code]

> for(int counter = 0;counter <

> < responses.length;counter++)

> /code]

> and see how it behaves then.

Absolutely irrelevant.

++counter vs. counter++ does NOT cause the ++ to happen at a different point relative to the loop. It's simply that the value the ++counter expression is the incremented value of counter, and the value of the counter++ expression is the original value of counter. But that expression's value is not used anywhere, so it doesn't matter.

jverda at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 4
> Absolutely irrelevantYou're correct, sir.Just ignore me. :)*goes for coffee*
kevjavaa at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 5
What i don't understand is why if the counter is set to include everything less than 40 it includes 40 anyway.
Timmy_The_Turtlea at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 6
> What i don't understand is why if the counter is set> to include everything less than 40 it includes 40> anyway.Read Jverds reply. It is because you start from zero. 0 to 39 = 40 iterations. The loop runs 40 times.Message was edited by:
kikemellya at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 7
> What i don't understand is why if the counter is set> to include everything less than 40 it includes 40> anyway.Re-read reply 2. It doesn't include index 40. It includes arr[0] through arr[39]. That's 40 items.
jverda at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...
# 8
> Re-read reply 2. It doesn't include index 40. It> includes arr[0] through arr[39]. That's 40 items.OK i get it now thanks..
Timmy_The_Turtlea at 2007-7-10 3:52:49 > top of Java-index,Java Essentials,New To Java...