Question

In: Computer Science

Program must be in C Write a program that allows the user to enter the last...

Program must be in C

Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate in two different arrays.

The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate.

Your program should also output the winner of the election.

Example (Letters and numbers with underscore indicate an input):

Enter candidate's name and the votes received by the candidate.
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Asthony 1800

 Candidate    Votes Received   % of Total Votes
 ---------    --------------   ----------------
   Johnson       5000                 25.91
    Miller       4000                 20.73
     Duffy       6000                 31.09
  Robinson       2500                 12.95
   Asthony       1800                  9.33

Total Votes: 19300
The Winner of the Election is: Duffy

Solutions

Expert Solution

Thanks for the question, here is the C program, with output screenshot

================================================================================

#include <stdio.h>

int main(){

               

                char candidates[5][20]; // to store candidate names

                int votes[5]={0,0,0,0,0}; // to store votes for each candidate

                int totalVotes =0; // total votes of all 5 candidates

                int winnerIndex =0; // index of the candidate with highest vote

                int i ;

               

                printf("Enter candidate\'s name and the votes received by the candidate.\n");

                for(i=0; i<5; i++ ){

                                scanf("%s %d",&candidates[i],&votes[i]);

                                totalVotes +=votes[i]; // add the count in the accumulator

                                if(votes[winnerIndex]<votes[i]){

                                                winnerIndex = i; // run time update the winner index

                                }

                               

                }

// display the results

                printf("%20s %20s %20s\n","Candidate","Votes Received","% of Total Votes");

                printf("%20s %20s %20s\n","---------","--------------","----------------");

                for(i=0; i<5; i++ ){

                                printf("%20s %20d %20.2f\n",candidates[i],votes[i],votes[i]*100.0/totalVotes);

                }

               

                printf("\nTotal Votes: %d",totalVotes);

                printf("\nThe Winner of the Election is: %s",candidates[winnerIndex]);

               

               

}


Related Solutions

C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the...
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by that candidate. The program should then output each candidates name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follow Johnson    5000 25.91 miller    4000   ...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a program that allows the user to enter two integers and a character If the...
Write a program that allows the user to enter two integers and a character If the character is A, add the two integers If it is S, subtract the second integer from the first else multiply the integers Display the results of the arithmetic
Write a Python program that allows the user to enter the total rainfall for each of...
Write a Python program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September 3.7 inches October 5.1 inches November 7.2...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT