CPE 101
Laboratory 5


Due Date

Objectives

Part 1

Below is a program similar to Figure 5.10 in the textbook.  Study the program to understand how it works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
/* Compute the sum of a list of rainfall measurements. */

#include <stdio.h>

#define kSentinel -99

int main(void)
{
        int sum = 0;   /* output - sum of rainfalls input so far    */
        int rainfall;     /* input - current rainfall    */

        /* Accumulate sum of all rainfalls.    */
        printf("Enter first rainfall (or %d to quit)> ", kSentinel);
        scanf("%d", &rainfall);       /* Get first rainfall.    */
        while (rainfall != kSentinel)
        {
            sum = sum + rainfall;
            printf("Enter next rainfall (%d to quit)> ", kSentinel);
            scanf("%d", &rainfall);   /* Get next rainfall.    */
        }
        printf("\nSum of rainfall measurements is %d\n", sum);

        return (0);
}



  1. Copy and paste the source code into your program editor.  Name the file rainfall.c. Compile it (it should compile with no errors).  Execute it with the following data: 55 33 77 -99.  Does it produce the correct results?

  2. Comment out the printf statement on line 18.  Now execute the program and enter all the input data on a single line. Does it produce the correct results?

  3. Change the sentinel constant to negative one. Execute it with the following data:  -5 -10 -15 -99 -1.  Does it produce the correct results? (Does it make any sense to have a negative rainfall measurement?)

  4. Change the while loop condition (and sentinel) so that any negative number will terminate the loop.

  5. Explain the effect of moving line 17 after 19.  What will be the output for the following data:  10 15 5 3 -1?  Execute the program and verify your prediction. Return the statement to it's original position.

  6. Enhance the loop so that it counts the number of rainfall measurements that are entered.  Display the count after the sum. Execute the program and be sure it works correctly for datasets of size 1, 2, and 3.

Part 2

  1. Download the skeleton of the cubes_table.c program. 
    Study the source code to comprehend how the program works.

  2. Predict the output if the user enters two numbers:  9   3

  3. Compile the program; it should compile with no errors.  Execute the program and see if your prediction is correct.
    Execute the program and enter values:  -5 5 3
    Explain the output in your lab notebook.

  4. Predict how the output will appear if the user enters:  -5 5 -9 3
    Execute the program and see if your prediction is correct.

  5. Enhance the get_first() function by adding a input validation loop similar to the one in the get_table_size() function. The loop should reject negative user inputs.

  6. Enhance the show_table() function by implementing the code for a counting loop that will display the desired table of cubes. 
    For inputs of  4  5  the output should look like this:

    Number  Cube
    5       125
    6       216
    7       343
    8       512

  7. Add a new function get_increment() that is similar to get_first().  It should display the prompt "Enter the increment between rows".  The function should use an input validation loop to reject negative user inputs.

  8. Enhance the show_table() so that it takes a third parameter that is the row increment in the table. Modify the counting loop so that it uses the row increment when displaying the table.

  9. Enhance the main function so that it calls get_increment() and passes the result to show_table()
    For inputs of  4  5  3 the output should look like this:
  10. Number  Cube
    5       125
    8       512
    11      1331
    14      2744

  11. Enhance the show_table() function so that it displays the sum of all the cubes in the table on a separate line below the table.

Part 3


Write a program find_sevens.c to perform the following 3 tasks:

  1. find how many numbers are in a list of positive integers.
  2. find the position of the last occurrence of the number 7 in the list.
  3. (Challenge) find the position of the first occurrence of the number 7 in the list

E.g. for a list of :  3 6 12 4 7 12 34 7 9 8

List contains 10 numbers.
Last occurrence: 8
First occurrence: 5

The data for this program is to be read from an data file using the technique of an "endfile-controlled" loop described on pages 265-266. (Example in Fig 5.11).
There is one number per line in the file.  You do not know in advance how many numbers are in this file. 

Create you own data file for testing your program.  When you are satisfied that it is working correctly, ask the instructor for his or her test data file and demonstrate your program running with the instructor data file.


Part 4: Handing in Your Programs Electronically

1.      Transfer all three  files (rainfall.c, cubes_table.c, find_sevens.c) to unix1 as you've done for previous labs.

2.      Log on to unix1 using the Secure Shell Client program (or your favorite equivalent).

3.      Change directory (cd-command) to the directory containing the source file or files to hand in.

4.      Execute the following command:

handin graderjd Lab05 rainfall.c cubes_table.c find_sevens.c

5.      You should see messages that indicate handin occurred without error.