IPC144 Lab #8 - Arrays

Weekly Tip: C does not check if an array index is within the array's bounds. When programming with an array, ensure that its index remains within the array bounds (0 to (s - 1), where s is the array size).

In this lab, you will experiment with arrays. 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 "lab8.txt", follow the instructions (at the bottom of this lab) to submit your answers to Murray Saul's e-mail account...

1. Enter the following program (call it lab8a.c):

   #include <stdio.h>
   main() {
      int nums[10], i, tot = 0;
      double avg;
      for (i = 0; i < 10; i++) {
         printf("Enter number %d: ", i + 1);
         scanf("%d", &nums[i]);
         tot += nums[i];
      }
      avg = tot/10.0;
      printf("Numbers bigger than the average (%.1lf):", avg);
      for (i = 0; i < 10; i++)
         if (nums[i] > avg)
            printf(" %d", nums[i]);
      printf("\n");
   }

 Run the program a few times, and make sure you understand what the code is doing. Answer the following questions:

a)      What does this program do?

__________________________________________________________________________________________________________

b)      Why is the first printf written to display i + 1 and not i?

___________________________________________________________________________________________________________

c)      Why is tot divided by 10.0 and not 10?

___________________________________________________________________________________________________________

d)      Why are two "for" loops needed (i.e. why couldn't it have been done in a single loop)?

____________________________________________________________________________________________________________

  1. Modify lab8a.c so that it also displays all the numbers entered that were less than the average, after displaying the output it already displays.
  2. Copy lab8a.c to lab8b.c. In lab8b.c, further modify the program so that the array can hold 100 numbers, and so that the user can enter 100 numbers. Then modify the program so that the user can enter ANY number of numbers desired, up to a maximum of 100 numbers. After each number is input, the user should be asked if he/she wants to enter another number, using y or n as an answer. (Hint: you will need two extra variables, one to keep track of the user's answer to the question and another to keep track of how many numbers the user actually entered).
  3. Now enter the following program (call it lab8c.c):
   #include <stdio.h>
   main() {
      int qty[5], i;
      double cost[5], value, tot = 0;
      for (i = 0; i < 5; i++) {
         printf("Enter quantity on hand of item %d: ", i + 1);
         scanf("%d", &qty[i]);
         printf("Enter unit cost of item %d: ", i + 1);
         scanf("%lf", &cost[i]);
      }
      printf("\nInventory Report\n");
      printf("Item Quantity Total Value\n");
      printf("---- -------- -----------\n");
      for (i = 0; i < 5; i++) {
         value = qty[i] * cost[i];
         tot += value;
         printf("%4d%9d%12.2lf\n", i + 1, qty[i], value);
      }
      printf("Total inventory value: %.2lf\n", tot);
   }

Test this program and ensure that you know what is going on. Notice how the numerical values in the conversion specifiers help generate columnar output. In particular, notice the technique of "parallel arrays", where matching positions in different arrays store information about the same item. Answer the following questions:

a)      What is the qty array used to store?                              ___________________________________

b)      What is the cost array used to store?                             ___________________________________

c)      Why are two different arrays needed by the program?            ____________________________________________________________

 

  1. Modify lab8c.c so that a maximum of 10 items can be processed. However, the precise number of items is not known, so an input of 0 for quantity in the program is to signal that there are no more items.

Submission Requirements:

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

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

The option -s "144lab8" makes subject line appear as "144lab8" so instructor can filter these e-mails in a directory to collect all lab8 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...