Calculation of range of values

Program 20) A survey of the computer market shows that personal computers are sold at varying costs by the vendors.The following is the list of costs (in hundreds) quoted by some vendors:
  
35.00,        40.50,               25.00,          31.25,           68.15,
47.00,        26.65,               29.00            53.45,           62.50

Determine the average cost and the range of values.
Problem analysis: Range is one of the measure of dispersion used in statistical analysis of a series of values. The range of any series is the difference between the highest and the lowest values in the series.  That is 
                        Range = highest value - lowest value
It is therefore necessary to find the highest and the lowest values in the series.
This program is to determine the range of values and the average cost of a personal computer in the market .

                             PROGRAM

main ( )
 { 
        int count ;
        float value, high, low, sum, average, range ;
        sum = 0 ;
        count = 0 ;
        printf ("Enter numbers in a line : input a NEGATIVE
                                    number to end\n") ;
input:
          scan (%"f", &value) ;
          if (value < 0) goto output ;
               count = count + 1;
          if (count == 1)
               high = low = value ;
         else if (value  high)
               high = value ;
             else if (value > low)
                    low = value ;
             sum = sum + value ;
             go to input ;
Output:
            average = sum/count ;
            range = high - low ;
            printf ("\n\n") ;
            printf ("Total values : %d\n", count) ;
            printf ("Highest-value: %f\nLowest-value : %f\n",
                                                     high, low) ;
            printf ("Range           : %f\nAverage : %f\n", range,
                                                average);
           getch ( ) ;
  }

Output:
Enter number in a line: input a NEGATIVE number to end
35  40.50  25  31.25  68.15  47  26.65  29  53.45  62.50  -1

Total values     :  10
Highest-value  :   68.150002
Lowest-value   :   25.000000
Range       :    43.150002
Average   :      41.849998

When the value is read the first time, it is assigned to two buckets, high and low, through the statement
                            high = low = value ;
For subsequent values, the value read is compared with high; if it is larger, the value is assigned to high. Otherwise, the value is compared with low; if it is smaller, the value is assigned to low. Note that at a given point, the buckets high and low hold the highest and the lowest value read so far.
The values are read on an input loop created by the goto  input; statement. The control is transferred out of the loop by inputting a negative number. This is caused by the statement 
                          if (value < 0) goto output ;
Note  that this program can be written using without goto statements. Try. 
        

Use of the goto statement

Program 19) This program illustrates the use of the goto statement. The program evaluates the square root for five numbers. The variable count keeps the count of numbers read. When count is less than or equal to 5, go to read; directs the control to the label read; otherwise, the program prints a message and stops.

                                         PROGRAM

#include <math.h>
main ( )
 {
         double x, y ;
         int count ;

        count = 1 ;
        printf ("Enter FIVE real values in a LINE \N") ;
read:
        scanf ("%if",&x) ;
        printf("\n") ;
       if (x<0)
          printf ("Value - %d is negative\n" ,count) ;
          else
            {
                y = sqrt(x) ;
                printf ("%1f\t %1f\n", x, y) ;
            }
            count = count +1 ;
            if (count <= 5)
  go to read ;
            printf ("\nEnd of computation") ;
         getch( ) ;
 }

Output:     Enter FIVE real values in a LINE
                 50.70  40  -36  75  11.25
                 50.750000         7.123903
                 40.000000         6.324555
                 Value -3 is negative
                 75.000000         8.660254
                 11.250000         3.354102
                  End of computation
 
 

llustration of the conditional operator

Program 18) An employee van apply for a loan at the beginning of every six months but he will be sanctioned the amount according to the following company rules:
Rule 1 : An employee cannot enjoy mote than two loan at
             any point of time.
Rule 2 : Maximum permissible total loan is limited and 
             depends upon the category of the employee.

This program is process loan applications and to sanction loans.
        
                                  PROGRAM

