Lab for week of 3-19. The idea of looping is to enable you to repeat a group of statements multiple times. The simplest loop is one that does this by counting the number of times it has already done something, starting with an initial count of zero. Each time the loop is repeated, the count of times should increase by one until the count reaches a prespecified limit. A. In lab today, you should take any program that you have completed and change it so that it repeats a specified number of times. I don't really care what program you choose. You can write a counting loop in the following way. 1. Declare a type int variable to keep track of the count. YOu might as well call it count. 2. Insert the lines for (count = 0; /* initial value of count is 0 */ count < 5; /* repeat if count < 5 is true */ count = count + 1) /* increment count by 1 */ { before the first line you wish repeated. These lines should follow all the type declarations. The lines above cause the statements between the curly braces (see step 3. below) to repeat 5 times. (The loop repeats for values of count equal to 0, 1, 2, 3, and 4, and the loop stops repeating when count is 5). You can change the number of times the statements repeat to 10 times just by changing the "loop repetition condition" count < 5 to count < 10. Normally we just write the for loop heading on one line without the comments: for (count = 0; count < 5; count = count + 1) { 3. Insert a closing curly brace just after the last line you wish to repeat. Try this and see if you do indeed have a program that repeats. B. You can make this a little more general by letting the program user specify how many times to repeat the program. Declare another variable, num_times (type int) and prompt for and read in the number of times the program should repeat BEFORE you reach the start of the for loop - (i.e. before the line that begins with for). Change the for loop heading to for (count = 0; /* start count at 0 */ count < ______ ; /* repeat if count < num_times is true */ count=count+1) /* increment count by 1 */ { Fill in the blank with what seems to be appropriate. Test this modified version of your program. C. Now let's try another way to write a counting loop using a while statement instead of a for statement. Get your original program again and declare a variable count that is type int. This time you will actually spell out the way the computer manipulates the counter variable - the variable that is keeping track of the number of times the loop has repeated. Begin by setting this variable to 0 just before the first statement you want to repeat. count = 0; Then use the while loop header to test the condition that should be true in order for the loop statements to repeat. For example: while (count < 5) { means repeat the loop body (everything between the braces) "while" the condition "count less than 5" is true. Since count starts at 0, the loop will repeat 5 times if you remember to increase the value of count by 1 after each time the loop statements execute (count starts at 0, goes to 1, 2, 3, 4, and 5 - but when it reaches 5 the condition count < 5 is false, not true). To increase the value of count by 1, just insert count = count + 1; } at the end of the loop - the closing brace indicates the end of the loop. Run this program and see if it works. Notice that the for statement simply combines the three C statements above into one line, replacing the word while with for: for (count = 0; count < 5; count = count + 1) { ... loop body goes here. } Using while loop: count = 0; /* start counter at initial value - 0 */ while (count < 5) { /* test counter */ ... loop body goes here. count = count + 1; /* increment counter */ } The statement count = count + 1; appears so frequently, C provides a special increment operator to write it: count++; Try sustituting this line for count = count + 1; and make sure the program works as before. D. Finally, let's try a different way to control the repetition of a loop. Instead of counting, we will use a data value to control the loop. For the projectile program, change the prompt for specifying whether the angle dimensions are in radians or degrees to: printf("Enter R (radians), D (degrees), or Q (quit):"); scanf(" %c", &rad_or_degree); If the program user enters Q, we want the program to stop its calculations. If the user enters R or D, it should carry out the computations as before and then repeat. To accomplish this, insert the line below just after the scanf. The condition in parentheses will be true and the loop will repeat if the user types in any character except for Q or q. The loop will stop if the user types in either Q or q. while (rad_or_degree != 'Q' && rad_or_degree != 'q') { /* read the angle and do the angle conversion if necessary */ /* compute height and time as before */ /* display the result just as before. */ ... Now comes the tricky part. After you finish the computations and display the result, you need to ask the user to type in again the character that determines whether the loop repeats or stops. This means you need to insert the printf and scanf shown above at the end of the loop as well. This seems pretty strange but it works! printf(" ... prompt goes here. ... "); scanf(" %c", @rad_or_degree); while ( ... condition that tests rad_or_degree ...) { /* all the stuff we did before to get angle and calculate and display the result */ ... printf(" ... prompt goes here. ... "); scanf(" %c", @rad_or_degree); } Each time we read a new character using the last scanf statement at the end of the loop, the computer retests the condition in the while header. If the condition is still true (we did not read Q or q), the loop repeats. When the condition becomes false (we did read Q or q), the loop is exited and the calculations stop. Make these changes and retry your projectile program.