Question

In: Computer Science

1. Allow the user to vote as many times as they indicated there are voters (see...

1. Allow the user to vote as many times as they indicated there are voters (see numVoters). For each voter, you have to display the list ofcandidates and their associated id number (id numbers should start at 1, not 0). A voter votes for a candidate by entering the candidate's id number.
2. You have to devise a method for keeping tracks of the votes.
3. After all the voters have voted, you have to print out the results for each candidate.
4. Make sure your output is nicely formatted, it will be part of your grade.

The following steps will help you set up the lab. For further reference, you may want to look at Lab 1.
1. Create a new folder in your Mystorage\CISC130 folder call it Lab6.
2. In CodeBlocks make a new c file. Title it Lab6 and save it in the folder you created above.
3. Copy the code below into your Lab6.c file:
/***YOUR NAME HERE***/
#include <stdio.h>
int main()
{
//2d array that will hold the names of the candidats
char cand[100][1000];
//The following could has the user enter the the names of the
//candidates. It places a \0 character at the end of each name
printf("How many candidates are in the race? ");
//Holds the number of candidates in the race
int numCand;
scanf("%i",&numCand);
//get rid of the return from the scanf above
getchar();
//Get the names of the candidates
int row = 0;
//one name per row and go until we have the number of candidates
while(row < numCand)
{
printf("Please enter the name candidate number %i: ",row+1);
//get t he first character of the name
char cur = getchar();
int col = 0;
//while they haven't hit enter get the next character
while(cur != '\n')
{
cand[row][col] = cur;
cur = getchar();
col = col +1;
}
//add the \0 character at the end of the useful info in the array
cand[row][col]='\0';
row = row + 1;
}
//Finally ask them how many people will be voting
printf("How many people will be voting? ");
int numVoters;
scanf("%i",&numVoters);
printf("\n**********VOTING WILL COMMENCE NOW***********\n");
/*YOU WORK GOES BELOW HERE*/
}

Solutions

Expert Solution

Below is complete code. Let me know if you have any problem or doubt. Thank you.

====================================================================

/***YOUR NAME HERE***/
#include <stdio.h>
#pragma warning(disable : 4996) // for visual studio only

int main()
{
   //2d array that will hold the names of the candidats
   char cand[100][1000];
   //The following could has the user enter the the names of the
   //candidates. It places a \0 character at the end of each name
   printf("How many candidates are in the race? ");
   //Holds the number of candidates in the race
   int numCand;
   scanf("%i", &numCand);
   //get rid of the return from the scanf above
   getchar();
   //Get the names of the candidates
   int row = 0;
   //one name per row and go until we have the number of candidates
   while (row < numCand)
   {
       printf("Please enter the name candidate number %i: ", row + 1);
       //get the first character of the name
       char cur = getchar();
       int col = 0;
       //while they haven't hit enter get the next character
       while (cur != '\n')
       {
           cand[row][col] = cur;
           cur = getchar();
           col = col + 1;
       }
       //add the \0 character at the end of the useful info in the array
       cand[row][col] = '\0';
       row = row + 1;
   }
   //Finally ask them how many people will be voting
   printf("How many people will be voting? ");
   int numVoters;
   scanf("%i", &numVoters);
   printf("\n**********VOTING WILL COMMENCE NOW***********\n");
  
   // create an array to hold votes for each candidate
   int votes[100];
   // initialize it to 0 votes
   int i = 0;
   while (i < 100)
   {
       votes[i] = 0;
       i = i + 1;
   }
   // create a loop to vote till each voter finish voting
   int voted = 0;
   while (voted < numVoters)
   {
       // get index of array for voted candidate
       int index = 0;
       // create a loop to make sure votes insert valid ID
       while (index < 1 || index > numCand)
       {
           // print each candidate with ID
           printf("%-10s %s\n", "ID", "Name");
           int row = 1;
           while (row <= numCand)
           {
               printf("%-10i %s\n", row, cand[row - 1]);
               row = row + 1;
           }
           printf("Enter candidate's ID number to vote: ");
           scanf("%i", &index);
           // if voter enters invalid ID ask for vote again
           if (index < 1 || index > numCand)
           {
               printf("Invalid ID! try again\n");
           }
       }
       // update votes
       votes[index - 1] = votes[index - 1] + 1;
       voted = voted + 1;
   }
   // declare result
   printf("\n********** RESULT ***********\n");
   row = 0;
   printf("%-100s %-10s\n", "Name", "Votes");
   while (row < numCand)
   {
       // print each candidate
       printf("%-100s %-10i\n", cand[row], votes[row]);
       row = row + 1;
   }
}

====================================================================


Related Solutions

A study determines that 60% of the voters in a town intend to vote for the...
A study determines that 60% of the voters in a town intend to vote for the incumbent mayor. if a sample of 8 people is selected, approximate probability that 6 of the 8 people surveyed intend to vote for the incumbent is
34​% of a certain​ country's voters think that it is too easy to vote in their...
34​% of a certain​ country's voters think that it is too easy to vote in their country. You randomly select 12 likely voters. Find the probability that the number of likely voters who think that it is too easy to vote is​ (a) exactly​ three, (b) at least​ four, (c) less than eight.
t is election time and voters are going to the polls to cast their vote for...
t is election time and voters are going to the polls to cast their vote for their favorite candidates. A researcher was interested to study whether there is a significant difference in patterns of voting turnout among three regions: Eastern, Western and Central. She randomly selected 12 voting precincts from each region and calculated the sum of squares of the rate of participation as follows: SSB= 3,342.89 SSW= 7,265.42 Write the ANOVA formula and conduct an ANOVA test of patterns...
How would I fix my coding to allow the user to see what they have entered...
How would I fix my coding to allow the user to see what they have entered after they submit it? Where would I plug it into my code to see it? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="../signintrial.css"> <script> function loginHandler(){ // code for handing login event } function registerHandler() {    //checking phone number    if(phonenumber(document.getElementById('phone').value))    {    //when phone number is valid    document.getElementById('demo').innerHTML = document.getElementById('fname').value + " "...
A recent study indicated of 50 cdddvcvvo people indicated that 53.5% of people vote. Test the...
A recent study indicated of 50 cdddvcvvo people indicated that 53.5% of people vote. Test the Hypothesis at the α =.05 level that the proportion of people that vote is &gt; 50%
Assume that 6% of voters are undecided about who to vote for in a national election....
Assume that 6% of voters are undecided about who to vote for in a national election. Among undecideds 20% are registered to a party and 80% are unaffiliated. Among decided voters 30% are unaffiliated. What is the probability that a person is undecided for this election given that he/she is unaffiliated? What is the probability that a person is decided given that he/she is registered to a party ?
Two candidates A and B are running for election. There are 5 voters who vote for...
Two candidates A and B are running for election. There are 5 voters who vote for one of the two candidates (no abstention). The candidate with most votes wins. A majority of voters prefer candidate A over candidate B. a) Describe formally this environment as a game (players, strategies, payoffs) b) Show that to vote for your preferred candidates is a weakly dominant strategy. c) Show that there exist Nash equilibria in which candidate B gets elected and that most...
In a survey of 500 likely voters, 271 responded that they would vote for the incumbent...
In a survey of 500 likely voters, 271 responded that they would vote for the incumbent and 229 responded that they would vote for the challenger. Let p denote the fraction of all likely voters who preferred the incumbent at the time of the survey, and let p̂ p^ be the fraction of survey respondents who preferred the incumbent. a. Construct a 95% confidence interval for pp. Keep in mind that you should calculate the standard error making no assumptions...
Make a program that lets the user enter a positive or negative integer as many times...
Make a program that lets the user enter a positive or negative integer as many times as they want. java //import statement //class header //Begin class scope //Method header for main. //Begin method scope. //Declare input object for Scanner. //Declare variable called entry initialized to zero. //Declare variable called response initialized to blank space. //Prompt "Do you want to enter an integer? Y or N: " //Store value in correct variable.   //while header that tests uppercase in response //Begin scope...
A recent study suggested that 70% of all eligible voters will vote in the next presidential...
A recent study suggested that 70% of all eligible voters will vote in the next presidential elections. Suppose 20 eligible voters were randomly selected from the population of all eligible voters. Use a binomial probability table to find the probability that more 10 but fewer than 16 of the 20 eligible voters sampled will vote in the next residential election. The answer is 0.714 but I would like to see the workings.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT