Created
August 26, 2023 16:38
-
-
Save maddox05/e3500632eef7bc8e4d0eed76cbcda6fa to your computer and use it in GitHub Desktop.
Tideman Completion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <string.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 = 0; | |
int candidate_count = 0; | |
// Function prototypes | |
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[]) | |
{ | |
for (int i = 0; i < candidate_count; i++) | |
{ | |
if (strcmp(name, candidates[i]) == 0) | |
{ | |
ranks[rank] = i; | |
return true; | |
} | |
} | |
return false; | |
} | |
// Update preferences given one voter's ranks | |
void record_preferences(int ranks[]) | |
{ | |
// preferences[i][j] is number of voters who prefer i over j | |
int count = 1; | |
for (int i = 0; i < candidate_count - 1; i++) | |
{ | |
for (int j = count; j < candidate_count; j++) | |
{ | |
preferences[ranks[i]][ranks[j]] = (preferences[ranks[i]][ranks[j]] + 1); | |
// printf("Adds %i ") | |
} | |
count = count + 1; | |
} | |
return; | |
} | |
// Record pairs of candidates where one is preferred over the other | |
void add_pairs(void) | |
{ | |
// use pair pairs | |
// update global pair count | |
for (int i = 0; i < candidate_count; i++) | |
{ | |
for (int j = 0; j < candidate_count; j++) | |
{ | |
if (preferences[i][j] > preferences[j][i]) | |
{ | |
pairs[pair_count].winner = i; | |
pairs[pair_count].loser = j; | |
// printf("Pair winner: %i Pair Loser %i\n",pairs[pair_count].winner, pairs[pair_count].loser); | |
pair_count = pair_count + 1; | |
} | |
} | |
} | |
return; | |
} | |
// Sort pairs in decreasing order by strength of victory | |
void sort_pairs(void) | |
{ | |
// strongest victory to weakest | |
// puts win difference values into normal array WORKS MAYBE | |
// printf("There are %i pairs\n", pair_count); | |
int winamt[pair_count]; | |
for (int i = 0; i < pair_count; i++) | |
{ | |
winamt[i] = (preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser][pairs[i].winner]); | |
// printf("winamt is currently: %i\n", winamt[i]); | |
} | |
// sorts array | |
int temp = 0; | |
int count = 0; | |
for (int j = pair_count - 1; j > 0; j--) | |
{ | |
bool sorted = true; | |
for (int k = pair_count - 1; k > count; k--) | |
{ | |
if (winamt[k] > winamt[k - 1]) | |
{ | |
temp = winamt[k - 1]; | |
winamt[k - 1] = winamt[k]; | |
winamt[k] = temp; | |
sorted = false; | |
} | |
} | |
count = count + 1; | |
if (sorted == true) | |
{ | |
// printf("was sorted early\n"); | |
break; | |
} | |
} | |
// printf("Sorted array is: "); | |
for (int h = 0; h < pair_count; h++) | |
{ | |
// printf("%i",winamt[h]); | |
break; | |
} | |
// printf("\n"); | |
// puts back into pairs | |
for (int m = 0; m < pair_count; m++) | |
{ | |
for (int n = m; n < pair_count; n++) | |
{ | |
if (winamt[m] == preferences[pairs[n].winner][pairs[n].loser] - preferences[pairs[n].loser][pairs[n].winner]) | |
{ | |
temp = pairs[m].winner; | |
pairs[m].winner = pairs[n].winner; | |
pairs[n].winner = temp; | |
temp = pairs[m].loser; | |
pairs[m].loser = pairs[n].loser; | |
pairs[n].loser = temp; | |
} | |
} | |
} | |
return; | |
} | |
// is last pair true (locked), if so is me the winner | |
bool can_lock(int original_winner, int loser, int winner, int recursion_index, int original_loser) | |
{ | |
int new_winner = 0; | |
int new_loser = 0; | |
int counter = 0; | |
if (original_winner == loser) | |
{ | |
return false; | |
} | |
for (int i = recursion_index; i < candidate_count; i++) | |
{ | |
// i goes through colums | |
if (locked[loser][i] == true) | |
{ | |
new_winner = loser; // who just won | |
new_loser = i; // who just lost | |
if (!can_lock(original_winner, new_loser, new_winner, counter, original_loser)) | |
{ | |
// if rodrick loses but never wins, travel back to alice to see if she beats anyone else who also wins. | |
return false; | |
} | |
} | |
} | |
// if loser ever wins // then check next loser if they win | |
// rodrick does not win | |
return true; | |
} | |
// Lock pairs into the candidate graph in order, without creating cycles | |
void lock_pairs(void) | |
{ | |
// goes thorugh all pairs | |
for (int pair_index = 0; pair_index < pair_count; pair_index++) | |
{ | |
if (can_lock(pairs[pair_index].winner, pairs[pair_index].loser, pairs[pair_index].winner, 0, pairs[pair_index].loser)) | |
{ | |
locked[pairs[pair_index].winner][pairs[pair_index].loser] = true; | |
} | |
} | |
// debug stuff | |
for (int j = 0; j < candidate_count; j++) | |
{ | |
printf("%i ", j); | |
for (int k = 0; k < candidate_count; k++) | |
{ | |
printf("%i", locked[j][k]); | |
} | |
printf("\n "); | |
} | |
} | |
// Print the winner of the election | |
void print_winner(void) | |
{ | |
bool check = false; | |
for (int i = 0; i < candidate_count; i++) | |
{ | |
check = false; | |
for (int j = 0; j < candidate_count; j++) | |
{ | |
if (locked[j][i] == true) | |
{ | |
check = true; | |
} | |
} | |
if (check == false) | |
{ | |
printf("%s\n", candidates[i]); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment