# Week 7 - Maps ## Team Team name: Date: Members | Role | Name | |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------| | **Facilitator** keeps track of time, assigns tasks and makes sure all the group members are heard and that decisions are agreed upon. | | | **Spokesperson** communicates group’s questions and problems to the teacher and talks to other teams; presents the group’s findings. | | | **Reflector** observes and assesses the interactions and performance among team members. Provides positive feedback and intervenes with suggestions to improve groups’ processes. | | | **Recorder** guides consensus building in the group by recording answers to questions. Collects important information and data. | | ## Activities Make sure to have the activities signed off regularly to ensure progress is tracked. Download the provided project and open it in CLion. ### Activity 1: The ctype header file int isalpha(int c) Deze functie is nuttig om te kijken of een char alfafbetisch is. ### Activity 2: Count letters in a string ```c unsigned long countLetters(unsigned long counts[static 26], const char* str) { ... } int main(void){ unsigned long counts[26] = {0}; const char* text = "The Quick Brown Fox Jumps Over The Lazy Dog."; unsigned long total = countLetters(&counts[0], text); assert(35 == total); printCounts(&counts[0], false, true); } ``` ### Activity 3: Recognizing languages ```c const char* makeSignature(unsigned long counts[static 26]) { ... } int main(void) { unsigned long counts1[26] = {15,3,4,5,16,6,7,8,9,7,6,3,2,11,14,1,2,12,13}; unsigned long counts2[26] = {16,4,7,5,20,7,4,3,14,5,9,1,2,18,6,12,9,13,9,15}; assert(strcmp("eaosrn", makeSignature(counts1)) == 0); assert(strcmp("enatir", makeSignature(counts2)) == 0); } ``` | File | Signature | Language | | ---------- | --------- | -------- | | alice0.txt | "etaoin" | "en"| | alice1.txt |"iantes"|"fi"| | alice2.txt |"eaionr"|"it"| | alice3.txt |"enisra"|"de"| ### Activity 4: Find out: dictionaries around us bsn nummer - persoon barcode - product mac adress - apparaat kenteken - voertuig corona qr code - persoon ### Activity 5: Find out: dictionaries in other languages python dict d['mynewkey'] = 'mynewvalue' c# dictionary Add('A', "Alpha"); rust hasmap c++ unordered_map typescript dict ### Activity 6: The ensureCapacity function ```c bool _ensureCapacity(counter_t* counter, size_t minimumCapacity) { ... } ``` ```c= bool _ensureCapacity(counter_t* counter, const size_t minimumCapacity){ size_t new_capacity = counter->capacity; while (minimumCapacity > new_capacity){ new_capacity = new_capacity * GROWTH_FACTOR + 1; } if (new_capacity > counter->capacity){ realloc(counter, new_capacity * sizeof(pair_t)); return true; } return false; } ``` ### Activity 7: insertAt function ```c bool _insertAt(counter_t* counter, const size_t index, const char* key, unsigned long value) { ... } int main() { counter_t counter; initCounter(&counter, 0); insert(&counter, "Bob", 3); insert(&counter, "Dave", 4); insert(&counter, "Alice", 5); insert(&counter, "Eva", 3); insert(&counter, "Charlie", 7); assert(strcmp(counter.data[0].key, "Alice") == 0); assert(counter.data[0].value == 5); assert(strcmp(counter.data[1].key, "Bob") == 0); assert(counter.data[1].value == 3); assert(strcmp(counter.data[2].key, "Charlie") == 0); assert(counter.data[2].value == 7); assert(strcmp(counter.data[3].key, "Dave") == 0); assert(counter.data[3].value == 4); assert(strcmp(counter.data[4].key, "Eva") == 0); assert(counter.data[4].value == 3); assert(counter.size == 5); assert(counter.capacity >= counter.size); destroyCounter(&counter); } ``` ```c= static bool _insertAt(counter_t* counter, const size_t index, const char* key, unsigned long value){ (void)index;// prevents unused-parameter error (void)key;// prevents unused-parameter error (void)value;// prevents unused-parameter error if (_ensureCapacity(counter, counter->size + 1)) { // TODO: implement insertion as described in activity 7 pair_t pair = makePair(key, value); for (int i = counter->capacity-1; i > index; --i) { counter->data[i] = counter->data[i-1]; } counter->data[index] = pair; counter->size ++; return true; } return false; } ``` ### Activity 8: Increment function ```c unsigned long increment(counter_t* counter, const char* key) { ... } int main(void) { counter_t counter; initCounter(&counter, 0); increment(&counter, "Alice"); increment(&counter, "Alice"); increment(&counter, "Alice"); increment(&counter, "Bob"); increment(&counter, "Bob"); assert(counter.size == 2); assert(counter.data[0].value == 3); assert(counter.data[1].value == 2); destroyCounter(&counter); } ``` ```c= unsigned long increment(counter_t *counter, const char *key) { // TODO: implement the increment function as described in Activity 8 // TIP: you can use the existing functions to do it // or implement an own version (that's potentially a bit faster) (void) counter;// prevents unused-parameter error (void) key;// prevents unused-parameter error auto value = getOrDefault(counter, key, 0); insert(counter, key, value + 1); return 0; } ``` ### Activity 9: Find out: function `strtok` ```c= void processLine(counter_t* counter, char* line) { // separator characters const char* sep = "\t (),.;:?!\"\n'"; //initalieert een char array char* token = strtok(line, sep); //splits de line op " " while (token) { //loopt door elk woord heen if (strlen(token)) // kijkt of token karakters heeft increment(counter, token); // inrecrement de token key token = strtok(NULL, sep); // gaat naar het volgende woord } } ``` ### Activity 10: How many words? Record your answer here ## Looking back ### What we've learnt Formulate at least one lesson learned. ### What were the surprises Fill in... ### What problems we've encountered Fill in... ### What was or still is unclear Fill in... ### How did the group perform? How was the collaboration? What were the reasons for hick-ups? What worked well? What can be improved next time? > Written with [StackEdit](https://stackedit.io/).