PROGRAM: 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 car he was using. The results, in coded 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 the popularity of various cars in four cities.
PROBLEM: The scanf
in the for
loop isn't taking input as per the condition I've mentioned. The loop is supposed to run 36 times so that I can input data 36 times, rather it takes only 19 inputs and then the loop ends. I don't know why it's happening.
#include<stdio.h>
void main(){
unsigned short car, i, survey[5][5] = {{0},{0},{0},{0},{0}};
char city;
printf("Enter city initial and car code\n");
for (i = 0; i < 36; i++)
{
scanf("%c %hu",&city,&car);
switch (city)
{
case 'b':
survey[1][car]++;
break;
case 'c':
survey[2][car]++;
break;
case 'd':
survey[3][car]++;
break;
default:
survey[4][car]++;
break;
}
}
printf("BOMBAY:\nAmbassador = %hu\nFiat = %hu\nDolphin = %hu\nMaruti = %hu\n",survey[1][1],survey[1][2],survey[1][3],survey[1][4]);
printf("CALCUTTA:\nAmbassador = %hu\nFiat = %hu\nDolphin = %hu\nMaruti = %hu\n",survey[2][1],survey[2][2],survey[2][3],survey[2][4]);
printf("DELHI:\nAmbassador = %hu\nFiat = %hu\nDolphin = %hu\nMaruti = %hu\n",survey[3][1],survey[3][2],survey[3][3],survey[3][4]);
printf("MADRAS:\nAmbassador = %hu\nFiat = %hu\nDolphin = %hu\nMaruti = %hu\n",survey[4][1],survey[4][2],survey[4][3],survey[4][1]);
}
OUTPUT:
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
BOMBAY:
Ambassador = 1
Fiat = 1
Dolphin = 2
Maruti = 1
CALCUTTA:
Ambassador = 1
Fiat = 1
Dolphin = 1
Maruti = 0
DELHI:
Ambassador = 1
Fiat = 0
Dolphin = 2
Maruti = 2
MADRAS:
Ambassador = 9
Fiat = 4
Dolphin = 5
Maruti = 9