In: Computer Science
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*/
}
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;
}
}
====================================================================