Need help adding winner to voting contest based on responses and frequency in c++

Viewed 20

I am having trouble with writing code to this prompt. Question: A school is conducting a poll of who should be the next apprentice. There are 5 candidates to choose from. The school has polled 20 students on campus. Write a program in C++ that tallies up the number of votes each candidate received and pronounces a winner. So in the code I wrote below, it does tally up the number of votes, but I do not know how to show the winner with printmax. Can someone help me print the winner? This is my code and idk why its not working:

#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    const int responseSize = 20; 
    int vote[responseSize];
    const int frequencySize = 6;

    for (int i = 0; i < 21; i++){
        cout <<"Please enter the next vote: "<< endl;
        cin >> vote[i];
    }

    int frequency [ frequencySize ] = { 0 };

    for ( int answer = 0; answer < responseSize;  answer++)
    ++frequency[vote[answer]];

    cout << "Rating" << setw(17) << "Frequency" <<endl;

    for ( int rating = 1; rating < frequencySize; rating++)
    cout << setw(6) << rating
    << setw(17) << frequency[rating] << endl;
    
    return 0;

}

int printmax(int frequency[], int frequencysize)
{
    int max =0, winner = 0, i=0;
    for (int i =0; i < frequencysize; i++)
        if (frequency[i] > max){
            max = frequency[i];
            winner = i;
        }
    cout << "and the winner is: " << i << endl;
    return 0;
}
0 Answers
Related