#define MAXLOAN 50000
main ( )
 {
        long int loan1, loan2, loan3, sancloan, sum23 ;
        printf (" Enter the values of previous 
                           two loans : \n") ;
        scanf ("%1d %1d &loan1 &loan2 ) ;
         
        printf ("nEnter the value of new loan : \n") ;
        scanf ("%1d", &loan3) ;

        sum23 = loan2 + loan3 ;
        sancloan = (loan1>0)? 0 : ((sum23>MAXLOAN)?
                                                MAXLOAN - loan2 : loan3) ;
        printf ("\n\n") ;
        printf ("Previous loans pending: \n%1d %1d\n", 
                                  loan1, loan2) ;
        printf ("Loan requested = %1d\n", loan3) ;
        printf ("loan sanctioned = %1d\n", sanclon) ;
       getch ( ) ;
 }

Output:       Enter the values of previous two loans :
                   0  20000
                   Enter the value of new loan :
                   45000
                   Previous loans pending :
                   0  20000
                   Loan requested = 45000
                   Loan sanctioned = 30000
                   Enter the values of previous two loans :
                   1000  15000
                   Enter the value of new loan :
                   25000
                   Previous loans pending :
                   1000  15000
                   Loan requested  = 25000
                   Loan sanctioned = 0


The program uses the following variables:
    Loan3  -  present loan amount requested
    Loan2  -  previous loan amount pending
    Loan1  -  previous to previous loan pending
    sum23 -   sum of loan2 and loan3
    sanclon  -  loan sanctioned 
The rules for sanctioning new loan are:
    1. Loan1 should be zero
    2. Loan2 + Loan3 should not be more than MAXLOAN.

Illustration of else...if ladder

Program 17) An electric power distribution company charges its domestic consumers as follows:

Consumption Units       Rate of charge
   0 - 200                         Rs. 0.50 per unit
201 - 400                        Rs. 100 plus Rs. 0.65 per unit
                                      excess of 200
401 - 600                        Rs. 230 plus Rs. 0.80 per unit
                                      excess of 400
601 and above               Rs. 390 plus Rs. 1.00 per unit
                                      excess of 600

This program reads the customers number and power consumed and prints the amount to be paid by the customer.

                                    PROGRAM

main ( )
 {
       int units, cutnum ;
       float charges ;
       printf ("Enter CUSTOMER NO. and UNITS 
                                   consumed\n") ;
       scanf ("%d %d", &custom, &units) ;
       if (units <= 200)
           charges = 0.5 * units ;
       else if       (units <= 400)
                        charges = 100 + 0.65 * (units - 200) ;
                            else if (units <= 600)
                            charges = 230 + 0.8 * (units - 400) ;
                                else
                                 charges = 230 + 0.8 * (units - 400) ;
                                    else
                                    charges = 390 + (units - 600) ;
      printf ("\n\nCustomer No: %d: Charges = %2f\n",
                                           custnum, charges) ;
      getch( ) ;
 }

Output:     
     
Enter CUSTOMER NO.and UNITS consumed 101 150
Customer No: 101 Charges = 75.00

Enter CUSTOMER NO.and UNITS consumed 202 225
Customer No: 202 Charges = 116.25

Enter CUSTOMER NO.and UNITS consumed 303 375
Customer No: 303 Charges =  213.75


Enter CUSTOMER NO. and UNITS consumed 404 520
Customer No: 404 Charges = 326.00


Enter CUSTOMER NO. and UNITS consumed 505 625
Customer No: 505 Charges = 415.00



Selecting the largets of three numbers

Program 16) This program selects and prints the largest of the three numbers using nested if....else statements.

                                   PROGRAM

main ( )
 {
       float A, B, C ;
       
       printf ("Enter three values\n") ;
       scanf ("%f %f %f, &A, &B, &C) ;

       printf ("\nlargest value is ") ;
       if (A>B)
        {
            if (A>C)
                printf ("%f\n", A) ;
            else
                printf ("%f\n", C) ;
        }
        else

         {
             if (C>B)
                 printf ("%f\n", C) ;
             else
                 printf ("%f\n", B) ;
        }
      getch( ) ;
 }

Output:          Enter three values
                      23445 67379 88843

                     Largest value is 88843.oooooo 

Use of if for counting

Program 15) Write a program to counts the number of boys whose weight is less than 50Kg and height is greater than 170cm.

The program has to least two conditions, one for weight and another for height. This is done using the compound relation
                         if (weight < 50 && height > 170)
This would have been equivalently done using two if statements as follows:
                        if (weight < 50)
                           if (height > 170)
                              count = count +1 ;
If the value of weight is less than 50, then the following statement is executed, which in turn is another if statement. This if statement tests height and if the height is greater than 170, then the count is incremented by 1.

                                  PROGRAM
main( )
 {
        int count, i ;
        float weight, height ;

        count = 0 ;
        printf ("Enter weight and height for 10 boys\n") ;
   
        for (i = 1; i <= 10; i++) 
         {
              scanf ("%f %f", &weight, &height) ;
              if (weight < 50 && height > 170)
                         count = count + 1 ;
         }
        printf ("Number of boys with weight < 50kg\n") ;
        printf ("and height > 170cm = %d\n", count) ;
        getch( ) ;
 }

Output:         Enter weight and height for 10 boys
                      45        176.5
                      55        174.2
                      47        168.0
                      49        170.7
                      54        169.0
                      53        170.5
                      49        167.0
                      47        167
                      51        170
                      Number of boys with weight < 50kg
                      and height > 170cm = 3 

Program for inventory report

Program 14) The ABC Electric Company manufactures four consumer products. their inventory position on a particular day is given below:

      Code                        Quantity              Rate(Rs)
       F105                         275                       575.00
       H220                        107                        99.95
       1019                         321                        215.50
       M315                        89                         725.00

