IPC144 Lab 4 - Controlling Loops (Mr. Saul's Sections)

Weekly Tip: when using loops remember initialization, test and change - the three elements of repetition

In this lab, you will experiment with controlling loops. As with the previous labs, if you get stuck, don't be shy about asking for help - that is what the lab sessions are for.

NOTE: When you have completed this lab, and placed answers into the file called "lab4.txt", follow the instructions (at the bottom of this lab) to submit your answers to Murray Saul's e-mail account...

Start by compiling and testing the following program (call it lab4a.c):

   main() {
      int amt;
      int tot;
      int i;
      i = 0;
      tot = 0;
      while (i < 10) {
         printf("Enter a number: ");
         scanf("%d", &amt);
         tot = tot + amt;
         i = i + 1;
      }
      printf("The sum of all those numbers is %d.\n", tot);
   }
  1. This program uses a couple of classic programming techniques: the use of a variable to store a running total (tot), and the use of a counter, a variable whose purpose is to keep track of how many times something has been repeated (i). Since mathematicians typically use names like i, j, and k for counting purposes, so do many programmers, although many other programmers prefer a name like "count" or "cnt" to the very generic name "i".

    Why are i and tot both set to zero before the while loop, but not amt?                            ______________________________________________

    How many times does the loop repeat (i.e. how many numbers are you asked to enter)?                                                                        __________

    How could you make the program ask for only 5 numbers instead?                                ______________________________________________
  2. Copy lab4a.c to lab4b.c and open lab4b.c in a text editing program such as NLED. In lab4b.c, change the program in two ways at once:
   Enter a number (enter 0 to stop):

(Hint: the loop will now be controlled by the value in amt instead of the value in i).

   The sum of those 15 numbers is 204.

(where, of course, the actual number of numbers entered should be displayed instead of 15, and the actual total should be displayed instead of 204).

   35 21 42 63 75 92 8 6 0 -1 2
  1. Copy lab4b.c to lab4c.c and open lab4c.c in a text editing program such as NLED. In lab4c.c, change the program so that it will stop accepting numbers when the user enters 0 or the user has entered 20 numbers, whichever comes first. (Hint: now the loop will be controlled by the values in both i and amt, so you'll need a compound condition).
  2. Copy lab4a.c to lab4d.c and open lab4d.c in a text editing program such as NLED. Change lab4d.c to use the for statement instead of the while statement. Which version do you prefer? (It is a personal opinion - there is no right answer).
  3. [Only if you have time left over] modify the program from #3 (or #4, if you prefer) so that it keeps track of two subtotals, the total of all of the positive numbers entered and the total of all of the negative numbers entered. With inputs of 3, 314, 2, -4, 5, and 310 the final outputs should be:
   The sum of the 5 positive numbers entered is 634.
   The sum of the 1 negative numbers entered is -4.
The sum of all 6 numbers entered is 630.

Hint: you will need at least two running total variables, and at least two counter variables, along with an if statement inside the while loop, deciding which total and counter to update.

Submission Requirements:

If you are in Murray Saul's class, issue the following command to send your lab #4 answers to Murray Saul:

mail -s "144lab4" -c $USER@learn.senecac.on.ca murray.saul@senecac.on.ca < lab4.txt

The option -s "144lab4" makes subject line appear as "144lab4" so instructor can filter these e-mails in a directory to collect all lab4 submissions.

The option -c $USER@learn.senecac.on.ca sends a copy of the e-mail message to YOUR learn account. The variable $USER is your Matrix id name assuming that you are issuing this command when logged into your Matrix account. Please keep this e-mail for the remainder of this term as proof that you sent your lab by the required deadline...