0

I realize that this code is not well designed, however I will try to solve that once my code can compile.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
typedef struct
{
    string names;
    int votes;
    bool eliminated;
}
candidate;
void tabulate(void);
bool vote(int voter, int rank, string names);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);

int voter_count, candidate_count;
candidate candidates[candidate_count];
int p;
//used in tabulate function
int minimum;
int main (int argc, string argv[])
{
candidate_count = argc;
voter_count = get_int("How many voters:\n ");
// don't forget to assign names to candidates
for (int z = 1; z <= candidate_count; z ++)
{
    for (int u = 0; u <candidate_count; z ++)
    {
        candidates[u].names = argv[z];
        // my attempt at assigning names to candidate
    }
}


for (int i = 0; i < voter_count; i++)
{
    for (int x= 0; x < candidate_count; x++)
    {

        string j = get_string("Rank the Voters:\n ");

        vote(i, x, j);
        tabulate();
        print_winner();
        find_min();
        is_tie(minimum);
        eliminate(minimum);
        // function calls

    }
}

}

bool vote(int voter, int rank, string names)
{
    int preferences[voter][rank];
    for (int i=0; i < voter_count; i++)
    {
        for (int x=0; x < candidate_count; x++)
        {
            if (strcmp(names, candidates[x].names)==0)
            {
                 preferences[voter][rank] = p;
                 return 0;
            }
            return 1;

        }

    }


}

void tabulate(void)
{
   if (candidates[p].eliminated==0)
  {
      candidates[p].votes+=1;
  }
}

bool print_winner(void)
{
    int half = voter_count/2;
    for (int a = 0; a < voter_count; a++)
    {
        for (int b = 0; b < candidate_count; b++)
        {
            if (candidates[b].votes > half)
            {
                printf("winner is %s", candidates[b].names);
                printf("\n");
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
}

int find_min(void)
{
    for (int a = 0; a < voter_count; a++)
    {
        for (int m = 0; m < candidate_count; m++)
        {
            if (candidates[m].eliminated == 0)
            {
                int next = m + 1;
                if (candidates[m].votes <= candidates[next].votes)
                {
                minimum = candidates[m].votes;
                return minimum;
                return 0;
                }
                else
                {
                minimum = candidates[next].votes;
                return 0;
                return minimum;
                }
            }
        else
        {
            return 1;
        }
        }
    }
}

bool is_tie(int min)
{
    for (int a = 0; a < voter_count; a++)
    {
        for (int b = 0; b< candidate_count; b++)
        {
           if (candidates[b].eliminated == 0)
           {
               if (candidates[b].votes == min)
               {
                   continue;
               }
            else
            {
                return 1;
            }
           }
        }
    }
}

void eliminate(int min)
{
    for (int a = 0; a < voter_count; a++)
    {
        for (int b = 0; b< candidate_count; b++)
        {
           if (candidates[b].eliminated == 0)
           {
               if (candidates[b].votes == min)
               {
                   candidates[b].eliminated = 1;
                   candidate_count-=1;
               }

           }

        }
    }

}
2
int voter_count, candidate_count;
candidate candidates[candidate_count];

In C, you cannot define an array with a variable size at a global scope (even if you could, candidate_count does not have a value at compile time so memory could not be allocated appropriately for the array).

You need to define the array with a fixed size (let's say 5 for the sake of the example):

// declared directly    
candidate candidates[5];
    
// declared with a constant variable
const int max_candidates = 5;
candidate candidates[max_candidates];
    
// declared with a preprocessor macro
#define MAX_CANDIDATES 5
candidate candidates[MAX_CANDIDATES];
1
  • that helps, appreciate – Yi RuoLe Mar 4 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .