-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
395 lines (251 loc) · 6.3 KB
/
main.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "main.h"
#define tes 1
#define check(x) (x == tes)
int give_number() {
return 42;
}
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
return a / b;
}
int modulo(int a, int b) {
return a % b;
}
int is_even(int n) {
if (n % 2 == 0)
return 1;
else return 0;
}
int absolute(int n) {
if (n >= 0)
return n;
else return -n;
}
int alternating_sum(int n) {
int res = 0;
for (int i = 0; i <= n; i++) {
if (is_even(i))
res += i;
else res -= i;
}
return res;
}
//int inner_product_helper(int *a, int *b, int len);
int inner_product_helper(int *a, int *b, int len) {
int res = 0;
for (int i = 0; i < len; i++) {
res += *(a + i) * *(b + i);
}
return res;
}
int inner_product(int *a, int a_len, int *b, int b_len) {
if (a_len <= b_len) {
return (inner_product_helper(a, b, a_len));
} else return (inner_product_helper(a, b, b_len));
}
int make_odd_negative(int *a, int size) {
int res = 0;
for (int i = 0; i < size; i++) {
if (is_even(*(a + i)) == 0 && *(a + i) > 0) {
*(a + i) *= -1;
res++;
}
}
return res;
}
int make_even_positive(int *a, int size) {
int res = 0;
for (int i = 0; i < size; i++) {
if (is_even(*(a + i)) == 1 && *(a + i) < 0) {
*(a + i) *= -1;
res++;
}
}
return res;
}
int mangle_array(int *a, int size) {
return make_even_positive(a, size) + make_odd_negative(a, size);
}
uint8_t set_bit(uint8_t reg, int n) {
uint8_t mask = 1;
mask = mask << n;
return mask | reg;
}
uint8_t unset_bit(uint8_t reg, int n) {
uint8_t mask = 1;
mask = mask << n;
mask = ~mask;
printBits(mask);
printf("\n");
return (mask & reg);
}
int is_set(uint8_t reg, int n) {
uint8_t mask = 1;
mask = mask << n;
if ((mask & reg) == 0) {
return 0;
} else return 1;
}
void get_knob_positions(uint8_t knobs, uint8_t *knob1_position, uint8_t *knod2_position) {
*knob1_position = knobs & 0b00001111;
*knod2_position = knobs >> 4;
}
void printBits(uint8_t num) {
for (int bit = 0; bit < (sizeof(uint8_t) * 8); bit++) {
printf("%i ", num & 0x01);
num = num >> 1;
}
}
typedef struct Grade {
struct Grade *next;
// char *name; why does this not work??? should it not be the same as the name[8]
char name[8];
float grade;
} Grade;
Grade *create_grade(Grade *next, char *name, float grade) {
Grade *new_grade = (Grade *) malloc(sizeof(Grade));
new_grade->next = next;
for (int i = 0; i < 8; i++) {
(*new_grade).name[i] = name[i];
}
new_grade->grade = grade;
return new_grade;
}
//store_grades(int n, char *name, float grades[]) {
Grade *store_grades(int n, char names[][8], float grades[]) {
Grade *next = 0;
for (int i = 0; i < n; i++) {
next = create_grade(next, names[i], grades[i]);
}
return next;
}
//Grade *store_grades(int n, char names[][8], float grades[]) {
//
// char copy[8];
// for(int i = 0 ; i < 8 ; i++)
// copy[i] = names[0][i];
//
// Grade* current = create_grade(NULL , copy , grades[n]);
//
// n--;
//
//
// while (n > 0) {
//
// char temp[8];
//
// for(int q = 0 ; q < 8 ; q++)
// temp[q] = names[n][q];
//
// Grade* new_Grade = create_grade( current , temp , grades[n]);
// n--;
//
// current->next = new_Grade;
//
// current = new_Grade;
// }
//
// return current;
//}
void remove_grades(Grade *root) {
Grade *current = root;
while (current != 0) {
Grade *current1 = current->next;
free(current);
current = current1;
}
}
int size(Grade *grade) {
Grade *current = grade;
int n = 0;
while (current != 0) {
current = current->next;
n++;
}
return n;
}
Grade *reverse_list1(Grade *grade) {
Grade *res = NULL;
while (grade) {
Grade *next = grade->next;
grade->next = res;
res = grade;
grade = next;
}
return res;
}
// defining a funtion pointer type called "rev" that returns grade and takes some para
typedef Grade *(*rev)(Grade *grade);
// the complier knows that this implements rev, because it have the same return type and paramters
Grade *reverse_list(Grade *grade) {
const int s = size(grade);
int count = 0;
char names[s][8];
float grades[s];
Grade *current = grade;
while (count < s) {
for (int i = 0; i < 8; i++) {
names[count][i] = current->name[i];
}
grades[count] = current->grade;
current = current->next;
count++;
}
return (store_grades(s, names, grades));
}
void printList(Grade *root) {
int s = size(root);
if (s == 0 || root == 0) {
printf("Array is has the same number of elements as the number of fucks I give\n");
}
Grade *current = root;
for (int i = 0; i < s; i++) {
printf("%s , %f \n", current->name, current->grade);
current = current->next;
}
}
uint64_t *interrupt_vector;
void register_isr(uint8_t interrupt_source, void (*isr)()) {
interrupt_vector[interrupt_source] = (uint64_t) isr;
}
void execute_isr(uint8_t interrupt_source) {
void (*function)() = (void (*)()) interrupt_vector[interrupt_source];
function();
}
// takes a funtion pointer
void foo(Grade *(*bar)()) {
//..
}
//
void foo2(rev bar);
#include "hashmap.h"
void *resolve_collision(void *old_value, void *new_value) {
return new_value;
}
int main() {
unsigned int key_space = 1024;
HashMap *hm = create_hashmap(key_space);
char *string_1 = "CSE2425";
char *string_2 = "Embedded";
const char *key_1 = "ab";
const char *key_2 = "cd";
// Insert ("ab" -> "CSE2425").
insert_data(hm, key_1, string_1, resolve_collision);
unsigned int i = (hm->size % hash(key_1));
Bucket *b = hm->buckets[i];
insert_data(hm, key_1, string_2, resolve_collision);
Bucket *c = hm->buckets[i];
insert_data(hm, key_2, string_2, resolve_collision);
return 0;
}