60-140

Introduction to Algorithms and Programming I

Dr. Christie Ezeife

Lab. Exercises  #7 Solution (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

 

 

Solution to Que 1:

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

 

Script started on Fri Sep 03 00:56:17 2010

sol:~/bk2010/programs>cat lab7slnq1.c

/*Lab 7 Question 1

  Calculate total sales income in a month given

  items code price and quantity sold until -1 is entered

*/

#include  <stdio.h>      

 

/*

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

 

*/

int main(void){

 

  int item, numitem = 0;

  float price, quantity, totalsales = 0;

 

  printf("Please enter the item code: ");

  scanf("%d", &item);

 

  while  (item != -1){

    numitem ++;                                         

    printf("Enter Price: ");

    scanf("%f",&price);

    printf("Enter Quantity: ");

    scanf("%f",&quantity);

    printf("Item Code: Item Sales Income: \n  %11d$%.2f\n",item,quantity*price);

    totalsales+=quantity*price;                          

    printf("Please enter the item code: ");

    scanf("%d", &item);                                

 

  }      

  printf("%-18s %s\n","Number of items","Total sales income");

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

  return  0;

}

 

sol:~/bk2010/programs>cc lab7slnq1.c

sol:~/bk2010/programs>a.out

Please enter the item code: 1

Enter Price: 8.00

Enter Quantity: 10

Item Code: Item Sales Income:

  1          $80.00

Please enter the item code: 2

Enter Price: .50

Enter Quantity: 200

Item Code: Item Sales Income:

  2          $100.00

Please enter the item code: 3

Enter Price: 4.50

Enter Quantity: 89

Item Code: Item Sales Income:

  3          $400.50

Please enter the item code: 4

Enter Price: 10.03

Enter Quantity: 10

Item Code: Item Sales Income:

  4          $100.30

Please enter the item code: 5

Enter Price: 1.50

Enter Quantity: 150

Item Code: Item Sales Income:

  5          $225.00

Please enter the item code: 6

Enter Price: 8.00

Enter Quantity: 5

Item Code: Item Sales Income:

  6          $40.00

Please enter the item code: 7

Enter Price: 0.50

Enter Quantity: 100

Item Code: Item Sales Income:

  7          $50.00

Please enter the item code: 8

Enter Price: 4.50

Enter Quantity: 43

Item Code: Item Sales Income:

  8          $193.50

Please enter the item code: 9

Enter Price: 10.03

Enter Quantity: 10

Item Code: Item Sales Income:

  9          $100.30

Please enter the item code: 10

Enter Price: 1.50

Enter Quantity: 79

Item Code: Item Sales Income:

  10         $118.50

Please enter the item code: -1

Number of items    Total sales income

10                 $1408.10

sol:~/bk2010/programs>exit

exit

 

script done on Fri Sep 03 00:59:12 2010

 

 

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

Section 7.8, que. 2

(a).       what output is printed by the following loop ?  N is of type integer.

 

N = 0;

while  ( N <= 10 )

{

            N ++;

            printf(“%d”, N);

}

 

Solution: Output is: 1  2  3  4  5  6  7  8  9  10 11

 

(b) How many times did the loop execute ?      

 

Solution:  The loop executed 11 times.

 

(c) What is printed if we change the first instruction to N = 1 ?

 

Solution: Output is: 2  3  4  5  6  7  8  9  10 11

 

(d) How many iterations are executed this time ?

 

Solution: This time, the loop executed 10 times.

(e) How should the algorithm be modified to have each number printed on a separate line?

          

Solution: Change printf(“%d”, N); to printf(“%d \n”, N);

 

 

 

Section 7.8, Q. 3:

(a)    What output is printed by this nested loop?

 

A = 5;

while  (A >= 1)

{

            B = 1;

  while   (B >= 1)

            {         

                        printf (“%d”, B);

                                    B--;

                        }

                        printf (“\n”);     

                        A--;

            }

 

Solution:
     Output is:

            1

            1

            1

            1

            1

 

(b)   Identify the statements that are updating the control variables.

Solution:  The statements are B-- and A--

(c)    Re-write this program segment such that the two statements B-- and A-- are changed to B++ and A++ respectively. Modify any other instructions appropriately to produce the same results.


Solution:
A = 1;

while  (A <= 5) {

            B = 1;

            while   (B <= 1) {

                        printf (“%d”, B);

                                    B++;

                        }

                        print (“\n”)       

                        A++;

      }

(d)   Re-write this program segment using do-while instructions in place of while instructions.
Solution:

 

  A = 5;

  do {

    B = 1;

    do {

      printf ("%d", B);

      B--;

    } while (B >= 1);

    printf ("\n");

    A--;

  } while (A>=1);

 

 

(e)    Re-write this program segment using a for instruction in place of while instructions.
Solution:

  for (A=5; A>=1; A--) {

    for (B=1; B>=1; B--)

      printf ("%d", B);

    printf ("\n");

  }

}

 

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