I finally understand the basics of the for loop but I am really having a hard time grasping the concept of how and when to use a nested loop. Is there anyway someone can explain it? I've spent over an hour trying to grasp it but I have nothing. Specifically there is a question I am trying to figure out. I really want to figure it out on my own but I am having a REALLY hard time getting it.
The question is: Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Ex: numRows = 2 and numColumns = 3 prints:
1A 1B 1C 2A 2B 2C
Nested loops are often used with 2 dimension arrays such as your theatre example.
1 2 3 4 5 6 7 8 9 10
char seats[MAX_ROWS][MAX_COLS] = {0};
for (int i=0; i<MAX_ROWS; i++)
{ // process a row
for (int j=0; j<MAX_COLS; j ++)
{ // Process a column (seat) in current row
if (seat[i][j])
// Is the seat sold (non-zero)
{}
}
}