Answer to Question #47544 in C for eridan

Question #47544
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.
1
Expert's answer
2014-10-08T01:09:04-0400
//answer on Question #47544, Programming, C
#include <stdio.h>
int main(void)
{
printf("Enter the last names of five candidates in a local election and the number of votes received by each candidate\n");
int i=0;
char* name[5][100];
int curVotes=0;
int votes[5];
//read number of votes for each candidate
for( i=0; i<5; ++i )
{
printf( "%d. Last name: ", i+1 );
scanf( "%s", name[i] );
printf( "Number of votes:\n" );
scanf( "%d", &curVotes );
votes[i] = curVotes;
}
float sum = 0;
//calculate total number of votes
for( i=0; i<5; ++i )
{
sum = sum + votes[i];
}
//calculate result percent for each candidate
float percentage[5];
for( i=0; i<5; ++i )
{
percentage[i] = 100*votes[i]/sum;
}
//show info
for( i=0; i<5; ++i )
{
printf( "%s: %d votes( %.2f %%)\n", name[i], votes[i], percentage[i]);
}
int curWinner=0;
float curValue = 0;
//winner of elections
for( i=0; i < 5; ++i )
{
if( percentage[i] > curValue )
{
curValue = percentage[i];
curWinner = i;
}
}
printf( "Winner: %s\n", name[curWinner] );
return 0;
}


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