Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In approval voting each voter specifies which candidates are acceptable. The win

ID: 3936726 • Letter: I

Question

In approval voting each voter specifies which candidates are acceptable. The winning candidate is the one who is voted acceptable on the most ballots.

For example, suppose seven votes submit these ballots:

Moe Curly

Curly Larry

Larry

Gerry Larry Moe

Larry Moe Gerry Curly

Gerry Moe

Curly Moe

Here are the vote totals for the candidates, in alphabetical order.

Curly                      4

Gerry                    3

Larry                      4

Moe                      5

Here are the vote totals for the candidates, in decreasing order of vote totals.

Moe                      5

Curly                      4

Larry                      4

Gerry                    3

Implement a program to process approval voting ballots. The program should

1. prompt for the name of a text file that contains the ballots, one per line,

2. read the ballots,

3. display the vote totals with two tables as in the example above.

Be sure that your software is documented and thoroughly tested. Submit you work as a jar file.

Explanation / Answer

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node{
   char name[24];
   int num;
   struct node *next;
};

void splitLinkedLists(struct node *head, struct node **a, struct node **b)
{
   struct node *onestep, *twosteps;
   if (head == NULL || head->next == NULL)
   {
       *a = head;
       *b = NULL;
   }
   else
   {
      >        twosteps = head->next;
       while (twosteps != NULL)
       {
           twosteps = twosteps->next;
           if (twosteps != NULL)
           {
              >                twosteps = twosteps->next;
           }
       }
       *a = head;
       *b = onestep->next;
       onestep->next = NULL;
   }
}

struct node * mergeLinkedLists(struct node *a, struct node *b)
{
   struct node* result = NULL;
   if (a == NULL)
       return(b);
   else if (b == NULL)
       return(a);
   if (a->num >= b->num)
   {
       result = a;
       result->next = mergeLinkedLists(a->next, b);
   }
   else
   {
       result = b;
       result->next = mergeLinkedLists(a, b->next);
   }
   return(result);
}

void mergeSort(struct node **head)
{
   struct node *a, *b, *start;
   start = *head;
   if (start == NULL || start->next == NULL)
   {
       return;
   }
   splitLinkedLists(start, &a, &b);
   mergeSort(&a);
   mergeSort(&b);
   *head = mergeLinkedLists(a, b);
}

int main()
{
   int i,ch;
   struct node *first, *temp = 0, *head, *t;
   first = 0;
   while (1)
   {
       while (1)
       {
           head = (struct node *)malloc(sizeof(struct node));
           printf("Name of the candidate:");
           scanf("%s", head->name);
           head->next = NULL;

           if (first == 0)
           {
               head->num = 1;
               first = temp = head;
           }
           else
           {
               t = first;
               int key = 0;
               while (t!=NULL)
               {
                   if (strcmp(t->name, head->name) == 0)
                   {
                       t->num += 1;
                       key = 1;
                       break;
                   }
                   else
                   {
                       t = t->next;
                   }
               }
               if (key != 1)
               {
                   head->num = 1;
                   temp->next = head;
                   temp = head;
               }
           }
           printf("wan't to continue[1/0]:");
           scanf("%d", &ch);
           if (!ch)
               break;
       }
       printf("Want to vote[1/0]:");
       scanf("%d", &ch);
       if (!ch)
           break;
   }
   temp = first;
   while (temp!=NULL)
   {
       printf("%s %d ", temp->name, temp->num);
       temp = temp->next;
   }
   mergeSort(&first);
   temp = first;
   while (temp != NULL)
   {
       printf("%s %d ", temp->name, temp->num);
       temp = temp->next;
   }
   getchar();
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote