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

C Programming : The citizens of the nation Atlantis will soon be taking to the p

ID: 3801910 • 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

Restrictions:

1. Your program should include a header and other comments.

2. You may assume that the number of states is no more than 30

3. You may assume that the number of parties is no more than 10

4. You may assume that all of the numbers in the file are valid

5. You may assume that there is always a single winning party

6. When run, your program should format the output text as shown in the sample output, although the numbers may be different.

Sample input – votes.txt

3

4

20 34 24 16

13 36 89 20

27 80 76 46

Sample Output - results.txt

Election Results

----------------------------------------------------

State1 was won by Party C

State2 was won by Party C

State3 was won by Party B

State4 was won by Party C

Party C has won the election, winning 3 states.

Explanation / Answer

/*Each line is provided with proper explaination and logic.

For outline, it takes input, stores into 2d array, travers columnwise to find party with max votes, and stores in another array. Now prints the results and also count which party won in max number of states. print the final results back into the file results.txt*/

#include <stdio.h>//header files
#include <stdlib.h>


int main()//main function
{
   FILE * f;//declaring the file pointer
   FILE * resultfile;//declaring the file pointer for result file
   f = fopen("voters.txt","r"); //opening the file in read mode
   resultfile = fopen("results.txt","w");//opeining the result file in write mode as we need to write in that
  
   int i , j;//flag variables

   int states;//number of state
   int parties; //number of parties

   fscanf(f, "%d", &parties);
   /*reading from the file using fscanf. The three arguments are f(file pointer) , "%d" as we need to read the integer, name of the variable into which values scanned will be stored*/
   fscanf(f, "%d", &states);
   /*Similar to scanning number of states*/
   int results[parties][states];

   //the result array stores the array of votes with rows being the number of parties and columns being number of states.
   /*
   From the question given below, the value of 2d array will be
   20 34 24 16
   13 36 89 20
   27 80 76 46


   */

   for(i=0;i<parties;i++)//traverse through the
   {
       for(j=0;j<states;j++)//traverse through the columns
           fscanf(f, "%d", &results[i][j]);//store the results
   }

   /*the above loop stores the array in the way explained above.Now to find the party with maximum number of votes per state, we need to travel column wise from top to down and find the party with the maximum votes and maximum results*/

   int state_results[states];//the results are stored in this array, it stores the integral value of the party that won the election like 0 for A , 1 for B and so on

   int max_vote;// this stores the integral value of votes won by the party
   int max_party;//this stores the integral vaue of the party that has maximum votes till now in a state


   for(i=0;i<states;i++)//travel through all states
   {
       max_vote=-1;//initializing the variable so that when we compare it with any number of votes, it gets reinitialized

       for(j=0;j<parties;j++)//traveling through all parties
       {
           if(results[j][i]>max_vote)//comparing the maximum votes that a party has won with max found tillnow
           {
               max_party=j;//if max till now is lesser than current value, update max party and max till now
               max_vote=results[j][i];
           }


       }

       state_results[i]=max_party;//finally updating the party with maximum votes and restarting the process for the next state

   }


   int count[parties];//stores the count of number of states where a particular party has won the elections like in the case above it will be 1 for A 0 for B and 3 for C

   for(i=0;i<parties;i++)//initializing to zero
   count[i]=0;

   for(i=0;i<states;i++)
   {
       fprintf(resultfile,"State %d was won by party %c ",i+1,state_results[i]+'A');//printing the result per state into the file. Remember fprintf is similar to fscanf. Just that inplace of reading, it writes into the file. Has the same number of arguments.
       /*here we had stored 0 for A 1 for B and so on. To print A we need to add the ascii value of A, hence we have done 'A' + state_results[i]*/
       count[state_results[i]]++;//raising the count of party for wining election in one more state
   }

   int max_countp;//stores the count of state


   int max_countv=-1;//stores the count of states that won in max number of states

   for(i=0;i<parties;i++)
   {
       if(count[i]>count[max_countp])
       {
           max_countp=i;//finding the party with wins in max number of state

       }

   }

   fprintf(resultfile,"Party %c has won the elections winning %d States ",max_countp+'A',count[max_countp]);//printing the final number of states into the file

   fclose(resultfile);
   fclose(f);//closing the file pointer and closeing the program as well
   return 0;
}