Question
Write a C++ program which has three arrays: candidates, votes and percents. They should be declared as string, int and double respectively and all should
Write a C++ program which has three arrays: candidates, votes and percents. They should be declared as string, int and double respectively and all should be of size 25. Your program should use a function to read the file candidates.txt into the array, candidates. The program should then pass the votes array to a function which will generate random numbers between 1500 and 25000 to fill the votes array. A third function (to which you will pass the votes and percents arrays) will determine the total number of votes for all candidates and then calculate the percentage for each candidate and fill the percents array. Then pass all three arrays to a function which will print the names of the candidates, their vote totals and percentages and print out the election results, eg:
Candidate Votes Percent
Rubio 8905 4.5%
Bush 8948 4.5%
Christie 4239 2.2%
Paul 13082 6.7%
O'Malley 13293 6.8%
Cruz 14463 7.4%
Clinton 17173 8.7%
Trump 4644 2.4%
Kasich 5542 2.8%
Sanders 1911 1.0%
Carson 16136 8.2%
Gilmore 14019 7.1%
Fiorina 4713 2.4%
Santorum 19235 9.8%
Huckabee 16742 8.5%
Graham 13048 6.6%
Jindal 9300 4.7%
Walker 5688 2.9%
Perry 5597 2.8%
The winner is Santorum with 19235 votes!
Your results will be different since we are using random numbers.
Here is a list of the function prototypes: bool readFile(string c[], int &size);
void getVotes(int v[], int size);
void calculatePercents(int v[], double p[], int size);
void displayResults(string c[], int v[], double p[], int size);
Note that we pass the size of the array to readfile as a reference parameter. In main, set size to CAPACITY which is the const value which sets the size of the arrays. After reading the file into the array, set size to i so that when you pass it to the other functions it will indicate the actual number of candidates, NOT the size of the array.
candidates.txt
Rubio Bush Christie Paul O'Malley Cruz Clinton Trump Kasich Sanders Carson Gilmore Fiorina Santorum Huckabee Graham Jindal Walker Perry
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started