Program to tabulate a survey data

Program 30) A survey to know the popularity of four cars (Ambassador, Fiat, Dolphin and Maruti) was conducted in four cities(Bombay, Calcutta, Delhi and Madras). Each person surveyed was asked to give his city and the type of the car he was using. The results form, are tabulated as follows:
 M    1     C     2     B     1     D     3     M     2     B     4
 C     1     D     3    M     4     B     2     D     1     C     3
 D     4     D     4    M     1     M    1     B     3      B     3
 C     1     C     1    C     2     M     4     M    4      C     2
 D     1     C     2    B     3     M     1     B     1      C    2
 D     3     M    4    C     1      D     2     M     3     B    4

Codes represent the following information:
            M - Madras             1 - Ambassador
            D - Delhi                 2 - Fiat
            C - Calcutta            3 - Dolphin
            B - Bombay            4 - Maruti 
Write a program to produce a table showing popularity of various cars in four cities.

A two dimensional array frequency is used as an accumulator to store the number of cars used, under various categories in each city. For example, the element frequency [i] [j] denotes the number of cars of type j used in city i. The frequency is declared as an array of size 5 x 5 and all the elements are initialized  to zero.
This program reads the city code , one set after another, from the terminal. Tabulation ends when the letter X is read in place of a city code.


                                      PROGRAM

main ( )
 {
     int i, j, car ;
     int frequency [5] [5] = {  {0}, {0}, {0}, {0}, {0}  } ;
     char city ;
     printf ("For each person, enter the city code \n") ;
     printf ("followed by the car code. \n") ;
     printf ("Enter the letter X to indicate end.\n") ;
   /*      TABULATION BEGINS      */
     for (i = 1; i < 100; i++)
     {
        scanf ("%c", &city) ;
        if (city == 'X') ;
           break ;
        switch (city)
        {
                  case 'B'   :    frequency [1] [car]++ ;
                                       break ;
                  case 'C'   :    frequency [2] [car]++ ;
                                       break ;
                  case 'D'   :    frequency [3] [car]++ ;
                                       break ;
                  case 'M'  :    frequency [4] [car]++ ;
                                      break ;
         }
     }
  /*TABULATION COMPLETED AND PRINTING BEGINS*/
     printf ("\n\n") ;
     printf ("   POPULARITY TABLE\n\n") ;
     printf ("______________________\n") ;
     printf (" City Ambassador Fiat Dolphin Maruti   \n") ;
     printf ("______________________\n") ;
     for (i = 1; i <= 4; i++)
     {
               switch (i)
               {
                          case 1 : printf ("Bombay      ") ;
                               break ;
                          case 2 :  printf ("Calcutta      ") ;
                               break ;
                          case 3 : printf (" Delhi          ") ;
                               break ;
                          case 4 : printf ("Madras         ") ;
                 }
                 for (j = 1; j <=4; j++)
                     printf ("%7d", frequency [i] [j]) ;
                printf ("\n") ;
        }
        printf ("___________________________\n") ;
  /*              PRINTING ENDS             */
 }


Output:       For each person, enter the city code
                   followed by the car code.
                   Enter the letter  X to indicate end     
                   M 1  C  2   B   1   D   3   M   2   B   4

                   C  1  D  3  M   4   B   2   D   1   C   3
                   D  4  D  4  M   1   M  1   B   3   B    3
                   C  1  C  1  C   2   M   4   M  4   C   2
                   D  1  C  2  B   3   M   1   B   1   C   2
                   D  3  M  4  C  1    D   2   M  3   B   4     x

                        POPULARITY TABLE
__________________________________________________
   City           Ambassador      Fiat         Dolphin     Maruti
   Bombay        2                      1                3                2
   Calcutta        4                      5                1                0
   Delhi             2                      1                3                2
   Madras         4                      1                1                4
__________________________________________________
       
       

Multiplication table using two dimensional array

Program 29) Write a program to compute and print a multiplication table for numbers 1 to 5 as shown below:
                1     2     3     4     5
         1     1     2     3     4     5
         2     2     4     6     8    10
         3     3     6     .      .      .
         4     4     8     .      .      .
         5     5    10    .      .     25

This program uses a two dimensional array to store the table values. Each value is calculated using the control variables of the nested loops as follows:
                       product [i] [j] = row * column
where i denotes rows and j denotes columns of the product table. Since the indices i and j range from 0 to 4, we have introduced the following transformation:
                         row     = j + 1
                      column = J + 1 

                           PROGRAM

 #define ROWS 5
 #define COLUMNS 5
 main ( )
  {
         int row, column, product [ROWS] [COLUMNS] ;
         int i, j ;
         printf ("     MULTIPLICATION TABLE\n\n") ;
         printf ("  ") ;
         for (j = 1; j <= COLUMNS; J++)
             printf ("\n") ;
             printf ("____________________\n") ;
             for (i = 0; i< ROWS; i++)
             {
                  row = i + 1 ;
                  printf ("%2d |", row) ;
                  for (j = 1; j <= COLUMNS; J++)
                  {
                     column j ;
                     product [i] [j] = row * column ;
                     printf ("%4d", product [i] [j]) ;
                 }
                 printf ("\n") ;
             }
  }


Output:      MULTIPLICATION TABLE
                 1     2     3     4     5
           1    2     4     6     8    10
           2    3     6     9    12   15
           3    4     8    12   16   20
           4    5    10   15   20   25 
   

