For C LANGUAGE ****Please answer in C Language***** Write 3 functions that could
ID: 3834544 • Letter: F
Question
For C LANGUAGE ****Please answer in C Language***** Write 3 functions that could be used in a program to conduct an election.
a) Write a function called win. Win will take as input the number of votes cast (e.g., votesCast) and the tallied votes in favor of a proposition or candidate (e.g., forVotes) and return true if the votes in favor are a majority of the votes cast (greater than 50%) and false if the votes in favor are not a majority of the votes cast. If the votes casts is less than the votes in favor, display an appropriate error message and return false. Write a main function to test your win function for all possible scenarios (input combinations).
b) Write a function called vote to cast a vote. The function does not have any inputs. It will return a struct Ballot. It will display a title for prop 75, prompt the user to vote on prop 75, display the possible candidates, and prompt the user to vote for a candidate. When voting for a candidate it should provide the candidate with the various options and assign the appropriate enum value to the candidate field of the struct. It should also assign a unique ballot ID using the ballotCount global variable and increment the ballotCount global variable afterwards. Initially set mark the ballot as valid (the valid field of the ballot should be set to true). If the user enters an invalid response to any of the prompts, mark the ballot invalid (change the valid field to false).
c) Write a function called tally to tally the votes. The input to the function will be an array of ballots and it will return a struct Tally. The global variable ballotCount should be used to determine the number of ballots that are actually used in the array. The function should go through the array of ballots, if a ballot is valid it should update the tallies for the prop and the candidates. Note, you can use the enum field candidates in the ballot to index into the candidateVotes array in the tally in order to update the tally for the candidate selected on that ballot. Note that you’ll need to initialize the array of ballots with some test values and initialize the global variable ballotCount to correspond with the number of ballots.
Functions should have the following definitions before the function:
enum Candidates Daffy, Daffy 0 Mickey Mickey 1 Bugs Bugs Minnie Minne typedef struct Ballot struct t bool valid int ballot id bool prop 75; enum Candidates candidate Ballot typedef struct Tally struct int num Votes; int prop75Yesvotes; int candidatevotes [4]; Tally in ballotCount 0; Total number of ballots assigned. When a vote is cast, this should be incrementedExplanation / Answer
#include<stdio.h>
#include<stdbool.h>
enum Candidates{
Daffy,
Mickey,
Bugs,
Minnie,
Invalid // new enum for wrong casting of votes
};
typedef struct Ballot_struct{
bool valid;
int ballot_id;
bool prop75;
enum Candidates candidate;
}Ballot;
typedef struct Tally_struct{
int numVotes;
int prop75YesVotes;
int candidateVotes[4];
}Tally;
int ballotCount = 0;
bool win(int voteCast, int forVotes){
if(voteCast < forVotes){
printf("Wrong data : voteCast should be greater or equal to forVotes ");
return false;
}
if( (((double)forVotes/voteCast) * 100) > 50.0){
return true;
}
else
return false;
}
Ballot vote(){
int casted_vote;
Ballot new_ballot;
printf(" *** prop75 *** 0.Daffy 1.Mickey 2.Bugs 3.Minnie ");
printf("Enter your vote : ");
scanf("%d",&casted_vote);
if(casted_vote >= 4){
new_ballot.valid = false;
new_ballot.ballot_id = ballotCount++;
new_ballot.prop75 = true;
new_ballot.candidate = Invalid;
}
else{
new_ballot.valid = true;
new_ballot.ballot_id = ballotCount++;
new_ballot.prop75 = true;
new_ballot.candidate = casted_vote;
}
return new_ballot;
}
Tally tally(Ballot *ballots){
int i = 0,valid_vote=0,total_vote = 0;
Tally total_tally;
total_tally.candidateVotes[0] = 0;
total_tally.candidateVotes[1] = 0;
total_tally.candidateVotes[2] = 0;
total_tally.candidateVotes[3] = 0;
for (i = 0; i < ballotCount; i++){
total_vote++;
if(ballots[i].valid == true){
valid_vote++;
total_tally.candidateVotes[ballots[i].candidate]++;
}
}
total_tally.numVotes = total_vote;
total_tally.prop75YesVotes = valid_vote;
return total_tally;
}
int main(){
printf(" *** Voting System *** ");
int want_vote;
Ballot ballots[100]; // currently maximum 100 votes can be casted, if you need more increase array value or create dynamic array of ballots
while(1){
printf("Want to vote press 1 : ");
scanf("%d",&want_vote);
if (want_vote == 1){
ballots[ballotCount-1] = vote();
}
else break;
}
Tally final_tally = tally(ballots);
int i = 0,won=0;
while(i<4){
if (win(final_tally.prop75YesVotes, final_tally.candidateVotes[i])){
won = 1;
switch (i){
case 0:
printf("Daffy won ");
break;
case 1:
printf("Mickey won ");
break;
case 2:
printf("Bugs won ");
break;
case 3:
printf("Minnie won ");
break;
}
}
i++;
}
if(won == 0) printf("No one got votes more than 50%% ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.