ere is my code: char candidate[20][20]; int avg; float csum; int size; size = si
ID: 3667552 • Letter: E
Question
ere is my code:
char candidate[20][20];
int avg; float csum;
int size;
size = sizeof(votes) / sizeof(votes[0]);
float sum = 0;
float percent[6];
do
{
vote1++;
cout << "Please enter the total number of votes for each candidate [press-1 to stop]: " << endl;
cin >> votes[vote1];
} while (votes[vote1] != -1);
cin.ignore();
for (int x = 0; x < vote1; x++)
{
cout << "Enter the last names of the 5 candidates: " << endl;
cin.getline(candidate[x], 30);
}
for (int i = 0; i < size; i++) {
sum += votes[i];
csum = sum + 1;
}
for (int i = 0; i < 6; i++) {
percent[i] = ((votes[i] * 100.0) / csum);
}
cout << "CANDIDATE" << setw(30) << "VOTES RECIEVED" << setw(30) << "% of TOTAL VOTES" << endl;
cout << "----------------------------------------------------------------------" << endl;
for (int x = 0; x < vote1; x++)
{
cout << setw(30) << left << candidate[x] << setw(30) << fixed << setprecision(2) << votes[x] << setw(30) << fixed << setprecision(2) << percent[x]
<< endl;
}
cout << "Total: " << csum << endl;
cout << endl;
for (int i = 1; i < percent[i]; ++i) /* Loop to store largest number to arr[0] */
{
if (percent[0] < percent[i])
percent[0] = percent[i];
}
cout << "The winner of the election is " << percent[0] << endl;
}
I must print out the name of the person with the most votes: How can I connect the canidate and votes array so that I can
print a statement with the name of the candidate thathas the most votes instead of the mav vaalue.
The final statement should look like this: The winner of the election is Duffy.
Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
Explanation / Answer
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
int main()
{
int NUMBER_OF_CANDIDATE = 5;
char candidate[NUMBER_OF_CANDIDATE][30];
int votes[NUMBER_OF_CANDIDATE];
float percent[NUMBER_OF_CANDIDATE];
int totalVote = 0;
int size= NUMBER_OF_CANDIDATE;
cout << "Enter the last names of the "<<size<<" candidates: " << endl;
for(int i=0; i< size; i++){
cout<<"Enter name of "<<(i+1)<<" candidate: ";
cin.getline(candidate[i], 30);
cout<<"Enter number of votes "<<(i+1)<<" candidate: ";
cin >> votes[i];
cin.ignore(); // Put this right after cin >> choice, before you go on
totalVote = totalVote + votes[i];
}
for (int i = 0; i < size; i++) {
percent[i] = ((votes[i] * 100.0) / totalVote);
}
cout << "CANDIDATE" << setw(30) << "VOTES RECIEVED" << setw(30) << "% of TOTAL VOTES" << endl;
cout << "----------------------------------------------------------------------" << endl;
for (int x = 0; x < size; x++)
{
cout << setw(30) << left << candidate[x] << setw(30) << fixed << setprecision(2)
<< votes[x] << setw(30) << fixed << setprecision(2) << percent[x]<< endl;
}
cout << "Total: " << totalVote << endl;
cout << endl;
float max = percent[0];
for (int i = 1; i < size; ++i) /* Loop to store largest number to arr[0] */
{
if (max < percent[i])
max = percent[i];
}
cout << "The winner of the election is " << max << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.