Program for frequency counting

Program 28) Given below is the list of marks obtained by a class of 50 students in annual examination.

43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37
40 49 16 75 87 91 33 24 58 78 65 56 76 45 54 36 63 12 21
 73 49 51 19 39 49 68 93 85 59

Write a program to count the number of students belonging to each of following group of marks:
 0 - 9, 10 - 19, 20 - 29, ...., 100

This program uses the array group containing 11 elements, one for each range of marks. Each element counts those values falling within the range of values it represents.
For any value, we can determine the correct group element by dividing the value by 10. For example, consider the value 49. The integer division of 59 by 10 yields 5. This element into which 59 in counted.

                                   PROGRAM

 #define MAXVAL 50
#define COUNTER 11
main ( )
 {
          float  value [MAXVAL] ;
          int     i, low, high ;
          int   group [COUNTER] = {0,0,0,0,0,0,0,0,0,0,0} ;
        
                   /*    READING AND COUNTING    */
                   for (i = 0; i < MAXVAL; i++)
                   {
                   /*    READING OF VALUES    */
                       scanf ("%f", &value [i]) ;
                   /*  COUNTING FREQUENCY OF GROUPS   */
                       ++ group [(int) (value[i] / 10] ;
                   }
                   /*     PRINTING OF FREQUENCY TABLE   */
                   printf ("\n") ;
                   printf (" GROUP   RANGE   FREQUENCY\n\n) ;
                   for (i = 0; i < COUNTER; i++)
                   {
                        low = i * 10 ;
                        if (i == 10) ;
                           high = 100 ;
                    else
                          high = low + 9 ;
                    printf (" %2d %3d to %3d %d\n",
                                 i + 1, low, high, group [i]) ;
                   }
  }


Output: 

 43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74
81 49 37 40 49 16 75 86 91 33 24 58 78 65 56 76 67  ( Input
45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59          data)
     GROUP                RANGE                FREQUENCY
       1                        0   to   9                       2
       2                       10  to  19                      4
       3                       20  to  29                      4
       4                       30  to  39                      5
       5                       40  to  49                      8
       6                       50  to  59                      8
       7                       60  to  69                      7
       8                       70  to  79                      6
       9                       80  to  89                      4
      10                      90  to  99                      2
      11                     100 to 100                     0

 Note that we have used an initialization statement.
    int group [COUNTER] = {0,0,0,0,0,0,0,0,0,0,0} ;
   which can be replaced by
                     int group [COUNTER] = {0} ;
This will initialize all the elements to zero.   
 

Program of minimum cost problem

Program 27) Minimum Cost

 Problem: The cost of operation of a unit consist of two components C1 and C2 which can be expressed as functions of a parameter p as follows:
                     C1 = 30 - 8p
                     C2 = 10 + p(square)
The parameter p ranges from 0 to 10. Determine the values of p with an accuracy of +0.1 where the cost of operation would be minimum.


Problem Analysis:
                   Total cost = C1 + C2 = 40 - 8p + p(square)
The cost is 40 when p = 0, and 33 when p = 1 and 60 when p = 10. The cost, therefore, decreases first and then increases. This program evaluates the cost at successive intervals of p (in steps of 0.1) and stops when the cost begins to increase. The program employs break and continue statements to exit the loop.


                          PROGRAM

 main ( )
 {
       float p, cost, p1, cost1 ;
       for (p = 0; p <= 10; p = p + 0.1)
       {
             cost = 40 - 8 * p + p * p ;
             if (p == 0)
             {
                 cost1 = cost ;
                 continue ;
             }
             if (cost >= cost1)
               break ;
             cost1 = cost ;
             p1 = p ;
        }
        p = (p +p1) / 2.0;
        cost = 40 - 8 * p + p * p ;
        printf ("\nMINIMUM COST = %.2f AT p = %.1f\n",
                            cost, p) ;
  }


Output:    MINIMUM COST = 24.00  AT p = 4.0

Program of minimum cost problem

Program 27) Minimum Cost

 Problem: The cost of operation of a unit consist of two components C1 and C2 which can be expressed as functions of a parameter p as follows:
                     C1 = 30 - 8p
                     C2 = 10 + p(square)
The parameter p ranges from 0 to 10. Determine the values of p with an accuracy of +0.1 where the cost of operation would be minimum.


Problem Analysis:
                   Total cost = C1 + C2 = 40 - 8p + p(square)
The cost is 40 when p = 0, and 33 when p = 1 and 60 when p = 10. The cost, therefore, decreases first and then increases. This program evaluates the cost at successive intervals of p (in steps of 0.1) and stops when the cost begins to increase. The program employs break and continue statements to exit the loop.


                          PROGRAM

 main ( )
 {
       float p, cost, p1, cost1 ;
       for (p = 0; p <= 10; p = p + 0.1)
       {
             cost = 40 - 8 * p + p * p ;
             if (p == 0)
             {
                 cost1 = cost ;
                 continue ;
             }
             if (cost >= cost1)
               break ;
             cost1 = cost ;
             p1 = p ;
        }
        p = (p +p1) / 2.0;
        cost = 40 - 8 * p + p * p ;
        printf ("\nMINIMUM COST = %.2f AT p = %.1f\n",
                            cost, p) ;
  }


Output:    MINIMUM COST = 24.00  AT p = 4.0