![]() |
Array with For Loop
I have a problem understanding how arrays are working when using the for loop. I am confused as to how lines 23-24 are working, how the stars are being printed. Because the statement says if the stars are less than the value of the one in the index using the counter variable, then increment (stars++). Does that not increment stars by one every time, how does the star get printed when a number like 4 gets read out of the index in the array. I just cant seem to get my head round on what variable it is applying it to. If someone is kind of enough to help me out in understanding please help. Thank you very much.
:
Figure 7.6. Bar chart printing program. |
Re: Array with For Loop
:
for (int stars = 0; stars < array[counter]; stars++):
int stars = 0;(except that the scope of 'stars' is limited to the inside of the for loop in the first case). 'stars' is just a counter that keeps track of how many stars you've printed. Once it reaches array[counter], you print a newline and move to the next index in the array. |
Re: Array with For Loop
Quote:
Grade distribution: 00-09: 10-19: 20-29: 30-39: 40-49: 50-59: 60-69: * 70-79: ** 80-89: **** 90-99: ** 100: * I understand the counter part of the for loop. But I do not understand how the stars are being calculated from the array in order to be printed. It seems weird that the stars increments by 1 every time, how does the star for loop pick up that the numbers in the index of an array and print the star for that. Thanks for the help. This is very confusing. |
Re: Array with For Loop
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++)If you go back to the first line I posted: :
array[counter] = array[8] = 4So array[counter] = 4 at this point in time, and the code becomes interpreted as: :
for (int stars = 0; stars < 4; stars++)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? |
Re: Array with For Loop
Quote:
|
Re: Array with For Loop
Quote:
:
That should output: :
0 0That should hopefully answer your question. Edit: I apologize if any of my syntax or anything is wrong. I've never used Java before. I'm only rearranging pieces from your post based intuitively on how things work in other languages. |
Re: Array with For Loop
I have one quick question, when the prefix increment operator is used before accessing the index of that array, what is it supposed to do, shift the index by one? Thanks for the help.
:
3 import java.util.Random; |
Re: Array with For Loop
I've annotated the source. I don't know if will be helpful at all (Sane seems to have covered it fine), but it couldn't hurt. :)
:
Alternatively, depending on the version of Java you have installed, you can iterate through an array like this: :
EDIT: In reply to your second question, given: :
Doing this: :
Will result in an array that looks like this: :
|
Re: Array with For Loop
I wasn't even looking at the inner for loops properly. Thanks everyone that really helped alot for my understanding in arrays.
|
Re: Array with For Loop
Quote:
|
| All times are GMT -5. The time now is 12:32 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC