/* Here is the code for the cups example. Recall, during lecture we used a series of differently colored cups to represent variables and arrays in the program. Below is a table where I point out the basic change in the variables as the loop executes. */ #include int main() { int blue[] = {5, 10, 15, 20, 25}; int black = 0; int red = 0; int clear = 10; int blue_i = 0; char headings[8][8] = { "black", "red", "clear", "blue[0]", "blue[1]", "blue[2]", "blue[3]", "blue[4]" }; printf("\n"); printf( " %9s %9s %9s %9s %9s %9s %9s %9s\n", headings[0], headings[1], headings[2], headings[3], headings[4], headings[5], headings[6], headings[7] ); printf("\n"); for ( black = 0; black < 5; black++, clear = clear + 5 ) { if ( ( clear % 10 ) == 0 ) { blue[ black ] = blue[ black ] + clear; } else { for ( red = 0; red < 5; red++ ) { blue[ red ] = blue[ red ] - ( clear % 10 ); } } printf( " %9d %9d %9d", black, red, clear ); for ( blue_i = 0; blue_i < 5; blue_i++ ) { printf( " %9d", blue[ blue_i ] ); } printf("\n"); } printf("\n"); } /* The loop runs from 0 to 4, we increment black by one each time thus we will be in the black loop 5 times. I do not show the change in clear until the next iteration of the loop because the change happens at the very end. Init is the initial value I have chosen for each variable. Time in the loop 5 - contains final values Variable Init 1 2 3 4 5 black = 0 0 1 2 3 4 red = 0 ( hard to show in a table like this the red value changing ) clear = 10 10 15 20 25 30 35 ( note the final value of clear ) blue[0] = 5 15 10 10 5 5 blue[1] = 10 10 5 5 0 0 blue[2] = 15 15 10 30 25 25 blue[3] = 20 20 15 15 10 10 blue[4] = 25 25 20 20 15 45 Note the values printed for blue by the code are a transpose ( rows and columns flipped )of the table above Each time we enter the else statement the red variable will start equal to zero and then increment: 1 - 2 - 3 - 4 - and finally to 5. Then the loop will exit. So the final value of red will be 5. */