It is required to prepare the inventory report table in the following format:
                               INVENTORY REPORT 
      Code              Quality             Rate          Value
       ____               _____               ____          _____
       ____               _____               ____          _____
       ____               _____               ____          _____
       ____               _____               ____          _____
                                                 Total value    _____
The value of each item is given by the product of quantity and rate.
This program reads the data from the terminal and generates the required output. 


                                  PROGRAM

#define ITEMS 4
main( )
 {
        int i, quantity[5] ;
        float rate[5], value, total_value ;
        char code[5] [5] ;
        i = 1 ;
        while(i <= ITEMS)
         {
                 printf("Enter code, quantity, and rate:") ;
                 scanf("%s %d %f", code[1], &quantity[i],
                                          &rate[i]) ;
                 i++ ;
         }
       /*....Printing of table and column Headings....*/
         printf("\n\n") ;
         printf("           INVENTORY REPORT       \n") ;
         printf("-----------------------------------------------\n") ;
         printf("   Code  Quantity  Rate  Value   \n") ;
         printf("-----------------------------------------------\n") ;
      /*....Preparation of inventory position....*/
         total_value = 0 ;
         i = 1 ;
         while(i <= ITEMS) 
          {
                  value = quantity[i] * rate[i] ;
                  printf("%5s %10d %10.2f %e\n", code[i],
                               quantity[i], rate[i], value) ;
                  total_value += value ;
                  i++ ;
          }
     /*....Printing of End of table....*/
       printf("------------------------------\n") ;
       printf("                  Total value = %e\n", total_value) ;
       printf("------------------------------\n") ;
       getch( ) ;
 }


Output:        
            Enter code, quantity, and rate:F105  275   575.00
            Enter code, quantity, and rate:H220  107   99.95
            Enter code, quantity, and rate:I0I9    321   215.50
            Enter code, quantity, and rate:M315  89    725.00
                                   
                               INVENTORY REPORT
      _______________________________________________
      Code              Quantity          Rate                Value
     ________________________________________________
     F105                     275             575.00        1.581250e+005
     H220                     107              99.95        1.069465e+004
     I0I9                        321             215.50       6.917550e+004
     M315                     89               725.00       6.452500e+004
     ________________________________________________
                                              Total Value = 3.025202e+005
    ________________________________________________



Reading of strings

Program 13) Reading of strings using %wc and %ws.

In program illustrates the use of various field specifications for reading strings. When we use %wc for reading a string. the system will wait until the wth character is keyed in. Note that the specification %s terminates reading at the encounter of a blank space.
Therefore, name2 has read only the first part of "NEW YORK" and the second part is automatically assigned to name3. However, during the second run, the string "New-York" is correctly assigned to name2.

                                 PROGRAM

main( )
 {
      int no ;
      char name1[15], name2[15], name3[15] ;
      
      printf("Enter serial number and name one \n") ;
      scanf("%d %15c", &no, name) ;
      printf("%d %15s\n\n", no, name1) ;
      printf("Enter serial number and name two\n");
      scanf("%d %s", &no, name2) ;
      printf("%d %15s", &no, name2) ;
    
      printf("Enter serial number and name three\n") ;
      scanf("%d %15s", &no, name3) ;
      printf("%d %15s\n\n", no, name3) ;
      getch( ) ;
 }

Output:           Enter serial number and name one
                       1  123456789012345
                       1 123456789012345r
                       Enter serial number and name two
                       2 New York
                       2                    New
                       Enter serial number and name three
                       2                    York
                       Enter serial number and name one
                       1 123456789012
                       1 123456789012r
                       Enter serial number and name two
                       2 New York
                       2                   New-York
                       Enter serial number and name three
                       3 London
                       3                   London