I see where your difficulty lies. I had to blink a couple times before seeing what was happening. I'll try to explain.
int array[] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
As you know, that's the input of the grade distribution.
In the first for loop, the variable "counter" starts at 0, and loops until it's through to the end of array[].
for ( int counter = 0; counter < array.length; counter++ )
So let's try to calculate the grade distribution of the 9th item. That means we're on the for loop's
9th iteration... also known as the
8th index. Which contains the value,
4.
The counter will equal
8 at this point.
So what happens when we get to this code?
for (int stars = 0; stars < array[counter]; stars++)
System.out.print("*");
If you go back to the first line I posted:
array[counter] = array[8] = 4
So
array[counter] = 4 at this point in time, and the code becomes interpreted as:
for (int stars = 0; stars < 4; stars++)
System.out.print("*");
The variable "stars", starts at
0, and iterates as long as its less than
4. Therefore, the second for loop will iterate
4 times, printing out a star each time.
Voila.
Does that helps you understand what's happening?