C++ #include <iostream> #include <iomanip> #include <string> using namespace std
ID: 3729576 • Letter: C
Question
C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
// declare array varaibles
int totalVotes;
int i;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by "
<<"the candidate." << endl;
// load data into arrays
// set totalVotes
cout << "Candidate Votes Received % of Total Votes" << endl;
// output data
cout << "Total " << totalVotes << endl;
// output winner
return 0;
}
int sumVotes(int list[], int size)
{
// function code
}
int winnerIndex(int list[], int size)
{
// function code
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main()
{
// declare array varaibles
int totalVotes;
int i;
char voterNames[5][10];
float votePer;
int numbers[5];
int sum = 0;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by "
<<"the candidate." << endl;
// load data into arrays
for(int i=0;i<5;i++){
cin >> voterNames[i];
}
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
//sum += numbers[i];
}
// load data into arrays
// set totalVotes
totalVotes = sumVotes(numbers,5);
cout << "Candidate Votes Received % of Total Votes" << endl;
for (int i = 0; i < 5; ++i)
{
votePer= (numbers[i]/totalVotes)*100;
cout << voterNames[i] << " " << numbers[i] << " " << votePer << endl;
}
// output dat
cout << "Total " << totalVotes << endl;
// output winner
int winIndex = winnerIndex(numbers,5);
cout << "The winner of Election is " << voterNames[winIndex] << endl;
return 0;
}
int sumVotes(int list[], int size)
{
int sum=0;
for (int i = 0; i < size; ++i)
{
sum += list[i];
}
// function code
return sum;
}
int winnerIndex(int list[], int size)
{
// function code
int max=0, winIndex;
for (int i = 0; i < size; ++i)
{
if (list[i]>max){
max = list[i];
winIndex = i;
}
}
return winIndex;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.