I've been working for weeks on pset3 - plurality, and all my logic is working fine in debug50, but not in real execution. It gives me either "segmentation fault" or (null). Basically, my logic is creating an organized array of the candidates (ganan [ ]) based on number of votes, find the largest number (int ganador), print its name, and checking for candidates with same number of votes. My code is the following (bool vote (string name) is working fine):
// Print the winner (or winners) of the election
void print_winner(void)
{
// Array for winners
candidate ganan[voter_count];
for (int k = 0; k < candidate_count; k ++)
{
for (int h = 0; h < (candidates[k].votes); )
{
if (ganan[(candidates[k].votes)-h].votes == 0 || ganan[(candidates[k].votes)-h].votes < candidates[k].votes)
{
ganan[(candidates[k].votes)-h].name = candidates[k].name;
ganan[(candidates[k].votes)-h].votes = candidates[k].votes;
h = (candidates[k].votes);
}
else
{
h++;
}
}
}
// Find the winner
int ganador;
for (int ab = 0; ab < voter_count; ab++)
{
if (ganan [voter_count - ab].votes > 0)
{
ganador = voter_count - ab;
ab = voter_count;
}
}
printf("%s \n", ganan[ganador].name);
for (int z = 0; z < voter_count; z++)
{
// Check if there's a tie
if (ganan[z].votes == ganan[ganador].votes && ganador != z)
{
printf("%s \n", ganan[z].name);
}
}
}