Question

No. 2 (35%)-array and record Following data is the number of votes each candidate on an election. Write a program to output each candidates 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 a table-like format): names of candidate, votes received, % of total votes, and the winner of the election at the end of line of the format above. The program consists of 1 header file (vote.h), 2 source files (main.cpp and vote.cpp). The project name: electionvote. The functions are : void read(.) - to read a data into the array, void winner(...), and please provide any functions based on your idea.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.cpp

#include <iostream>

#include <fstream>

#include <iomanip>

#include "vote.h"

using namespace std;

int main()

{

      

ifstream fin;

fin.open("votes.dat");

if(!fin.is_open())

  {

   cout<<"Unable to open the file: "<<endl;

   return 0;

  }

string names[10];

int votes[10];

int count = 0;

while(!fin.eof())

  {

   string name1, name2;

   fin>>name1>>name2>>votes[count];

   names[count] = name1 + " " + name2;

   count++;

  }

double percentages[10];

calcPercentages(votes, percentages, count);

printVotingDetails(names, votes, percentages, count);

int in=findWinnerIndex(votes, count);

cout<<" the winner is "<<names[in]<<endl;

}

Vote.h

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

void calcPercentages(int votes[], double percentages[], int count)

{

double sum = 0;

for(int i = 0; i < count; i++)

  sum += votes[i];

for(int i = 0; i < count; i++)

  percentages[i] = votes[i] * 100 / sum;

}

void printVotingDetails(string names[], int votes[], double percentages[], int count)

{

cout<<setw(-40)<<"Name"<<setw(20)<<"Number of votes"<<setw(40)<<"Percentage of Votes"<<endl;

cout<<"*******************************************************************"<<endl;

for(int i = 0; i < count; i++)

  cout<<setw(30)<<names[i]<<left<<setw(20)<<votes[i]<<fixed<<setprecision(2)<<percentages[i]<<"%"<<endl;

}

int findWinnerIndex(int votes[], int count)

{

int index = 0;

for(int i = 0; i < count; i++)

  if(votes[i] > votes[index])

   index = i;

return index;

}

Votes.dat

Allan johnson 5009

Bea Miller 4500

peter Duffy 6533

Robinson Cruso 2500

Mark Ashtony 1845

Bondita Rodriguez 3432

Output

Name Number of votes Percentage of Votes Bea Miller peter Duffv Robinson Crusc Mark Ashtony Bondita Rodriguez Allan johnson5009 4500 6533 2500 1845 3432 21.03% 18. 89% 27 . 43% 10 . 50% 7 .75% 14 . 41% the winner is peter Duffv

Add a comment
Know the answer?
Add Answer to:
No. 2 (35%)-array and record Following data is the number of votes each candidate on an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ Votes received by the candidate. Your program should also output the winner of the election....

    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....

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    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...

  • Using C language, write a program for the following: In a student representative council election, there...

    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...

    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,...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    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...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • In C program Consider a file that contains employee records, called "empstat.txt." The data for each...

    In C program Consider a file that contains employee records, called "empstat.txt." The data for each employee consist of the employee’s last name (up to 20 characters), employee id number (up to 11 characters), gross pay for the week (double), taxes deducted (double) for the week, and net pay (double) for the week. Create a struct to hold the employee information. Write a void function called writeEmpFile that prompts the user to enter last name, id number, gross pay and...

  • For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the mon...

    For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the monitor: the average score and the total number of scores. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file 2 of 2 //postcondition: inFile is opened. If inFile cannot...

  • [C++] Outline: The movie data is read from a file into an array of structs and...

    [C++] Outline: The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.   Requirements: Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT