Answer to Question #96955 in C for ilham

Question #96955
conduct a survey from 10 people to discover most popular sport. the result will be typed in to the computer for analysis at the end of the survey analysis the result and disply the most popular sports “type in the letter Q for finish”
A athletes
B swimming
C football
D badminton
1
Expert's answer
2019-10-23T14:23:33-0400
#include <stdio.h>

#define NP 10 /* number of people */
#define NS 4 /* number of sports */

int main(void) {
    char result, survey[NP], most;
    int i, pop, freq[NS] = { 0 };
    const char *sports[NS] = {
        "athletes", "swimming", "football", "badminton"
    };

    for (i = 0; i < NS; i++)
        printf("%c: %s%s", 'A' + i, sports[i], (i + 1 < NS) ? "; " : "\n");

    for (i = 0; i < NP; i++) {
        printf("Person %d: ", i + 1);
        scanf(" %c", &result);
        survey[i] = result;
        if ( !(result >= 'A' && result <= 'D') )
            return 1;
        freq[result - 'A']++;
        if (i == 0)
            most = result, pop = 1;
        else if (freq[result - 'A'] > pop)
            most = result, pop = freq[result - 'A'];
    }
    
    printf("The most popular sport (one of) is %c: %s (%d)\n", most,
        sports[most - 'A'], pop);
    for (i = 0; i < NP; i++)
        if (survey[i] == most)
            printf("Person %d\n", i + 1);

    return 0;
}

/*
A: athletes; B: swimming; C: football; D: badminton
Person 1: A
Person 2: B
Person 3: C
Person 4: D
Person 5: C
Person 6: D
Person 7: A
Person 8: B
Person 9: D
Person 10: B
The most popular sport (one of) is D: badminton (3)
Person 4
Person 6
Person 9

*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS