A survey of 10 Hip-Hop / R&B records is made. Each person votes by specifying 3 values (between 1 and 10) representing their choice of first, second and third. The first choice scores 4 points, the second 2 and the third 1 point. Using structures, write a program to read the data, process the votes and display the most popular record. Appropriate validations should be incorporated into the program (A person cannot vote the same record twice).

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

typedef struct {

    int records[10];

} RECORD;


int main(int argc, char **argv) {
    RECORD record;
    int i;
    for (i = 0; i < 10; i++)
        record.records[i] = 0;  // Initialize records to 0

    int votecount = 1, choice;
    int buf[2] = {0}; // Buffer to temporarily store choices
    // Assume three persons want to vote
    for (int i = 1; i <= 3; i++) {
        printf("Person %d\n", i);
        // Each person can vote for only three hip-hop music
        while ( votecount <= 3) {
            printf("Enter %d choice: ", votecount);
            scanf("%d", &choice);
            printf("%d\n", record.records[choice-1]);
            L: while (choice < 1 || choice > 10) {
                 printf("Please enter a valid %d choice: ", votecount);
                 scanf("%d", &choice);
            }

            if (votecount == 1){ // 1st vote
                buf[0] = choice;
                record.records[choice - 1] += 4; // 1st choice scores 4 points
            } else if (votecount == 2){
                // Check if new value match first
                if (choice == buf[0]){
                    printf("You can not vote thesame record twice\n");
                    printf("Please select a different %d choice: ", votecount);
                    scanf("%d", &choice);
                    goto L;
                } else { //  Store the second value
                    buf[1] = choice;
                    record.records[choice - 1] += 2; // 2nd choice scores 2 points
                }
            } else if (votecount == 3){
                // Check if vote count matches the previous values
                if (choice == buf[0] || choice == buf[1]){
                    printf("You can not vote thesame record twice\n");
                    printf("Please select a different %d choice: ", votecount);
                    scanf("%d", &choice);
                    goto L;
                } else {
                    record.records[choice - 1] += 1;
                }
            }

            votecount++;
        }
        int j;
        for (j = 0; j < 2; j++)
            buf[j] = 0;
        votecount = 1;
        printf("\n");
    }

    // Compute Maximum Vote
    int max = record.records[0], pos = 0, it;
    for(it = 1; it < 10; it++) {
        if (record.records[it] > max) {
            max = record.records[it];
            pos = it;
        }
    }

    printf("The most popular record is %d and the vote is %d\n", pos+1, max);
    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Person 1
Enter 1 choice: 2
0
Enter 2 choice: 3
0
Enter 3 choice: 4
0

Person 2
Enter 1 choice: 2
4
Enter 2 choice: 5
0
Enter 3 choice: 6
0

Person 3
Enter 1 choice: 7
0
Enter 2 choice: 9
0
Enter 3 choice: 8
0

The most popular record is 2 and the vote is 8

Brief Explanation

  • A structure in C is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
  • Using keyword typedef, RECORD is created as a synonym for the structure representing the 10 Hip-Hop records. RECORD is then use in the main function to define a variable record of type structure. i.e RECORD record
  • The array records of size 10 representing the 10 Hip-Hop records is initialized to 0. This array is used for counting the vote of each record.
  • The program assumes three persons wants to vote. You can modify the program so that the number of voters can be entered at run time.
  • Using the for loop, the choices of the first person is recorded, followed by the second and then the third.
  • Given that each person can choose just three records, a variable votecount is initialized to one and exits the while loop if greater than three.
  • A simple validation mechanism is implemented using the while loop to prevent the user from entering a wrong choice. Values less than 1 or greater than 10 are unacceptable.
  • If votecount is 1 (1st choice), the selected record get a vote score of 4 points, and if votecount is 2 (2nd choice), the selected record get a vote score of 2 points and 1 point for the 3rd choice.
  • goto is used to return control to the label marked L in the case where the user tries to vote the same record twice.
  • After the first person has voted, the temporary buffer buf, is initialized to zero and variable votecount set to 1.
  • Using a for loop, the most popular record is computed and displayed.

Home Work:

What happens when there is a tie i.e more than 1 record has a maximum vote. Modify the code to address that and share your idea in the comments below.

Add comment


Security code
Refresh