Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your program, you will complete code that finds the winner of an election. The program gets the names of the candidates and the number

For your program, you will complete code that finds the winner of an election. The program gets the names of the candidates and the number of votes each candidate received from file votingresults.txt. It then calculates the total number of votes and prints out the percentage of votes each candidate received. Finally, the program reports which candidate won the election. You need to write code in the main function, and complete two other functions. Refer back to the example program for hints about properly getting user input. You can also refer to the example program from the Arrays lab for hints about how to find the index of the maximum value in the array.? FOLLOWING IS THE MAIN FUNCTION TO MADE MODIFICATIONS TO:

#include #include #include #include using namespace std;

int sumList(const int list[], int size); int indexOfMax(const int list[], int size); // This function gets the results from the file and returns them in the corresponding void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count); void printResults(const string candidates[], const int ballots[], int count, int totalBallots); const int NUM_CANDIDATES = 10; // The number of candidates

int main() { ifstream inFile; string names[NUM_CANDIDATES]; // The names of the candidates int votes[NUM_CANDIDATES]; // The number of votes received by each candidate int totalVotes; // The total number of votes received by all candidates int max; // number of candidates processed

inFile.open("votingresults.txt"); if (!inFile) { cout

/////////////////////////////// // Start of your code

// Get the candidates' names and number of votes each received // Assume the user enters valid input (just call the getElectionData() function)

// Calculate the total number of votes received by all candidates // (call the sumList() function)

// Print each candidate's name, the number of votes she received, // and the percent of votes the candidate received // (just call the printResults() function)

// End of your code in the main function // Don't forget to complete the functions below ///////////////////////////////

return 0; }

// // Returns the sum of the numbers in the given array // // WRITE THE BODY OF THIS FUNCTION // int sumList(int list[], int size) {

}

// // Returns the index of the largest value in the given array (not the largest // value, but the index of it) // // WRITE THE BODY OF THIS FUNCTION // int indexOfMax(int list[], int size) {

}

// Gets the names of the candidates and the votes they got from the file and returns // them in the corresponding arrays. It also returns the number of rows in the file that // were processed // // WRITE THE BODY OF THIS FUNCTION // void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count) {

}

// Receives the candidates names and the quantity of votes they got, the number of elements // in the arrays to be processed, and the total number of votes so they can be displayed as // shown in the file with the problem statement // Must call the indexOfMax function() to find the array index of the candidate with the // largest number of votes // // WRITE THE BODY OF THIS FUNCTION // void printResults(const string candidates[], const int ballots[], int count, int totalBallots) {

} (THE FILE'S CONTENTS ARE:) ALVAREZ 8532 MORRISON 7176 HANSEN 6690 FERNANDEZ 9432 GARZA 8648 HARVEY 9330 /// THE SAMPLE OUTPUT OF THE PROGRAM : image text in transcribed?Output must look exactly like picture using code, thanks!!

. C:Windowslsystem321cmd.exe Candidate ALVAREZ MORRISON HANSEN FERNANDEZ GARZA HARVEY Num Votes 8532 7176 6690 9432 8648 9330 % of Votes 17.13 14.41 13.43 18.94 17.36 18.73 The winner is: FERNANDEZ Press any key to continue

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

Students also viewed these Databases questions