please explain your code In instant runoff voting ( single candidate. If no cand
ID: 3737683 • Letter: P
Question
please explain your code
In instant runoff voting ( single candidate. If no candidate has received a majority of the first-place votes, the last- place candidate is removed, and that candidate's votes are reassigned to the next preference of the voter. For example, if you have the following votes for 5 candidates by 7 voters where the candidates are CA, CB, CC, CD, and CE: IRV), voters rank their choice of candidates instead of choosing a Voter 1: CA, CB, CC, CD, CE Voter 2: CA, CB, CC, CD, CE Voter 3: CA, CB, CC, CD, CE Voter 4: CC, CB, CA, CD, CE Voter 5: CB, CC, CD, CE, CA Voter 6: CB, CC, CD, CE, CA Voter 7: CB, CC, CD, CE, CA Both CA and CB have 3 first-place votes, but neither have a majority. So, we remove the last-place candidate. Neither CD or CE received any votes, so we will remove both of them. When their votes are reassigned, we get the following in the 2nd round: Voter 1: CA, CB, CC Voter 2: CA, CB, CC Voter 3: CA, CB, CC Voter 4: CC, CB, CA Voter 5: CB, CC, CA Voter 6: CB, CC, CA Voter 7: CB, CC, CA We still have CA and CB with 3 first-place votes, but no majority. The last-place candidate now is CC, and we reassign its votes for round 3: Voter 1: CA, CB Voter 2: CA, CB Voter 3: CA, CB Voter 4: CB, CA Voter 5: CB, CA Voter 6: CB, CA Voter 7: CB, CA Now, CB has 4 votes and CA has 3 votes, and since 4 votes is a majority, CB is declared the winner after round 4Explanation / Answer
ANS:-
Given that,
In instant runoff voting ( single candidate. If no candidate has received a majority of the first-place votes, the last- place candidate is removed, and that candidates votes are reassigned to the next preference of the voter.
PROGRAM:-
finding leader.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
// Method to sort the array
void sortArr(int *Unarray,int size){
int temp;
for(int i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(Unarray[i] > Unarray[j])
{
temp = Unarray[i];
Unarray[i] = Unarray[j];
Unarray[j] = temp;
}
}
}
}
//Method to delete the smallest number in the array
int * remove_copy(const int *in, size_t n, int *out, int value)
{
for (size_t i = 0; i != n; i++)
{
if (in[i] != value) *out++ = in[i];
}
return out;
}
//Method to find the smallest number in the array
int returnSmallest(int *array){
int size = sizeof(array) / sizeof(*array);
int minimum = array[0];
int location = 0;
for (int c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
return minimum;
}
// main function
int main(int argc, char **argv)
{
// Checking if the parameters are passed properly
if(argc !=2){
printf("%s filename ",argv[0]);
printf(" where filename is name of votes file ");
exit(0);
}
char *filename = argv[1];
int *votes;
int n,file_length=0,CA=0,CB=0,CC=0,CD=0,CE=0;
int temp;
int candidateSize = 5;
// getting the file
FILE *fp;
printf("Enter the number of voters");
scanf("%d", &n);
votes = (int*) calloc(n * candidateSize, sizeof(int));
fp=fopen(filename, "r"); // Opening file
while(fscanf(fp, "%s", temp)!=EOF){ // Reading the file
votes[file_length] = temp; // assigning the numbers to votes list
file_length++;
}
fclose(fp);
char Y = 'y';
while(Y == 'y'){ // Loop to iteratively find the leader
for(int i = 0;i <candidateSize ;i++){ // Loop to find the number of votes
if(votes[5*(i=1)]== 1){
CA++;
}
else if(votes[5*(i=1)]== 2){
CB++;
}
else if(votes[5*(i=1)]== 1){
CC++;
}
else if(votes[5*(i=1)]== 1){
CD++;
}
else if(votes[5*(i=1)]== 1){
CE++;
}
}
int votearr[] = {CA,CB,CC,CD,CE};
// sortArr(votearr);
// Checking the winning candidate
if(candidateSize==1){
printf("The winner is : %d", votearr[0]);
Y = 'n';
continue;
}
else if(candidateSize==2){
if(votearr[0]==votearr[1]){
printf("The winners are : %d and %d", votearr[0],votearr[1]);
Y = 'n';
continue;
}
}
else if(candidateSize==3){
if(votearr[0]==votearr[1] && votearr[1]==votearr[2]){
printf("The winners are : %d and %d and %d", votearr[0],votearr[1],votearr[2]);
Y = 'n';
continue;
}
}
else if(candidateSize==4){
if(votearr[0]==votearr[1] && votearr[1]==votearr[2] && votearr[0]==votearr[3] ){
printf("The winners are : %d and %d and %d and %d", votearr[0],votearr[1],votearr[2],votearr[3]);
Y = 'n';
continue;
}
}
else if(candidateSize==5){
if(votearr[0]==votearr[1] && votearr[1]==votearr[2] && votearr[0]==votearr[3] && votearr[0]==votearr[4] ){
printf("The winners are : %d and %d and %d and %d and %d", votearr[0],votearr[1],votearr[2],votearr[3],votearr[4]);
Y = 'n';
continue;
}
}
// Method to remove the smallest element of the array
int *last = remove_copy(votearr, candidateSize, votearr, returnSmallest(votearr));
candidateSize = sizeof(votearr) / sizeof(*votearr); // calculating the array size.
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.