60-140

Introduction to Algorithms and Programming I

Dr. Christie Ezeife

Lab. Exercises  #7 (Lab Date: Week 9 of Classes)

 

Objectives are to:

1.                                                      Practise on use of repetition (loop) instructions like while, do-while and for instructions in problem solving as taught in chapter 7 of text book.

2.                                                      Begin to prepare for quiz #2.

 

Continue to prepare for midterm test if not yet written.

 

Que. 1. Do a version of question 4 of Section 7.8 of book as follows.

 

Write a program that computes the total monthly sales income of a small convenience store. It is not known the number of items sold in the month.  Your program should read information about each item sold in this store in the month until a sentinel value of -1 is read. The information about each item to be read are the item code, unit price and number sold of each item in the month. The output of the program should print each item and the total sales income from the item as well as the number of items sold and the total sales income for the month.

Sample input:

item code         unit price          number sold this month

 

1                     8.00                             10

2                     0.50                             200

3                     4.50                             89

4                     10.03                           10

5                     1.50                             150

6                     8.00                             5

7                     0.50                             100

8                     4.50                             43

9                     10.03                           10

10                   1.50                             79

      -1

 

 

Sample output:

Item code        Item Sales Income

1                                 $80.00

2                                 $100.00

:

 

Number of items                      Total sales income

              10                                          $ 1408.10

 

 

Hints on how to solve

 

i.                     You need an event-control loop instruction because you do not know the number of data lines (number of weeks to compute for) ahead of time.  You can only tell that there are no more data when the week number read is a sentinel value of  1.

ii.                   To help in solving the problem, you can type and execute the following program first, which provides a scanty version of the solution to the problem.  This following program, reads only the item code until a sentinel value of –1 is read.  Your job now is to modify the program such that it can read the item code, price and quantity until there is no more data line for an item (which is indicated when a -1 is read), and to solve the problem in lab question 1.

 

#include            <stdio.h>

int main(void)

{

            int   item, numitem = 0;

            float  price, quantity, totalsales = 0;

            printf(“please enter the item number\n”);

            scanf(“%d”, &item);

            while  (item != -1)

            {

                        numitem ++;

                        /* Read the items sold in this month and compute sales */

                        //------------------------------------------

                        //------------------------------------------

                        printf(“please enter the item code\n”);

                        scanf(“%d”, &item);

            }          /*  end of while */

           

            printf("%d \t $%0.2f \n", numitem, totalsales);

 

            return  0;

}                      /*  end of program        */

 

 

  Compile and run your program with test data and show your work in a script file.

 

Que. 2.  Do questions 2 and 3 of Section 7.8

 

Que. 3. (Optional) Define a problem of your own that involves loops and solve.            Que. 4. Start to prepare for Quiz #2