### Using Index in Arrays

Hello. I am a beginner in c++ and I was confused as to how to use the indexOfMax function in my code to find the maxVotesIndex. Also, i can't get the percentVotes to calculate correctly and it prints out strangely. Please forgive me for all the errors, as I said, I am a beginner. Any help is really appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129`` ``````#include #include #include using namespace std; int sumList(int list[], int size); int indexOfMax(int list[], int size); const int NUM_CANDIDATES = 5; // The number of candidates int main() { 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 double percentVotes; // The percentage of votes received by a candidate int maxVotesIndex; // The index of the winner with the largest number of votes int i; // A counter /////////////////////////////// // Start of your code // Get the candidates' names and number of votes each received // Assume the user enters valid input for(i=0; i < NUM_CANDIDATES; i++) { cout << "Enter the candidates' names: "; getline(cin,names[i]); cout << "How many votes did " << names[i] << " get? "; cin >> votes[i]; cin.ignore(5000, '\n'); cout << endl; } // Calculate the total number of votes received by all candidates // (use the sumList function) totalVotes = sumList(votes, NUM_CANDIDATES); cout << "the totalVotes is " << totalVotes << endl; for( i=0; i < NUM_CANDIDATES; i++) { percentVotes = (static_cast(votes[i])/totalVotes) * 100; } // Print each candidate's name, the number of votes she received, // and the percent of votes the candidate received cout << "Candidates"; cout << setw(15) << "Num Votes"; cout << setw(15) << "% of Votes" << endl; for( i = 0; i < NUM_CANDIDATES; i++ ) { cout << names[i]; cout << votes[i]; cout << percentVotes; cout << endl; } // Find the array index of the candidate with the largest number of votes // (use the indexOfMax function) // Print the name of the winner // End of your code in the main function // Don't forget to complete the functions below /////////////////////////////// system("pause"); return 0; } // // Returns the sum of the numbers in the given array // // WRITE THE BODY OF THIS FUNCTION // int sumList(int list[], int size) { int sum = 0; int i; for(i = 0; i < size; i++) { sum += list[i]; } return sum; } // // 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) { int highestIndex = 0; int i; for(i=0; i < size; i++) { if(list[i] > list[highestIndex]) { highestIndex = i; } } return highestIndex; }``````
Last edited on
Wht exactly is the problem with your code?

Are there any errors or warnings?

For percentVotes, you need to calculate to the total sum of array prior to calculating the percentage.

 ``1234`` ``````for(int i = 0; i < size; i++) percentVotes += votes[i]; percentVotes /= totalVotes; perentVotes *= 100;``````

Also, a nice little trick for the size of arrays, so you don't need to declare the size in case you're using dynamic allocations is to do the following:
 ``12345678910111213141516`` ``````#include using std::cout; int a_function(int*); int main() { int list[] = {1,2,3,4,5,6,5,4,3,2,1}; a_function(list); return 0; } void a_function(int *list) // int *list denotes an array being passed to the function { int size = sizeof(list)/sizeof(int) // Or sizeof(data type of your array) for(int i = 0; i < size; i++) cout << list[i] << endl; }``````

Sorry about the no code tags, I'm new and I didn't know, so thanks. And thanks for the reply.

Okay... sooo

There are no errors or warnings. Only the unreferenced local variable for MaxVotesIndex.

the output is supposed to look like this:

Enter the candidate's name: Alice

Enter the candidate's name: Bob

Enter the candidate's name: Carlos

Enter the candidate's name: Debbie

Enter the candidate's name: Edward
 ``123456`` ``````Candidates NumVotes % of Votes Alice 1234 8.46 Bob 8764 60.08 Carlos 456 3.13 Debbie 3456 23.69 Edward 678 4.65``````

The winner is: Bob

________________________

However, mine looks like this

Enter the candidate's name: Alice

Enter the candidate's name: Bob

Enter the candidate's name: Carlos

Enter the candidate's name: Debbie

Enter the candidate's name: Edward
 ``123456`` ``````Candidates NumVotes % of Votes Alice12344.80988 Bob876544.80988 Carlos4564.80988 Debbie34564.80988 Edward6784.80988``````

and i don't know how to find maxVotesIndex so I don't have an output for winner... I really need help.
Last edited on
Well, I fixed the output.

However,

my % of Votes still prints out the wrong number.
It still prints out "44.80988" for all of them.

And I'm still totally lost on how to find the array index of the candidate with the largest number of votes using the indexOfMax function found at the bottom..

Thanks for any help you could give me. I'd really appreciate it.
 ``12345678910111213141516`` ``````for( i=0; i < NUM_CANDIDATES; i++) { percentVotes = (static_cast(votes[i])/totalVotes) * 100; } cout << "Candidates"; cout << setw(15) << "Num Votes"; cout << setw(15) << "% of Votes" << endl; for( i = 0; i < NUM_CANDIDATES; i++ ) { cout << names[i]; cout << votes[i]; cout << percentVotes; cout << endl; }``````

That row 4 overwrites 'percentVotes'. Only one value remains after the vote. I would remove the whole loop and calculate on row 14.

Rows 7 and 8 do use setw(15). So should rows 13 and 14 too.

You call indexOfMax just like you call sumList. The return value is an index. Who is names[index]?

This is what I have now. I don't know what you mean by calculating it on row 14.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121`` `````` #include #include #include using namespace std; int sumList(int list[], int size); int indexOfMax(int list[], int size); const int NUM_CANDIDATES = 5; // The number of candidates int main() { string names[NUM_CANDIDATES]; // The names of the candidates int votes[NUM_CANDIDATES]; // The number of votes received by each candidate int totalVotes = 0; // The total number of votes received by all candidates double percentVotes = 0; // The percentage of votes received by a candidate int maxVotesIndex; // The index of the winner with the largest number of votes int i; // A counter /////////////////////////////// // Start of your code // Get the candidates' names and number of votes each received // Assume the user enters valid input for(i=0; i < NUM_CANDIDATES; i++) { cout << "Enter the candidates' names: "; getline(cin,names[i]); cout << "How many votes did " << names[i] << " get? "; cin >> votes[i]; cin.ignore(5000, '\n'); cout << endl; } // Calculate the total number of votes received by all candidates // (use the sumList function) totalVotes = sumList(votes, NUM_CANDIDATES); // Print each candidate's name, the number of votes she received, // and the percent of votes the candidate received cout << setw(15) << "Candidates"; cout << setw(15) << "Num Votes"; cout << setw(15) << "% of Votes" << endl; for( i = 0; i < NUM_CANDIDATES; i++ ) { cout << setw(15) << names[i]; cout << setw(15) << votes[i]; cout << setw(15) << percentVotes; cout << endl; } // Find the array index of the candidate with the largest number of votes // (use the indexOfMax function) maxVotesIndex = indexOfMax(votes, NUM_CANDIDATES); cout << "The winner is: " << names[i]; // Print the name of the winner // End of your code in the main function // Don't forget to complete the functions below /////////////////////////////// system("pause"); return 0; } // // Returns the sum of the numbers in the given array // // WRITE THE BODY OF THIS FUNCTION // int sumList(int list[], int size) { int sum = 0; int i; for(i = 0; i < size; i++) { sum += list[i]; } return sum; } // // 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) { int highestIndex = 0; int i; for(i=0; i < size; i++) { if(list[i] > list[highestIndex]) { highestIndex = i; } } return highestIndex; }``````