diff --git a/Exercise 5 - Hash Map (Week 6)/Ex5.pdf b/Exercise 5 - Hash Map (Week 6)/Ex5.pdf new file mode 100644 index 0000000..cd95e36 Binary files /dev/null and b/Exercise 5 - Hash Map (Week 6)/Ex5.pdf differ diff --git a/Exercise 5 - Hash Map (Week 6)/Ex5.txt b/Exercise 5 - Hash Map (Week 6)/Ex5.txt new file mode 100644 index 0000000..6607cbd --- /dev/null +++ b/Exercise 5 - Hash Map (Week 6)/Ex5.txt @@ -0,0 +1,100 @@ +4210 +7312 +1521 +9408 +8802 +9120 +5168 +4052 +4566 +5403 +1947 +5957 +4065 +5797 +4375 +4383 +8041 +4917 +7333 +9366 +3264 +7218 +5941 +3904 +9682 +665 +1988 +8985 +6362 +7882 +190 +9006 +4114 +7514 +8534 +7871 +2459 +3072 +4906 +7396 +8413 +8633 +6473 +2651 +8647 +3076 +6677 +2202 +5810 +4527 +104 +2947 +845 +7550 +4131 +9450 +5674 +9308 +9364 +5539 +9010 +4128 +99 +1362 +1298 +2097 +664 +7459 +5262 +5894 +7623 +4973 +190 +9960 +3107 +9149 +5491 +4697 +2687 +1833 +9247 +4639 +8026 +4674 +2672 +8531 +4136 +6747 +4846 +8047 +835 +7590 +1060 +4034 +6465 +9802 +9633 +2494 +8569 +4545 diff --git a/Exercise 5 - Hash Map (Week 6)/Ex5_Prof_Solution.pdf b/Exercise 5 - Hash Map (Week 6)/Ex5_Prof_Solution.pdf new file mode 100644 index 0000000..8f3e29e Binary files /dev/null and b/Exercise 5 - Hash Map (Week 6)/Ex5_Prof_Solution.pdf differ diff --git a/Exercise 5 - Hash Map (Week 6)/ex5.c b/Exercise 5 - Hash Map (Week 6)/ex5.c new file mode 100644 index 0000000..7350590 --- /dev/null +++ b/Exercise 5 - Hash Map (Week 6)/ex5.c @@ -0,0 +1,100 @@ +/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html + * + * Exercise 5 - Hash Map (Week 6) + * Name: Manish + * Student Login: ***** + * + * Compile as: + * $ gcc -std=c11 -Wall -o ex5 ex5.c + */ + +#include +#include + +typedef struct entry +{ + int data; + struct entry * next; +} entry; + +/* This method of array initialization is GNU C extension and + * may not work with other compilers. Not even g++ + */ +entry hashmap[100] = {[0 ... 99] = (entry){.data = -1, .next = NULL}}; +int size = 0; + +int abs(int i); + +int main(void) +{ + printf("Enter file name: "); + char filename[257]; + // Assuming filename/file path won't be longer than 256 characters + scanf("%256s", filename); + + FILE* file = fopen(filename, "r"); + if (!file) + { + perror(filename); + exit(EXIT_FAILURE); + } + + int num; + int key; + entry * node; + while (fscanf(file, "%d", &num) != EOF) + { + key = abs(num%100); + if (hashmap[key].data == -1) // empty + hashmap[key].data = num; + else // chaining + { + node = &hashmap[key]; + while (node->next != NULL) + { + node = node->next; + } + entry * new_entry = malloc(sizeof(entry)); + new_entry->data = num; + new_entry->next = NULL; + node->next = new_entry; + } + } + + int empty = 0; + for (int i=0; i < 100; i++) + { + if (hashmap[i].data == -1) + empty++; + } + + int max_chain = 0; + int current_chain = 0; + for (int i=0; i < 100; i++) + { + if (hashmap[i].data == -1) // if empty, chain is 0 + continue; + current_chain = 1; // if not empty, chain is at least 1 long + node = hashmap[i].next; + while (node != NULL) + { + current_chain++; + node = node->next; + } + if (current_chain > max_chain) + max_chain = current_chain; + } + + printf("Empty Entries: %d\nLongest Chain: %d\n", empty, max_chain); + + fclose(file); + + return 0; +} + +int abs(int i) +{ + if (i < 0) + return -i; + return i; +} diff --git a/Exercise 5 - Hash Map (Week 6)/makefile b/Exercise 5 - Hash Map (Week 6)/makefile new file mode 100644 index 0000000..c2d17ec --- /dev/null +++ b/Exercise 5 - Hash Map (Week 6)/makefile @@ -0,0 +1,2 @@ +ex5: ex5.c + gcc -std=gnu11 -Wall -o ex5 ex5.c