I have no idea what I am doing... Please help me!!

I have no idea where to start on this program. Here are the specifications that I am supposed to include:

The function will determine which candidate won an election. The votes will be represented by an array of integers valued 1 through N, where N is the number of candidates. An integer of 1 denotes a vote for candidate 1 and a 2 denotes a vote for candidate 2, etc. The end of the votes' array are denoted by a 0 which, of course, zero does not correspond to any candidate.

-returns the number of the winner based on candidate numbers 1,2, etc.

-name the function countVotes

-receives the array of votes

-iterates through the votes array to determine the number of candidates(maximum number in array)

-allocates a new array of integers, tallies, to have a size equal to the number of candidates

-initialize each element in the tallies array to be zero

-iterates through the votes array while adding one to tallies array element that corresponds to the vote
~For example, if a vote has a value 3, increment tallies [ 3-1 ]
~One is subtracted in the array notation to account for the array beginning at element 0

-Iterate through the tallies array
~To determine the winner, i.e. which candidate has the maximum tally.
~To print out how many votes each candidate received

-Return the number of the winner based on candidate numbers 1,2, etc.

My professor gave us this code to start out with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int countVotes(int votes[]);

int main()
{
	int election[] = { 3, 4, 2, 4, 3, 1, 2, 4, 3, 3, 3, 2, 1, 4, 0 };
	int elected = countVotes(election);

	cout << "Candidate " << elected << " won the election.\n";

	return 0;
}

int countVotes(int votes[])
{

}


please help me!
I know I will need a loop of some sort to iterate through the arrays but I don't know where they go or what kind of loop I should use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int countVotes(int votes[])
{
    int numCandidates = 0;
    int *votesReceived; //pointer declaration for when you have to do dynamic array allocation
    int winner = 0;

    for (int i = 0; votes[i] != 0; ++i)
    {
        //find the largest number and store it in numCandidates
    }

    //do the dynamic array allocation now that you know the number of candidates
    //size of the dynamic array should probably be (numCandidates + 1) to make coding easier
    //set all elements of the dynamic array to 0

    for (int i = 0; votes[i] != 0; ++i)
        ++votesReceived[votes[i]]; //tallying votes per candidate

    //iterate through votesReceived[] and print out how many votes each candidate got
    //also update winner to whoever has the most amount of votes

    delete[] votesReceived;
    return winner;
}


Read up on loops: http://www.cplusplus.com/doc/tutorial/control/
Read up on dynamic memory: http://www.cplusplus.com/doc/tutorial/dynamic/
where does the new array tallies come in? is that what is being put into this part in your code

1
2
3
//do the dynamic array allocation now that you know the number of candidates
    //size of the dynamic array should probably be (numCandidates + 1) to make coding easier
    //set all elements of the dynamic array to 0 
I named it votesReceived but you can change it to tallies.
closed account (E3h7X9L8)
i didnt understood well what the problem requires so i assumed that
for my example of 1,1,3,3,8,8,8,9,0,0
will be 9 candidates votes 1,1 will be for candidate 1 ; 3,3 for candidate 3 and so on , finally the candidate who won is candidate 8 with 3 votes ... let me know if i got it wrong
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
#include <iostream>
using namespace std;
int election[] = { 1,1,3,3,8,8,8,9,0,0 };
int winner[9] = {0}; 
int countVotes();
int sortVotes();

int main()
{
    countVotes();
    sortVotes();
	return 0;
}

int countVotes()
{
    int x;
    for(int i = 1; i <= 10; i++) // put the length of election array instead of 10
    {
        x = election[i];
        winner[x]++;
    }

}

int sortVotes()
{
    int elected = 0;
    for(int i = 1; i <= 9; i++)
    {
        if(winner[i] >= elected)
        {
            elected = i;
        }

    }
    cout << "Winner is candidate number " << elected << " with " << winner[elected] << " votes!";
}
fg109,

this is what I added so far to the code you gave me
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
int countVotes(int votes[])
{
	int numCandidates = 0;
	int *tallies; //pointer declaration for when you have to do dynamic array allocation
	int winner = 0;

	for (int candidate = 0; votes[candidate] != 0; ++candidate)
	{
		//find the largest number and store it in numCandidates
		if (votes[candidate] > numCandidates)
		{
			numCandidates = votes[candidate];
		}
	}

	//do the dynamic array allocation now that you know the number of candidates
	//size of the dynamic array should probably be (numCandidates + 1) to make coding easier
	//set all elements of the dynamic array to 0
	tallies = new int[numCandidates + 1];

	for (int candidate = 0; votes[candidate] != 0; ++candidate)
	{
		++tallies[votes[candidate]]; //tallying votes per candidate
	}

	//iterate through tallies[] and print out how many votes each candidate got
	//also update winner to whoever has the most amount of votes

	delete[] tallies;
	return winner;

}


the only part I don't understand now is this part:
//iterate through tallies[] and print out how many votes each candidate got
//also update winner to whoever has the most amount of votes

will I need another loop to iterate through tallies[]?
Yes, another for loop. The condition for the loop will be different than the other ones.
Last edited on
Ok so then how do you know what these conditions will be? I know we are supposed to print out how many votes for each candidate which means I will need a cout but that isn't part of the conditions. How come the other two for loops are the same?
1
2
3
4
for (int candidate = 0; tallies[votes[candidate]] != 0; ++candidate)
	{
		cout << tallies[votes[candidate]]++ << endl;
	}


this what I put but it gave me some craze negative numbers.... I think I did something wrong -lol-
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
#include <iostream>
using namespace std;

int countVotes(int votes[]);

int main()
{
	int election[] = { 3, 4, 2, 4, 3, 1, 2, 4, 3, 3, 3, 2, 1, 4, 0 };
	int elected = countVotes(election);

	cout << "Candidate " << elected << " won the election.\n";

	return 0;
}

int countVotes(int votes[])
{
	int numCandidates = 0;
	int *tallies=0;
	int winner = 0;

	for (int voteForcandidate = 0; votes[voteForcandidate] != 0; ++voteForcandidate)
	{
		if (votes[voteForcandidate] > numCandidates)
		{
			numCandidates = votes[voteForcandidate];
		}
	}

	tallies = new int[numCandidates];

	for (int candidate = 0; votes[candidate] != 0; ++candidate)
	{
		++tallies[votes[candidate]];
	}

	//iterate through tallies[] and print out how many votes each candidate got
	//also update winner to whoever has the most amount of votes
	
	
	delete[] tallies;
	return winner;

}


this is what I have... I don't know how to finish it. please help me!
Did you read through the part about loops on this site's C++ tutorial? It explains everything very clearly.

for (initialization; condition; increase) statement;

Think about what you're trying to do with the loop and then how to do it.

In the first two loops, you were supposed to iterate over the votes[] array. In this loop, which array are you iterating through? How will that change the loop conditions?
Last edited on
Topic archived. No new replies allowed.