Question

Using C language, write a program for the following:

In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes.

A file, electionVotes.txt contains the names of the candidates. The first full name is given as candidate 1, the second as candidate 2 and so forth. The names are followed by the number of votes for the candidate. Write a program to read the data and evaluate the results of the election. Print all the output to a file called electionResults.txt. Your output should specify the total number of votes, the number of invalid votes, and the number of spoilt votes. This is followed on by the votes received by each candidate and the winners of the election.

Suppose you are given the following data in the file with 7 candidates, electionVotes.txt

Victor Taylor Denise Duncan Kamal Ramdhan Michael Ali Anisa Sawh Carol Khan Garry Oliverie 312543535328167735 693 471 24 5 5

Your program should send the following output to the file, electionResults.txt

Invalid vote: 8 Invalid vote: 9 Number of voters: 30 Number of valid votes: 28 Number of spoilt votes: 2 Candidate Score Vict

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code:

// header files
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// main function
int main() {
   // opening file to read
   FILE *fp = fopen("electionVotes.txt","r");
   if(fp == NULL) {
       printf("Error while file opening!\n");
       exit(0);
   }
   // opening file to write
   FILE *out = fopen("electionResults.txt","w");
   if(out == NULL) {
       printf("Error while file opening!\n");
       exit(0);
   }
   // line for reading line by line
   char *line = (char*)malloc(10000*sizeof(char));
   int numOfPerson = 0; // for number of competitors
   char names[100][100]; // for storing names
   // reading line by line
   while (fgets(line, 10000, fp) != NULL) {
       if(line[strlen(line)-1] == '\n') // remove new line if the line contains it at last
           line[strlen(line)-1] = '\0';
       strcpy(names[numOfPerson++], line); // store the names
   }
    fclose(fp); // closing the file
   numOfPerson--; // remove last line which is votes
   int votes[100] = {0}; // for counting number of votes
   int numOfVoters = 0, validVotes = 0; // for counting number of voters
   char *word; // split last line of input file with respect to white space
    word = strtok(line, " "); // splitting
   while( word != NULL ) {
       int vote = atoi(word); // converting to int
       if(vote == 0) // if it is last vote then break
           break;
       if(vote > numOfPerson) // if vote number is greater than number of competitors then invalid
           fprintf(out,"Invalid vote %d\n",vote);
       else {
           votes[vote]++; // otherwise count the vote
           validVotes++; // increase valid vote by 1
       }
       numOfVoters++; // increase total vote by 1
       word = strtok(NULL, " "); // get next vote as string
    }
    // printing the vote details
   fprintf(out,"Number of voters: %d\n",numOfVoters);
   fprintf(out,"Number of valid votes: %d\n",validVotes);
   fprintf(out,"Number of spoilt votes: %d\n\n",numOfVoters-validVotes);
   int mx = -1; // for finding max vote
   // printing individual votes and calculate max vote
   fprintf(out,"Candidate Score\n");
   for(int i=1;i<=numOfPerson;i++) {
       fprintf(out,"%s %d\n",names[i-1],votes[i]);
       if(mx < votes[i]) // update max vote
           mx = votes[i];
   }
   // print all names as winners which have max number of votes
   fprintf(out,"\nWinner(s):\n");
   for(int i=1;i<=numOfPerson;i++)
       if(votes[i] == mx)
           fprintf(out,"%s\n",names[i-1]);
   fclose(out); // closing file
return 0;
}

Sample input (electionVotes.txt):

Victor Taylor
Denise Duncan
Kamal Ramdhan
Michael Ali
Anisa Sawh
Carol Khan
Garry Oliverie
3 1 2 5 4 3 5 3 5 3 2 8 1 6 7 7 3 5 6 9 3 4 7 1 2 4 5 5 1 4 0

Sample output (electionResults.txt):

Invalid vote 8
Invalid vote 9
Number of voters: 30
Number of valid votes: 28
Number of spoilt votes: 2

Candidate Score
Victor Taylor 4
Denise Duncan 3
Kamal Ramdhan 6
Michael Ali 4
Anisa Sawh 6
Carol Khan 2
Garry Oliverie 3

Winner(s):
Kamal Ramdhan
Anisa Sawh

Screenshot of code:

2 3 4 } // header files #include <stdio.h> #include <string.h> #include <stdlib.h> // main function int main() { // opening f35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 while( word != NULL) { int

If the answer helped, please upvote. For any query please comment.

Add a comment
Know the answer?
Add Answer to:
Using C language, write a program for the following: In a student representative council election, there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program that creates a class which represents a candidate in an election. The...

    Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...

  • (Write a program for C++ )that allows the user to enter the last names of five...

    (Write a program for C++ )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 Johnson            5000               25.91...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • It’s almost election day and the election officials need a program to help tally election results....

    It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

  • c++ Votes received by the candidate. Your program should also output the winner of the election....

    c++ Votes received by the candidate. Your program should also output the winner of the election. A sample output is: The Winner of the Election is Duffy. The input data should come from the file. Use 3 separate arrays to store the names, votes, and percentages before they are displayed. Include functions inputValues to input values for candidate names and votes received and place them in arrays. calcPercents which fills an array with the percentage of votes each candidate got....

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should 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. To solve this problem, you will use one dynamic...

  • Ranked choice voting is a system of tallying election ballots that is used in many national...

    Ranked choice voting is a system of tallying election ballots that is used in many national and local elections throughout the world. Instead of choosing a single candidate, voters must rank the available candidates in the order of their choice. For example, if three candidates are available, a voter might choose #2, #1, and #3 as their choices, with #2 being their first choice, #1 the second, and #3 the third. The outcome is determined by a runoff, which follows...

  • To combat election fraud, your city is instituting a new voting procedure. The ballot has a...

    To combat election fraud, your city is instituting a new voting procedure. The ballot has a letter associated with every selection a voter may make. A sample ballot is shown:- (Voter places a tick next to his or her preferred candidate, proposition and measure to indicate his/her vote) 1. Mayoral Candidates     A. Pincher, Penny     B. Dover, Skip     C. Perman, Sue 2. PROP 17     D. YES     E. NO 3. MEASURE 1     F. YES     G....

  • Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write...

    Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate of his/her choice. Display the current number of votes above each button. Include a Finished button that erases the images of the losers and displays only the winner's image with a message of...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT