COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic array of structure to keep track the votes and candidate names. You will ask user for number of candidates and create one dynamic array of structure to keep track the votes for each candidate, and the names of each candidate. The programs will allow user to enter the last name of each candidate, and the number of votes each candidate received. The program should then output the summary of the voting result (see sample output below). You will design the necessary functions in your program to accomplish the tasks. File Name: FirstName_LastName_Poll.cpp The structure is defined as follows: struct Poll { string lastName; int votes; }; Functions in the program: Function name Return type Parameter list and purpose getSize int ()->parameter list is empty Prompt user to enter total number of candidates and return the size (size cannot be negative). makePollArray Poll* (int size)->parameter list Create a dynamic array of Poll and return a pointer to the array of Poll. getPollInfo void (Poll *poll, int size)->parameter list Prompt users to enter candidate last name and votes for all candidates (votes cannot be negative) calcMaxIndex int (Poll *poll, int size)->parameter list Return the array index which contains the most votes among candidates. (Assume no two candidates receive the same votes) calcTotalVotes int (Poll *poll, int size)->parameter list Return total votes for all candidates displayResults void (Poll *poll, int size)->parameter list Display each candidate with their votes, percentage to the total cotes, total votes and the winner of the election. See sample output Assignment Instructions: 1. Create a Visual Studio or Xcode project (Mac user) and name it ProjectAssignment2. 2. Add a cpp file and name it FirstName_LastName_Poll.cpp. This file contains all the functions above. COSC 1437 C++ Project Assignment 2 Poll Summary 3. In your main function, call getSize(), makePollArray(), getPollInfo(), and displayResults() functions. In displayResults() function, call calcTotalVotes() and calcMaxIndex() functions. 4. Use the sample output data to test your code. 5. You can create extra functions to enhance your program. 6. Name your variables clearly. 7. Add comments to document your project and code. 8. Add space/indentation to make your code easier to read. 9. Submit your cpp file and a screen shot of your output in Canvas for grading. Sample Program Output: Candidate Votes Received % of Total Votes Miller 5000 25.91 Johnson 4000 20.73 Robinson 6000 31.09 Smith 2500 12.95 Jones 1800 9.33 Total 19300 The winner of the Election is Robinson.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
#include<iomanip>
using namespace std;
struct Poll { string lastName; int votes; };
//method to get a valid number of candidates from user
int getSize(){
int n;
//prompting and reading size
cout<<"Enter total number of candidates: ";
cin>>n;
//validating
if(n>0){
//valid, returning it
return n;
}
//otherwise, invalid, displaying error message
cout<<"Sorry, please enter a valid size"<<endl;
//asking again and returning it (simple recursion)
return getSize();
}
//method to create a Poll array dynamically and return it
Poll * makePollArray(int size){
Poll* polls=new Poll[size];
return polls;
}
//method to read data for all candidates and fill the poll array
void getPollInfo(Poll *poll, int size){
string name;
int votes;
int i=0;
//looping and reading data
while(i<size){
cout<<"Enter last name of candidate: ";
cin>>name;
cout<<"Enter number of votes received by "<<name<<": ";
cin>>votes;
//validating votes
if(votes>=0){
//valid, assigning to current poll element
poll[i].lastName=name;
poll[i].votes=votes;
//moving to next candidate
i++;
}else{
//error, asking to try again
cout<<"Error! number of votes cant be negative. Try again"<<endl;
}
}
}
//returns the index of candidate with maximum votes
int calcMaxIndex (Poll *poll, int size){
int max=-1;
for(int i=0;i<size;i++){
//if max is still -1 or current person has more votes, then updating max
if(max==-1 || poll[i].votes > poll[max].votes){
max=i;
}
}
return max;
}
//sums and returns the total number of votes
int calcTotalVotes (Poll *poll, int size){
int sum=0;
//looping and summing votes
for(int i=0;i<size;i++){
sum+=poll[i].votes;
}
return sum;
}
//method to display results
void displayResults (Poll *poll, int size){
//finding total
int total=calcTotalVotes(poll,size);
double percent;
//aligning output to left, using a fixed precision of 2 digits
cout<<left<<setprecision(2)<<fixed;
//displaying heading
cout<<endl<<setw(15)<<"Candidate"<<setw(15)<<"Votes Received"<<"% of total votes"<<endl;
//looping and displaying details of all candidates
for(int i=0;i<size;i++){
//finding percentage
percent=(double)poll[i].votes/total;
percent*=100;
//displaying name, vote and percentage
cout<<setw(15)<<poll[i].lastName<<setw(15)<<poll[i].votes<<percent<<endl;
}
//displaying total and final winner
cout<<"Total "<<total<<endl;
cout<<"The winner of the Election is "<<poll[calcMaxIndex(poll,size)].lastName<<endl;
}
int main(){
//reading size
int size=getSize();
//creating dynamic array
Poll* p=makePollArray(size);
//reading input
getPollInfo(p,size);
//displaying results
displayResults(p,size);
//de allocating memory
delete[] p;
return 0;
}
/*OUTPUT*/
COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...
No. 2 (35%)-array and record Following data is the number of votes each candidate on an election. Write a program to output each candidate's name, the number of votes received, and the percentage of the total votes received by the name. The data: (write into a file, named "votes.dat") Candidate Votes Received 5009 4500 6533 2500 1845 Allan Johnson Bea Miller Peter Duffy Robinson Cruoso Mark Ashtony Bondita Rodriguez 3432 The output of this program consists of following information (into...
(Write a program for C++ )that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91...
c++ Votes received by the candidate. Your program should also output the winner of the election. A sample output is: The Winner of the Election is Duffy. The input data should come from the file. Use 3 separate arrays to store the names, votes, and percentages before they are displayed. Include functions inputValues to input values for candidate names and votes received and place them in arrays. calcPercents which fills an array with the percentage of votes each candidate got....
Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...
COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see which professor has the highest rating according to student input. You will create a ProfessorRating class consisting of professor Name and three ratings. The three ratings are used to evaluate easiness, helpfulness, and clarity. The value for each rating is in the range of 1 to 5, with 1 being the lowest and 5 being the highest. Your program should contain the following functionalities:...
(2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...
Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...
Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. HOW TO DO? PLEASE READ CAREFULLY. MAX_SIZE is size of array When iterating over the candidates’ list, do not iterate over the entire array, but just over the records where data is filled in void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line,...
It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...