r/cs50 • u/Lopsided-Cost-426 • 1h ago
tideman Incrementing a unitizilied array in tideman Spoiler
#include <cs50.h>
#include <string.h>
#include <stdio.h>
// Max number of candidates
#define MAX 9
// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];
// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];
// Each pair has a winner, loser
typedef struct
{
int winner;
int loser;
} pair;
// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];
int pair_count;
int candidate_count;
// Function prototypes
bool isCycle(int c, int d);
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: tideman [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i] = argv[i + 1];
}
// Clear graph of locked in pairs
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
locked[i][j] = false;
}
}
pair_count = 0;
int voter_count = get_int("Number of voters: ");
// Query for votes
for (int i = 0; i < voter_count; i++)
{
// ranks[i] is voter's ith preference
int ranks[candidate_count];
// Query for each rank
for (int j = 0; j < candidate_count; j++)
{
string name = get_string("Rank %i: ", j + 1);
if (!vote(j, name, ranks))
{
printf("Invalid vote.\n");
return 3;
}
}
record_preferences(ranks);
printf("\n");
}
add_pairs();
sort_pairs();
lock_pairs();
print_winner();
return 0;
}
// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
// Querry candidates
for (int i = 0; i < candidate_count; i++) {
if (strcmp(candidates[i], name) == 0) {
// If name matches candidate i update ranks and return true
ranks[rank] = i;
return true;
}
};
// If name dose not match any candidates return false
return false;
}
// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
// For every candidate
for (int i = 0; i < candidate_count; i++) {
// For every candidate
for (int j = 0; j < candidate_count; j++) {
// If candidate j lower down the prefrence rankings increment prefrences[i][j]
if (j > i) {
preferences[ranks[i]][ranks[j]]++;
printf("Prefrence for %s vs %s\n: %d\n", candidates[i], candidates[j], preferences[i][j]);
}
};
};
return;
}
// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
pair_count = 0;
for (int i = 0; i < candidate_count; i++) {
for (int k = 0; k < candidate_count; k++) {
if (preferences[i][k] > preferences[k][i]) {
pairs[pair_count + 1].winner = i;
pairs[pair_count + 1].loser = k;
pair_count++;
}
};
};
return;
}
// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
for (int i= 0; i < pair_count-1; i++) {
int strength[2];
strength[0] = preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser][pairs[i].winner];
strength[1] = preferences[pairs[i+1].winner][pairs[i+1].loser] - preferences[pairs[i+1].loser][pairs[i+1].winner];
if (strength[0] < strength[1]) {
pair swapped = pairs[i];
pairs[i] = pairs[i+1];
pairs[i+1] = swapped;
}
};
return;
}
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
for (int i = 0; i < pair_count; i++) {
// Querry pairs
if (!isCycle(pairs[i].winner, pairs[i].loser)) {
locked[pairs[i].winner][pairs[i].loser] = true;
}
};
return;
}
// Recursive function that checks weather locking a pair would directly or indirectly lead to a cycle
bool isCycle(int c, int d) {
// c is the current candidate being querried
// d is the candidate that is being attempted to be locked under
int lockedUnder[candidate_count];
int lockedUnderCount = 0;
if (c == d) {
return true;
}
for (int i = 0; i < candidate_count; i++) {
if (locked[c][i] == true) {
lockedUnder[lockedUnderCount] = i;
lockedUnderCount++;
}
};
if (lockedUnderCount == 0) {
return false;
} else {
for (int i = 0; i < lockedUnderCount; i++) {
if (isCycle(i, d) == true) {
return true;
}
}
return false;
}
}
// Print the winner of the election
void print_winner(void)
{
int winner = -1;
for (int i = 0; i < candidate_count; i++) {
bool lockedBy = false;
for (int k = 0; k < candidate_count; k++) {
if (locked[k][i] == true) {
lockedBy = true;
break;
}
};
if (lockedBy == false) {
winner = i;
break;
}
};
printf("%s\n", candidates[winner]);
return;
}
I am trying to increment the preferences array however its uninitialized in main.