Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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: boolreadFile(stringc[],int&size);

voidgetVotes(intv[],intsize);

voidcalculatePercents(intv[],doublep[],intsize);

voiddisplayResults(stringc[],intv[],doublep[],intsize);

Note that we pass the size of the array to readfile as a reference parameter. Inmain, set size to CAPACITY which is theconstvalue 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

My code is the code that follows, but there's something wrong with it. Everytime I run it in my compiler, it comes out blank, and I can't quite figure it out. Can anyone help me?

#include #include #include #include #include

using namespace std;

bool readFile(string c[], int *); void calculatePercents(int v[], double p[], int ); void displayResults(string c[], int v[], double p[], int); void getVotes(int v[], int );

int main() {

int n=25; string c[n]; int v[n]; double p[n];

readFile(c, &n);

getVotes(v, n);

calculatePercents(v, p, n);

displayResults(c, v, p, n);

return 0; }

void getVotes(int v[], int size) { int min =1500; int max=25000; srand(time(0));

for(int i=0;i

void displayResults(string c[], int v[], double p[], int size) { cout<<" Candidates Votes \tPercent ";

int max=v[0]; int pos=0;

for(int i=0; i < size;i++) { if (max < v[i]) { max=v[i]; pos=i; }

cout<<" "<

cout<<"\t "<

}

cout << endl << "Winner is " <

}

void calculatePercents(int v[], double p[], int n) { int sum=0; int min =1500; int max=25000; srand(time(0));

for (int i=0;i

v[i]=(int)min + (rand() % ( max - min + 1 )); sum+=v[i]; }

for (int i=0;i

bool readFile(string c[], int *size) { string candidate;

ifstream fin;

fin.open("/storage/emulated/0/C++/candidates.txt");

int i=0;

while (i < *size) while (fin >> candidate) { cout << candidate; c[i] = candidate; i++; }

*size = i; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions