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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<iomanip>
#include<string>
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<double>(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.

1
2
3
4
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
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;
}


ALso, please use code tags.

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
How many votes: 1234

Enter the candidate's name: Bob
How many votes: 8765

Enter the candidate's name: Carlos
How many votes: 456

Enter the candidate's name: Debbie
How many votes: 3456

Enter the candidate's name: Edward
How many votes: 678
1
2
3
4
5
6
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
How many votes: 1234

Enter the candidate's name: Bob
How many votes: 8765

Enter the candidate's name: Carlos
How many votes: 456

Enter the candidate's name: Debbie
How many votes: 3456

Enter the candidate's name: Edward
How many votes: 678
1
2
3
4
5
6
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for( i=0; i < NUM_CANDIDATES; i++)
{
  percentVotes = (static_cast<double>(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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

#include<iostream>
#include<iomanip>
#include<string>
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;
}
Ok, who is names[maxVotesIndex] ?

Replace the 'percentVotes' that you are about to print with the equation that calculates that value.
it works, but that way "percentVotes" variable is completely unreferenced...
Last edited on
If you don't need a variable, then remove it completely.
Topic archived. No new replies allowed.