1. function1 ratkaisee yksinkertaisia ensimmäisen asteen yhtälöitä. function2 kelpaa kaksi vastausta johtuen tehtävään livahtaneesta virheestä. joko: testaa onko bittijono palindromi. tai: palauttaa aina 0. function3 lukee annetun tiedoston ja palauttaa sen sisällön ja koon pointterien kautta. function1 solves simple first order equations. function2 has two valid answers due to a minor glitch in the code. either the function tests that a given vector of bits is a palindrome or that the function always returns 0 function3 reads the entire file and returns the contents and size of the file through pointers. 2. #include char *decodeString(const char *code) { size_t size = 0; for (const char *ptr = code;*ptr;ptr+=2) size+= *ptr - '0'; char *result = malloc(size + 1); size_t p = 0; for (const char *ptr = code;*ptr;ptr+=2) for (char i = '0';i < *ptr;i++) result[p++] = ptr[1]; result[p] = '\0'; return result; } 3. A: 18 fd e5 B: f7 C: c8 D: b9 E: 1f 4. Mc* McConstruct(const char* genome, size_t nCa) { Mc *mc = malloc(sizeof(Mc)); mc->genome = malloc(strlen(genome) + 1); strcpy(mc->genome, genome); mc->nCa = nCa; mc->parent = NULL; return mc; } void McDestruct(Mc* mc) { if (!mc) return; McDestruct(mc->parent); free(mc->genome); free(mc); } Mc* McMitosis(Mc* mc) { mc->nCa /= 2; Mitochondrion* new = McConstruct(mc->genome, mc->nCa); new->parent = mc; return new; } int McMutation(Mc mc) { for (Mc *p = &mc;p;p = p->parent) if (strcmp(mc.genome, p->genome)) return 1; return 0; } 5. A) Rivi 10: strArrayCreaten pitäisi varata ja alustaa tyhjä strArray eikä palauttaa NULL, eli: char **new = malloc(sizeof(char *)); *new = NULL; return new; B) Rivi 19: strlen(str + 1) pitäisi olla strlen(str) + 1 C) Rivi 21: strArrayAdd ei aseta lopettavaa NULL arvoa, eli ennen returnia: arr[s + 1] = NULL; D) Rivi 29: ehdon str == *tmp pitäisi olla !strcmp(str, *tmp) E) Rivi 33: ennen taulukon tiivistämistä, alkio pitäisi vapauttaa: free(*tmp); F) Rivi 44: kerroin sizeof(char *) on väärin, sitä ei tarvita ollenkaan. -----In english------ A) Row 10: strArrayCreate should allocate and initialise an empty strArray rather than return NULL: char **new = malloc(sizeof(char *)); *new = NULL; return new; B) Row 19: strlen(str + 1) should be strlen(str) + 1 C) Row 21: strArrayAdd does not set the terminating NULL value, so before the return: arr[s + 1] = NULL; D) Row 29: The condition str == *tmp should be !strcmp(str, *tmp) E) Row 33: Before moving the end of the array, the element should be freed: free(*tmp); F) Row 44: The factor sizeof(char*) is wrong and is not needed at all 6. int compareFunctions(int from, int to, double (*f1)(int), double (*f2)(int)) { for (int i = from;i < to;i++) if (f1(i) > f2(i)) return 0; return 1; } int monotonicIncrease(int from, int to, double (*f)(int)) { for (int i = from + 1;i < to;i++) if (f(i) <= f(i - 1)) return 0; return 1; }