C Programming The citizens of the nation Atlantis will soon be taking to the pol
ID: 674423 • Letter: C
Question
C Programming
The citizens of the nation Atlantis will soon be taking to the polls to elect their next government. 7 parties (named using the first 7 letters of the alphabet A-G), are participating in the election in 25 states. You have just been commissioned by the parliament, to create software for analyzing the results for the upcoming election. A party wins a state if it obtains the most votes for that state. A party wins the election, if it wins in the most states.
You have decided to make your software reusable for any number of states or parties.
When run, your program should
1. Read the election results from a text file – votes.txt, and store the results by state and party in a 2D array. The specifications for the input file are given below.
2. Manipulate the array created in 1) to obtain the winner of each state
3. Store the winner for each state in a 1D array of characters
4. Print out the winning party for each state
5. Use the array created in 3), to compute the overall winning party for the election
6. Print out the number of states won by each party
7. Print out the winner of the election
Input Specification
1.The first line of the input file is an integer p indicating the number of parties
2. The second line is an integer s indicating the number of states
3. The following p lines of the file will each contain s integers
Comment
Explanation / Answer
/* c progra for elections */
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[50];
int votes;
} candidates_t;
void showBallot(candidates_t cand[], int size) {
int i;
printf(" ");
printf(" OFFICIAL BALLOT ");
for(i=0; i<size; i++) {
printf("%d. %s ", (i+1), cand[i].name);
}
// system("pause"); system("cls"); BAD
printf("INSTRUCTION: ");
printf("TO VOTE FOR A CANDIDATE,INPUT THE NUMBER CORRESPONDING TO YOUR ");
printf("Enter Candidate Number (1-%d) you want to Vote: ", (size+1));
}
int getVoteFromUser(int size) {
int vote;
for(;;){
printf("Vote: ");
scanf("%d",&vote);
vote--;
if(vote>=0 && vote<20) { break; }
printf("Invalid Vote! Please Try Again ");
}
return vote;
}
void showResults(candidates_t cand[], int size) {
int i;
for(i=0; i<size; i++) {
printf("%s got %d votes ", cand[i].name, cand[i].votes);
}
}
int main() {
candidates_t cand[] = {
{ "AQUINO, Juan Gabriel", 0}, { "ASUNCION, Beal", 0}, { "CHONG, Jessica", 0},
{ "DACANAY, Therese", 0}, { "DE LEON, Alveene Joyce", 0}, { "ESPELETA, Paola", 0},
{ "GABALDON, Neil", 0}, { "HADAP, Krista", 0}, { "JUANO, Jeffrey", 0},
{ "KIONG, Jennifer", 0}, { "LIM, Jeremy", 0}, { "LO, Jimmy", 0},
{ "NG, Robbie", 0}, { "ORMOC, Regan", 0}, { "PANULAYA, Leonila", 0},
{ "RECENO, Roy Justin", 0}, { "SEVILLA, Nigel", 0}, { "TAN, Donald", 0},
{ "UY, Jocelle", 0}, { "VELASCO, Anton", 0},
};
int candSize = sizeof(cand)/sizeof(candidates_t);
showBallot(cand, candSize);
cand[getVoteFromUser(candSize)].votes++;
cand[getVoteFromUser(candSize)].votes++;
showResults(cand, candSize);
return 0;
}
-----------------------------------------------------------------------------
OUTPUT:-
OFFICIAL BALLOT
1. AQUINO, Juan Gabriel
2. ASUNCION, Beal
3. CHONG, Jessica
4. DACANAY, Therese
5. DE LEON, Alveene Joyce
6. ESPELETA, Paola
7. GABALDON, Neil
8. HADAP, Krista
9. JUANO, Jeffrey
10. KIONG, Jennifer
11. LIM, Jeremy
12. LO, Jimmy
13. NG, Robbie
14. ORMOC, Regan
15. PANULAYA, Leonila
16. RECENO, Roy Justin
17. SEVILLA, Nigel
18. TAN, Donald
19. UY, Jocelle
20. VELASCO, Anton
INSTRUCTION:
TO VOTE FOR A CANDIDATE,INPUT THE NUMBER CORRESPONDING TO YOUR
Enter Candidate Number (1-21) you want to Vote:
Vote: Vote: AQUINO, Juan Gabriel got 0 votes
ASUNCION, Beal got 0 votes
CHONG, Jessica got 0 votes
DACANAY, Therese got 0 votes
DE LEON, Alveene Joyce got 1 votes
ESPELETA, Paola got 1 votes
GABALDON, Neil got 0 votes
HADAP, Krista got 0 votes
JUANO, Jeffrey got 0 votes
KIONG, Jennifer got 0 votes
LIM, Jeremy got 0 votes
LO, Jimmy got 0 votes
NG, Robbie got 0 votes
ORMOC, Regan got 0 votes
PANULAYA, Leonila got 0 votes
RECENO, Roy Justin got 0 votes
SEVILLA, Nigel got 0 votes
TAN, Donald got 0 votes
UY, Jocelle got 0 votes
VELASCO, Anton got 0 votes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.