1. Plurality

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <cs50.h>
#include <string.h>
 
#define MAX 9
 
typedef struct{
 
    char *name;
    int votes;
 
}candidate;
 
//array of candidates
candidate candidates[MAX];
 
//number of candidates
int candidate_count;
 
//functions
bool vote(char *name);
void print_winner();
 
int main(int argc, char *argv[]){
    //Plurality vote
        //every voter chooses one candidate
        //whichever candidate has the msot votes wins
 
    //./plurality Alice Bob Charlie
    //Number of voters : 4
    //Vote: Alice
    //Vote: Bob
    //Vote: Charlie
    //Vote: Charlie
    //Charlie
 
//2 functions
    //vote
        //look for a candidate called name.
        //if candidate found, update their vote total and return true.
        //if no candidate found, dont update any vote totals and return false
    //print_winner
        //print the name of the candidate(s) who have the most votes.
 
    //check for invalid usage
    if(argc < 2){
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }
 
    //populate array of candidates
    //check for error
    candidate_count = argc - 1;
    if(candidate_count > MAX){
        printf("Maxium number of candidates is %i\n", MAX);
        return 2;
    }
 
    for(int i = 0; i < candidate_count; i++){
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }
 
    //prints array
    /*for(int i = 0; i < candidate_counts; i++){
        printf("%i %s %i\n", i, candidates[i].name, candidates[i].votes);
    }*/
 
    //receive number of voters
    int voters = get_int("Number of voters: ");
 
    //iterate and check for invalid votes
    for(int i = 0; i < voters; i++){
        char *name = get_string("Vote: ");
        if(!vote(name)){
            printf("Invalid vote.\n");
        }
    }
 
    print_winner();
    return 0;
 
}
 
bool vote(char *name){
 
    for(int i = 0; i < candidate_count; i++){
        if(strcmp(name, candidates[i].name) == 0){
            candidates[i].votes += 1;
            return true;
        }
    }
    return false;
}
 
void print_winner(){
 
    //finds the highest votes
    int highest = 0;
    for(int i = 0; i < candidate_count; i++){
        if(candidates[i].votes > highest) highest = candidates[i].votes;
    }
 
    //prints highest voters
    for(int i = 0; i < candidate_count; i++){
        if(candidates[i].votes == highest) printf("%s\n", candidates[i].name);
    }
    return;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

2. Runoff

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cs50.h>
#include <stdio.h>
#include <string.h>
 
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
 
int preferences[MAX_VOTERS][MAX_CANDIDATES];
 
typedef struct{
    char *name;
    int votes;
    bool eliminated;
}candidate;
 
candidate candidates[MAX_CANDIDATES];
 
int voter_count;
int candidate_count;
 
//Function prototypes
bool vote(int voter, int rank, char *name);
void tabulate();
bool print_winner();
int find_min();
bool is_tie(int min);
void eliminate(int min);
 
int main(int argc, char *argv[]){
    //Runoff Vote
        //Every voter ranks their preferences
        //if a candidate has a majority (more than half) of the ovtes,
        // they are the winner
        //otherwise, eliminate the candidate with the fewest votes
        //and re-run the election without them
 
    //Check for invalid usage
    if(argc < 2){
        printf("Usage: runoff [candidate...]\n");
        return 1;
    }
 
    //Populate array of candidates
    candidate_count = argc - 1;
    if(candidate_count > MAX_CANDIDATES) {
        printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
        return 2;
    }
 
    for(int i = 0; i < candidate_count; i++){
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
        candidates[i].eliminated = false;
    }
 
    //receive number of votes
    voter_count = get_int("Number of voters: ");
    if(voter_count > MAX_VOTERS){
        printf("Maximum number of voters is %i\n", MAX_VOTERS);
        return 3;
    }
 
    //keep querying for votes
    for(int i = 0; i < voter_count; i++){
        //query for each rank
        for(int j = 0; j < candidate_count; j++){
            //record vote, unless it's invalid
            char *name = get_string("Rank %i: ", j + 1);
            if(!vote(i, j, name)){
                printf("Invalid vote\n");
            }
        }
        printf("\n");
    }
 
    // Keep holding runoffs untsil winner exists
    while(true){
        // Calculate votes given remaining candidates
        tabulate();
        // Check if election has been won
        if(print_winner()){
            break;
        }
 
        int min = find_min();
        if(is_tie(min)){
            for(int i = 0; i < candidate_count; i++){
                if(!candidates[i].eliminated){
                    printf("%s\n", candidates[i].name);
                }
            }
            break;
        }
        eliminate(min);
 
        // Eliminate last-place candidates
        // If tie, everyone wins
        // Eliminate anyone with minimum number of votes
        // Reset vote counts back to zero
        for (int i = 0; i < candidate_count; i++)
        {
            candidates[i].votes = 0;
        }
    }
 
    return 0;
}
 
// Record preference if vote is valid
bool vote(int voter, int rank, char *name){
 
    bool has_candidate = false;
    //Look for a candidate called name,
    //if candidate found, update preferences so that they are the voter's
    for(int i = 0; i < candidate_count; i++){
        if(strcmp(name, candidates[i].name) == 0) {
            preferences[voter][rank] = i;
            return true;
        }
    }
 
    //if no candidate found, dont update and return false
    return false;
}
 
// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    //update vote counts for all non-eliminated candidates
    for(int i = 0; i < voter_count; i++){
        //recall that each voter votes for their heighest preference
        for(int j = 0; j < candidate_count; j++){
            //candidate who has not yet been eliminated.
            //only update vote who are still in the race
            if(!candidates[preferences[i][j]].eliminated){
                candidates[preferences[i][j]].votes += 1;
                break;
            }
        }
    }
 
    // TODO
    return;
}
 
// Print the winner of the election, if there is one
bool print_winner(void)
{
    //if any candidate has more ethan half the vote, print out their
    //their name and return true
    for(int i = 0; i < candidate_count; i++){
        if(voter_count/2 < candidates[i].votes){
            printf("%s\n", candidates[i].name);
            return true;
        }
    }
 
    //if nobody has won the election yet, return false
    return false;
}
 
// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    //return the minimum number of votes of anyone still in the elction.
    //return candidates who has not been eliminated
    int min = MAX_VOTERS;
    for(int i = 0; i < candidate_count; i++){
        if(!candidates[i].eliminated && min > candidates[i].votes)
        min = candidates[i].votes;
    }
    // TODO
    return min;
}
 
// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    //accepts the minimum number of votes min as input.
    for(int i = 0; i < candidate_count; i++){
        if(!candidates[i].eliminated && candidates[i].votes != min){
            return false;
        }
    }
    //returns true if the election is tied between all remaining candidates
    //, and return false otherwise.
    // TODO
    return true;
}
 
// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{
    //eliminate anyone still in the race who has the min number of votes
    //if votes are the same, should eliminate two candidates
    for(int i = 0; i < candidate_count; i++){
        if(!candidates[i].eliminated && candidates[i].votes == min){
            candidates[i].eliminated = true;
        }
    }
    // TODO
    return;
}
 
 
 
 
 
 
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

3. Tideman

COULD NOT FINISH TIDEMAN WITH MY CURRENT UNDERSTANDING OF PROGRAMMING

Will get back to it in the future

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#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;
int candidate_count;
 
// 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[])
{
    //- look for a candidate called name
    //- if candidate found, update ranks and return true. ranks[i] is the voter's ith preference.
    //- if no candidate found, dont update any ranks and return false.
    // TODO
 
    for(int i = 0; i < candidate_count; i++){
        if(strcmp(candidates[i], name) == 0){
            ranks[i] = rank;
            return true;
        }
    }
    return false;
}
 
// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    //- update the preferences array based on the current voter's rank
    // TODO
 
    for(int i = 0; i < candidate_count; i++){
        for(int j = 0; j < candidate_count; j++){
            if(ranks[i] < ranks[j]) preferences[i][j] += 1;
        }
    }
 
    for(int i = 0; i < candidate_count; i++){
        for(int j = 0; j < candidate_count; j++){
            printf("%i\t", preferences[i][j]);
        }
        printf("\n");
    }
    return;
}
 
// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
  //  - add each pair of candidates to pairs array if one candidate is preferred over the other.
//    - udpate global variable pair_count to be the total number of pairs
 
    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;
                pair_count += 1;
            }
            /*else if(preferences[i][j] < preferences[j][i]){
                pairs[pair_count].winner = i;
                pairs[pair_count].loser = j;
                pair_count += 1;
            }*/
 
        }
    }
 
    //before sort
    for(int i = 0; i < pair_count; i++){
        printf("pairs %ith is winner %i and loser %i\n", i, pairs[i].winner, pairs[i].loser);
    }
    printf("\n");
    // TODO
    return;
}
 
// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    //bubble sort
    //backwards, placing the highest value first
    for(int i = 0; i < pair_count - 1; i++){
        int ifirstDiff = preferences[pairs[i].winner][pairs[i].loser];
        int isecondDiff = preferences[pairs[i].loser][pairs[i].winner];
        int iComp = ifirstDiff - isecondDiff;
 
        for(int j = i + 1; j < pair_count; j++){
            int jfirstDiff = preferences[pairs[j].winner][pairs[j].loser];
            int jsecondDiff = preferences[pairs[j].loser][pairs[j].winner];
            int jComp = jfirstDiff - jsecondDiff;
 
            if(iComp < jComp){
                pair temp = pairs[j];
                pairs[j] = pairs[i];
                pairs[i] = temp;
            }
        }
    }
 
    for(int i = 0; i < pair_count; i++){
        printf("pairs %ith is winner %i and loser %i\n", i, pairs[i].winner, pairs[i].loser);
    }
    return;
}
 
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    //- update locked to create the locked graph by adding all edges
    //in decreasing order of vicotry strength, as long as there is no cycle
    // TODO
    locked[pairs[0].winner][pairs[0].loser] = true;
    for(int i = 1; i < pair_count; i++){
        int a = pairs[i].winner;
        int b = pairs[i].loser;
        for(int j = i + 1; j < pair_count; j++){
            int z = pairs[j].winner;
 
            if(a > z) locked[a][b] = true;
            else break;
        }
 
    }
 
    for(int i = 0; i < candidate_count; i++){
        for(int j = 0; j < candidate_count; j++){
            printf("%s\t", locked[i][j] ? "true" : "false");
        }
        printf("\n");
    }
 
    return;
}
 
// Print the winner of the election
void print_winner(void)
{
    //-print out the winner of the election, who will be the source of the graph
    //- you may assume there may be no more than one source.
    int count = 0;
    int winner = 0;
    for(int i = 0; i < pair_count; i++){
        int temp = 0;
        for(int j = 0; j < pair_count; j++){
            if(locked[i][j]) {
                temp ++;
                if(count < temp){
                    count = temp;
                    winner = i;
                } else if (count == temp){
                    winner = i;
                }
            }
        }
    }
 
    printf("%s\n", candidates[winner]);
    // TODO
    return;
}
 
/*bool lockrecursive(int winner, int loser){
 
    bool check = true;
    if(winner < 0){
        winner = 0;
        return true;
    }
    if(loser < 0){
        loser = 0;
        return true;
    }
 
    for(int i = 0; i < winner; i++){
        if(locked[winner][i]) check = false;
    }
}*/
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

'ETC' 카테고리의 다른 글

Apache poi(excel)  (0) 2020.01.28
01/20/20 notes(mac java dw, java download and setup)  (0) 2020.01.20
Arrays(week 2, problem set)  (0) 2019.12.30
C Programming Language(week1, problem set)  (0) 2019.12.30
Memory (week3)  (0) 2019.12.27

+ Recent posts