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
__________________________________________________