├── .gitignore ├── .vscode └── tasks.json ├── exams ├── exam-1-2022.c ├── exam-2-2022.c ├── exam-3-2022.c └── other │ ├── strcmp-example.c │ └── understand-pointers.c ├── week1 ├── complex-calc.c └── other │ └── extra-excercises.c ├── week2 ├── arrays-strings.c └── other │ ├── binary.c │ ├── book-program.c │ ├── char-input.c │ ├── extra-excercises.c │ ├── learning.c │ └── powers.c ├── week3 ├── matlab.c └── other │ ├── bubble-sort.c │ └── merge-sort.c ├── week4 ├── other │ ├── ca.c │ ├── extra-excercises.c │ ├── extra-excercises3.c │ ├── extra-exercises2.c │ ├── index.c │ ├── pointer-adress.c │ ├── pointers.c │ └── reverse.c └── sorting-and-pointers.c ├── week5 ├── malloc-sorting.c └── other │ ├── arrays-of-pointers.c │ ├── heap.c │ └── history.c ├── week6 ├── navigation.c └── other │ ├── c-program.c │ └── common-divisor.s ├── week7 ├── concordance.c └── other │ ├── checking.c │ ├── checking2.c │ ├── files.c │ └── test.c └── week8 └── practice-exam.c /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !/**/ 3 | !*.* 4 | *.exe -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc.exe build active file", 6 | "command": "C:\\msys64\\mingw64\\bin\\gcc.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /exams/exam-1-2022.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // #include "predefined.h" /* Do not delete this line. Do not define row_t yourself. */ 6 | 7 | typedef struct _product_t { 8 | char *product; 9 | float price; 10 | } product_t; 11 | 12 | product_t * newDatabase(product_t *database, int *dbSize, int newSize) { 13 | 14 | // clean previous database 15 | if (*dbSize != 0) { 16 | for (int i = 0; i<*dbSize; i++) { 17 | free(database[i].product); 18 | } 19 | free(database); 20 | } 21 | 22 | if (newSize != 0) { 23 | database = (product_t *)malloc(newSize*sizeof(product_t)); 24 | } 25 | *dbSize = newSize; 26 | 27 | for (int i = 0; i<*dbSize; i++) { 28 | database[i].product = NULL; 29 | database[i].price = -1; 30 | } 31 | 32 | return database; 33 | } 34 | 35 | void addProduct(product_t *database, int dbSize, char *product, float price) { 36 | // malloc min size of product and use that to store in database.product 37 | // we add +1 to strlen because it wont return the full size with the final \0 variable 38 | char *temp = (char *)malloc((strlen(product)+1)*sizeof(char)); 39 | strcpy(temp, product); 40 | 41 | // check if database full 42 | if ((dbSize == 0) || (database[dbSize-1].product != NULL)) { 43 | printf("The database is full\n"); 44 | free(temp); 45 | return; 46 | } 47 | 48 | // check if such product already exists 49 | // add the product to the first NULL in order 50 | for (int i = 0; i 0) { 71 | swap = database[j]; 72 | database[j] = database[j+1]; 73 | database[j+1] = swap; 74 | } 75 | } 76 | } 77 | } 78 | 79 | } 80 | 81 | void printDatabase(product_t *database, int dbSize) { 82 | for (int i = 0; i database[j+1].price) { 97 | swap = database[j]; 98 | database[j] = database[j+1]; 99 | database[j+1] = swap; 100 | } 101 | else if (database[j].price == database[j+1].price) { 102 | if (strcmp(database[j].product, database[j+1].product) > 0) { 103 | swap2 = database[j]; 104 | database[j] = database[j+1]; 105 | database[j+1] = swap2; 106 | } 107 | } 108 | } 109 | } 110 | } 111 | } 112 | 113 | void readFile(product_t *database, int dbSize, char *fileName) { 114 | char fileProduct[20]; 115 | float filePrice; 116 | FILE *fp; 117 | fp = fopen(fileName, "r"); 118 | if (fp == NULL) { 119 | printf("Cannot open file %s\n", fileName); 120 | return; 121 | } 122 | else { 123 | while (fscanf(fp, "%s %f", fileProduct, &filePrice) != EOF) { 124 | addProduct(database, dbSize, fileProduct, filePrice); 125 | } 126 | } 127 | } 128 | 129 | product_t * findProduct(product_t *database, int dbSize, char *product, int lower, int upper) { 130 | // returns pointer to product or NULL if not in db 131 | // see if equal with middle if not search one half, then half again then other half 132 | 133 | // print: The price of "product" is "price" 134 | // or 135 | // print: Product not found 136 | 137 | int mid = (lower+upper)/2; 138 | printf("find range (%d,%d)\n", lower, upper); 139 | 140 | if (lower > upper) { 141 | // wrong input 142 | return NULL; 143 | } 144 | 145 | if (strcmp(database[mid].product, product) == 0) { 146 | return &database[mid]; 147 | } 148 | else if (strcmp(database[mid].product, product) > 0) { 149 | return findProduct(database, dbSize, product, lower, mid - 1); 150 | } 151 | else { 152 | return findProduct(database, dbSize, product, mid + 1, upper); 153 | } 154 | } 155 | 156 | int countAmountofProductinDB(product_t *database, int dbSize) { 157 | int counter = 0; 158 | for (int i = 0; iprice); 234 | } 235 | else { 236 | printf("Product not found\n"); 237 | } 238 | break; 239 | } 240 | default: 241 | { 242 | printf("Unknown command '%c'\n", cmd); 243 | break; 244 | } 245 | } 246 | } while (cmd != 'q'); 247 | } -------------------------------------------------------------------------------- /exams/exam-2-2022.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // #include "predefined.h" /* Do not delete this line. Do not define row_t yourself. */ 6 | 7 | #define MAXROWS 11 8 | #define MAXINPUT 12 9 | typedef struct _row_t { 10 | int nrElements; 11 | int *elements; 12 | } row_t; 13 | 14 | void printMatrix(row_t matrix[MAXROWS]) { 15 | // check if matrix empty 16 | if (matrix[0].nrElements == 0) { 17 | printf("The matrix is empty\n"); 18 | return; 19 | } 20 | 21 | printf("Matrix:\n"); 22 | for (int i = 0; i matrix[j+1].nrElements) { 205 | row_t temp = matrix[j+1]; 206 | matrix[j+1] = matrix[j]; 207 | matrix[j] = temp; 208 | printf("Swapped %i and %i\n", j, j+1); 209 | pass = 1; 210 | } 211 | } 212 | } 213 | if (pass == 1) { 214 | printf("Do another pass\n"); 215 | } 216 | } 217 | } 218 | 219 | int main (void) 220 | { 221 | char cmd; 222 | row_t matrix[MAXROWS]; 223 | for (int i=0; i 2 | #include 3 | #include 4 | #include 5 | // #include "predefined.h" /* Do not delete this line. Do not define MAXWORDS. */ 6 | 7 | #include MAXWORDS 11 8 | 9 | void printWordList(char *wordList[MAXWORDS]) { 10 | if (wordList[0] == NULL) { 11 | return; 12 | } 13 | for (int i = 0; i 0) { 40 | char * tempWord = wordList[j]; 41 | wordList[j] = wordList[j+1]; 42 | wordList[j+1] = tempWord; 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | void vowelChange (char *wordList[MAXWORDS]) { 50 | if (wordList[0] == NULL) { 51 | return; 52 | } 53 | 54 | for (int i = 0; i strlen(wordList[j+1])) { 101 | printf("Swapped %i and %i\n", j, j+1); 102 | char * swap = wordList[j]; 103 | wordList[j] = wordList[j+1]; 104 | wordList[j+1] = swap; 105 | swapped = 1; 106 | } 107 | } 108 | } 109 | if (swapped == 1) { 110 | printf("Do another pass\n"); 111 | } 112 | } 113 | } 114 | 115 | void shareWord(char *wordList[MAXWORDS], char *word) { 116 | int exists = 0; 117 | for (int i = 0; i 2 | #include 3 | 4 | int main() { 5 | char str1[20] = "hello"; 6 | char str2[20] = "world"; 7 | 8 | int result = strcmp(str1, str2); 9 | if (result == 0) { 10 | printf("The strings are equal.\n"); 11 | } else if (result < 0) { 12 | printf("'%s' is less than '%s'.\n", str1, str2); 13 | } else { 14 | printf("'%s' is greater than '%s'.\n", str1, str2); 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /exams/other/understand-pointers.c: -------------------------------------------------------------------------------- 1 | // integer x is set to 4 2 | int x = 4; 3 | 4 | // integer pointer pX is set to (address) of x 5 | int * pX = &x; 6 | 7 | // now we can access x by reference and NOT BY VALUE 8 | 9 | // integer y is set to the thing pointed to by pX 10 | int y = *pX; 11 | -------------------------------------------------------------------------------- /week1/complex-calc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char imm_signChecker(float curr_imm) 4 | { 5 | if (curr_imm<0) 6 | { 7 | return '-'; 8 | } 9 | return '+'; 10 | } 11 | 12 | void printVal(float curr_re, float curr_imm) 13 | { 14 | char imm_sign = imm_signChecker(curr_imm); 15 | if (imm_sign == '-') 16 | { 17 | printf("Current value is %f%fi\n", curr_re,curr_imm); 18 | } 19 | else 20 | { 21 | printf("Current value is %f%c%fi\n", curr_re,imm_sign,curr_imm); 22 | } 23 | } 24 | 25 | int main(void) 26 | { 27 | char cmd; 28 | float curr_re = 0.0; 29 | float curr_imm = 0.0; 30 | float new_re = 0.0; 31 | float new_imm = 0.0; 32 | int nat_o = 0; 33 | 34 | printf("** Complex Calculator **\n"); 35 | 36 | do 37 | { 38 | printf("Operation [0+-*crpq]? "); 39 | scanf(" %c", &cmd); 40 | 41 | switch(cmd) 42 | { 43 | case ('q'): 44 | { 45 | printVal(curr_re, curr_imm); 46 | printf("Bye!\n"); 47 | break; 48 | } 49 | 50 | case ('+'): 51 | { 52 | printf("Complex operand? "); 53 | scanf(" %f %f", &new_re,&new_imm); 54 | curr_re+=new_re; 55 | curr_imm+=new_imm; 56 | printVal(curr_re, curr_imm); 57 | break; 58 | } 59 | 60 | case ('-'): 61 | { 62 | printf("Complex operand? "); 63 | scanf(" %f %f", &new_re,&new_imm); 64 | curr_re-=new_re; 65 | curr_imm-=new_imm; 66 | printVal(curr_re, curr_imm); 67 | break; 68 | } 69 | 70 | case ('0'): 71 | { 72 | curr_re = 0.0; 73 | curr_imm = 0.0; 74 | printVal(curr_re, curr_imm); 75 | break; 76 | } 77 | 78 | case ('c'): 79 | { 80 | curr_imm*=-1.0; 81 | printVal(curr_re, curr_imm); 82 | break; 83 | 84 | } 85 | 86 | case ('*'): 87 | { 88 | printf("Complex operand? "); 89 | scanf(" %f %f", &new_re,&new_imm); 90 | float tmp_re = curr_re * new_re - curr_imm * new_imm; 91 | float tmp_imm = curr_re * new_imm + curr_imm * new_re; 92 | curr_re = tmp_re; 93 | curr_imm = tmp_imm; 94 | printVal(curr_re, curr_imm); 95 | break; 96 | } 97 | 98 | case ('r'): 99 | { 100 | printf("Natural operand? "); 101 | scanf(" %d", &nat_o); 102 | float multiplier_re = curr_re; 103 | float multiplier_imm = curr_imm; 104 | 105 | for (int i=1; i=-10; i--) 128 | { 129 | for (int j = -10; j<=10; j++) 130 | { 131 | if ((curr_re >= j && curr_re < j+1) && (curr_imm >= i && curr_imm < i+1)) printf("*"); 132 | else if (i == 0 && j==0) printf("+"); 133 | else if (i == 0 && j!=0) printf("-"); 134 | else if (j == 0 && i!=0) printf("|"); 135 | else printf("."); 136 | 137 | } 138 | printf("\n"); 139 | } 140 | printVal(curr_re, curr_imm); 141 | break; 142 | } 143 | 144 | default: 145 | { 146 | printf("Invalid command \'%c\'\n", cmd); 147 | printVal(curr_re, curr_imm); 148 | break; 149 | } 150 | } 151 | 152 | } while (cmd!='q'); 153 | } 154 | -------------------------------------------------------------------------------- /week1/other/extra-excercises.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int n; 7 | void print_statements(); 8 | void loops(); 9 | void operators(); 10 | void input(); 11 | void if_statements(); 12 | void complex_programs(); 13 | 14 | int main(void) 15 | { 16 | printf("Which function would you like to run? "); 17 | scanf("%d", &n); 18 | 19 | if (n == 2) 20 | { 21 | print_statements(); 22 | } 23 | else if (n == 3) 24 | { 25 | loops(); 26 | } 27 | else if (n == 4) 28 | { 29 | operators(); 30 | } 31 | else if (n == 5) 32 | { 33 | input(); 34 | } 35 | else if (n == 6) 36 | { 37 | if_statements(); 38 | } 39 | else if (n == 7) 40 | { 41 | complex_programs(); 42 | } 43 | else 44 | { 45 | printf("Invalid input. Please try again.\n"); 46 | } 47 | } 48 | 49 | void print_statements() 50 | { 51 | // 2 52 | printf("Hello, there!\n"); 53 | 54 | printf("Hello, there!\nHow are you?\n"); 55 | 56 | printf("Hello"); 57 | printf("\nHow "); 58 | printf("are y"); 59 | printf("ou?\n"); 60 | 61 | int i1 = 10, i2 = 200; 62 | printf("i1=%d i2=%d\n", i1, i2); 63 | 64 | int j, j1 = 10, j2 = 200, j3 = 3000, j4 = 40000; 65 | j = j1; 66 | printf("%d\n", j); 67 | j = j2; 68 | printf("%d\n", j); 69 | j = j3; 70 | printf("%d\n", j); 71 | j = j4; 72 | printf("%d\n", j); 73 | 74 | int g, g1 = 10, g2 = 200, g3 = 3000, g4 = 40000; 75 | g = g1; 76 | printf("%5d\n", g); 77 | g = g2; 78 | printf("%5d\n", g); 79 | g = g3; 80 | printf("%5d\n", g); 81 | g = g4; 82 | printf("%5d\n", g); 83 | } 84 | 85 | void loops() 86 | { 87 | // 3 88 | int i; 89 | for (i = 0; i < 9; i++) 90 | { 91 | printf("%d\n", i); 92 | } 93 | printf("\n"); 94 | 95 | int a; 96 | for (a = 0; a < 9; a++) 97 | { 98 | printf("%d\n", 9 - a); 99 | } 100 | printf("\n"); 101 | 102 | int b; 103 | for (b = 9; b >= 0; b--) 104 | { 105 | printf("%d\n", b); 106 | } 107 | printf("\n"); 108 | 109 | int c; 110 | for (c = 9; c > -1; c--) 111 | { 112 | printf("%d\n", c); 113 | } 114 | printf("\n"); 115 | 116 | int d; 117 | for (d = 0; 9 >= d; d++) 118 | { 119 | printf("%d\n", 9 - d); 120 | } 121 | printf("\n"); 122 | 123 | int e = 0; 124 | while (e < 9) 125 | { 126 | printf("%d\n", e); 127 | e++; 128 | } 129 | printf("\n"); 130 | 131 | int f = 0; 132 | do 133 | { 134 | printf("%d\n", f); 135 | f++; 136 | } while (f <= 9); 137 | printf("\n"); 138 | 139 | int g = 9; 140 | do 141 | { 142 | printf("%d\n", g); 143 | g--; 144 | } while (g >= 0); 145 | printf("\n"); 146 | 147 | int h = 9; 148 | do 149 | { 150 | h--; 151 | printf("%d\n", h); 152 | } while (h > 0); 153 | printf("\n"); 154 | 155 | // endless loop 156 | // for (;;); 157 | 158 | // endless hello world loop 159 | // for (;;) printf("Hello!\n"); 160 | 161 | // endless hello world loop 2 162 | // while(1) printf("Hello!\n"); 163 | 164 | // endless hello world loop 3 165 | // do { printf("Hello!\n"); } while (1); 166 | 167 | for (int j = 0; j < 6; j++) 168 | { 169 | for (int k = 0; k < j; k++) 170 | { 171 | printf("*"); 172 | } 173 | printf("\n"); 174 | } 175 | printf("\n"); 176 | 177 | for (int i = 0; i < 6; i++) 178 | { 179 | for (int j = 1; j < 6 - i; j++) 180 | { 181 | printf(" "); 182 | } 183 | for (int j = 0; j <= i; j++) 184 | { 185 | printf("*"); 186 | } 187 | printf("\n"); 188 | } 189 | printf("\n"); 190 | 191 | for (int i = 5; i >= 0; i--) 192 | { 193 | for (int j = 1; j < 6 - i; j++) 194 | { 195 | printf(" "); 196 | } 197 | for (int j = 0; j <= i; j++) 198 | { 199 | printf("*"); 200 | } 201 | printf("\n"); 202 | } 203 | printf("\n"); 204 | 205 | for (int i = 6; i > 0; i--) 206 | { 207 | for (int j = 0; j < i; j++) 208 | { 209 | printf("*"); 210 | } 211 | printf("\n"); 212 | } 213 | printf("\n"); 214 | 215 | for (int i = 0; i < 6; i++) 216 | { 217 | for (int j = 1; j < 6 - i; j++) 218 | { 219 | printf(" "); 220 | } 221 | for (int j = 0; j <= i * 2; j++) 222 | { 223 | printf("*"); 224 | } 225 | printf("\n"); 226 | } 227 | printf("\n"); 228 | 229 | for (int i = 0; i < 6; i++) 230 | { 231 | for (int j = 1; j < 6 - i; j++) 232 | { 233 | printf(" "); 234 | } 235 | for (int j = 0; j < i; j++) 236 | { 237 | printf("O*"); 238 | } 239 | printf("O\n"); 240 | } 241 | } 242 | 243 | void operators() 244 | { 245 | // 4 246 | int i; 247 | for (i = 0; i < 10; i++) 248 | { 249 | printf("%d %d %d\n", i, i % 3, i % 2); 250 | } 251 | printf("\n"); 252 | 253 | int j; 254 | for (j = 0; j < 10; j++) 255 | { 256 | printf("%d=3*%d+%d\n", j, j / 3, j % 3); 257 | } 258 | } 259 | 260 | void input() 261 | { 262 | // 5 263 | int i; 264 | printf("Integer? "); 265 | scanf("%d", &i); 266 | printf("You entered: %d\n", i); 267 | 268 | int i1, i2; 269 | printf("Integer one? "); 270 | scanf("%d", &i1); 271 | printf("Integer two? "); 272 | scanf("%d", &i2); 273 | printf("You entered %d and %d\n", i1, i2); 274 | 275 | int j1, j2; 276 | printf("Two Integegers? "); 277 | scanf("%d %d", &j1, &j2); 278 | printf("You entered %d and %d\n", j1, j2); 279 | } 280 | 281 | void if_statements() 282 | { 283 | // 6 284 | int i = 0; 285 | scanf("%d", i); 286 | if (i < 0) 287 | { 288 | printf("%d is a negative number\n", i); 289 | } 290 | else 291 | { 292 | if (i == 0) 293 | { 294 | printf("%d is zero\n", i); 295 | } 296 | else 297 | { 298 | printf("%d is a positive number\n", i); 299 | } 300 | } 301 | 302 | int a = 0; 303 | scanf("%d", a); 304 | if (!(a > 0 || a == 0)) 305 | { 306 | printf("%d is a negative number\n", a); 307 | } 308 | else 309 | { 310 | if (!(a < 0 || a == 0)) 311 | { 312 | printf("%d is a psoitive number\n", a); 313 | } 314 | else 315 | { 316 | printf("%d is zero\n", a); 317 | } 318 | } 319 | 320 | int b; 321 | for (b = 0; b < 6; b++) 322 | { 323 | if (b % 2 == 0) 324 | printf("%d is even", b); 325 | else 326 | printf("%d is odd", b); 327 | if (b < 3) 328 | { 329 | printf("%d and less than 3\n", b); 330 | } 331 | else 332 | { 333 | if (b > 3) 334 | printf("%d and greater than 3\n", b); 335 | else 336 | printf("%d and equal to 3\n", b); 337 | } 338 | } 339 | 340 | int z; 341 | for (z = 0; z < 6; z++) 342 | { 343 | if (z % 2 == 0 && z < 3) 344 | printf("%d is even and less than 3\n", z); 345 | if (z % 2 == 0 && z > 3) 346 | printf("%d is even and greater than 3\n", z); 347 | if (z % 2 == 1 && z < 3) 348 | printf("%d is odd and less than 3\n", z); 349 | if (z % 2 == 1 && z > 3) 350 | printf("%d is odd and greater than 3\n", z); 351 | if (z == 3) 352 | printf("3 is odd and equal to 3\n"); 353 | } 354 | 355 | for (i = 0; i < 6; i++) 356 | { 357 | if (i == 3) 358 | printf("3 is odd and equal to 3\n"); 359 | else if (!(i % 2 != 0 || i > 3)) 360 | printf("%d is even and less than 3\n", i); 361 | else if (!(i % 2 != 0 || i < 3)) 362 | printf("%d is even and greater than 3\n", i); 363 | else if (!(i % 2 != 1 || i > 3)) 364 | printf("%d is odd and less than 3\n", i); 365 | else if (!(i % 2 != 1 || i < 3)) 366 | printf("%d is odd and greater than 3\n", i); 367 | } 368 | } 369 | 370 | void complex_programs() 371 | { 372 | // 7 373 | // fabonacci 374 | int first = 1, second = 1, sum = 2; 375 | printf("1\n1\n"); 376 | while (sum < 100) 377 | { 378 | printf("%d\n", sum); 379 | first = second; 380 | second = sum; 381 | sum = first + second; 382 | } 383 | printf("\n"); 384 | 385 | // prime numbers 386 | int i = 11; 387 | int divisors = 0; 388 | for (int d = 1; d <= i; d++) 389 | { 390 | if (i % d == 0) 391 | divisors++; 392 | } 393 | if (divisors == 2) 394 | printf("%d is a prime\n", i); 395 | else 396 | printf("%d is not a prime\n", i); 397 | 398 | printf("\n"); 399 | 400 | // optimized prime numbers 401 | int j = 11; 402 | int divisors2 = 2; 403 | for (int d = 2; d < j && divisors2 == 2; d++) 404 | { 405 | if (j % d == 0) 406 | divisors2++; 407 | } 408 | if (divisors2 == 2) 409 | printf("%d is a prime\n", j); 410 | else 411 | printf("%d is not a prime\n", j); 412 | 413 | printf("\n"); 414 | 415 | // normal to roman numbers 416 | int stop = 0; 417 | do 418 | { 419 | int i; 420 | printf("Number? "); 421 | scanf("%d", &i); 422 | if (i == 0) 423 | stop = 1; 424 | while (i >= 1000) 425 | { 426 | printf("M"); 427 | i -= 1000; 428 | } 429 | while (i >= 500) 430 | { 431 | printf("D"); 432 | i -= 500; 433 | } 434 | while (i >= 100) 435 | { 436 | printf("C"); 437 | i -= 100; 438 | } 439 | while (i >= 50) 440 | { 441 | printf("L"); 442 | i -= 50; 443 | } 444 | while (i >= 10) 445 | { 446 | printf("X"); 447 | i -= 10; 448 | } 449 | while (i >= 5) 450 | { 451 | printf("V"); 452 | i -= 5; 453 | } 454 | while (i >= 1) 455 | { 456 | printf("I"); 457 | i -= 1; 458 | } 459 | if (!stop) 460 | printf("\n"); 461 | } while (!stop); 462 | printf("Bye!\n"); 463 | printf("\n"); 464 | 465 | // valid values checker 466 | int k, valid_values; 467 | printf("Integer? "); 468 | valid_values = scanf("%d", &k); 469 | if (valid_values == 1) 470 | printf("You entered: %d\n", k); 471 | else 472 | printf("You did not enter an integer!\n"); 473 | } -------------------------------------------------------------------------------- /week2/arrays-strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define LENGTH 30 4 | // declaring constant array lengths 5 | char inputString[LENGTH]; 6 | char inputChar[LENGTH]; 7 | char inputOccurrence[LENGTH]; 8 | char inputCharacter[LENGTH]; 9 | char inputPosition[LENGTH]; 10 | 11 | char inputReplace[LENGTH]; 12 | char inputReplaceWith[LENGTH]; 13 | 14 | char inputIndex1[LENGTH]; 15 | char inputIndex2[LENGTH]; 16 | 17 | void printArray(char a[], int length) 18 | { 19 | printf("["); 20 | for (int i = 0; i < length; i++) 21 | { 22 | if (a[i] == '\0') 23 | { 24 | printf("\\0"); 25 | } 26 | else if (a[i] == '\n') 27 | { 28 | printf("\\n"); 29 | } 30 | else 31 | { 32 | printf("%c", a[i]); 33 | } 34 | if (i < length - 1) 35 | { 36 | printf(","); 37 | } 38 | } 39 | printf("]\n"); 40 | } 41 | 42 | void printString(char str[]) 43 | { 44 | printf("\""); 45 | int i = 0; 46 | while (str[i] != '\0') 47 | { 48 | putchar(str[i]); 49 | i++; 50 | } 51 | printf("\""); 52 | printf("\n"); 53 | } 54 | 55 | void readLine(char str[], int length) 56 | { 57 | int i = 0; 58 | char c; 59 | while ((c = getchar()) != '\n') 60 | { 61 | if (i < length - 1) 62 | { 63 | str[i] = c; 64 | i++; 65 | } 66 | } 67 | str[i] = '\0'; 68 | } 69 | 70 | int findFirstOccurrence(char str[], char aChar) 71 | { 72 | int i = 0; 73 | while (str[i] != '\0') 74 | { 75 | if (str[i] == aChar) 76 | { 77 | return i; 78 | } 79 | i++; 80 | } 81 | return -1; 82 | } 83 | 84 | int readInt(char str[], int length) 85 | { 86 | int i = 0; 87 | int result = 0; 88 | while (str[i] != '\0') 89 | { 90 | if (str[i] >= '0' && str[i] <= '9') 91 | { 92 | result = result * 10 + (str[i] - '0'); 93 | } 94 | else 95 | { 96 | break; 97 | } 98 | i++; 99 | } 100 | return result; 101 | } 102 | 103 | void insertChar(char str[], char aChar, int index) 104 | { 105 | int i = 0; 106 | while (str[i] != '\0') 107 | { 108 | i++; 109 | } 110 | while (i >= index) 111 | { 112 | str[i + 1] = str[i]; 113 | i--; 114 | } 115 | str[index] = aChar; 116 | } 117 | 118 | void replaceChars(char str[], char fromString[], char toChar) 119 | { 120 | int i = 0; 121 | while (str[i] != '\0') 122 | { 123 | int j = 0; 124 | while (fromString[j] != '\0') 125 | { 126 | if (str[i] == fromString[j]) 127 | { 128 | str[i] = toChar; 129 | } 130 | j++; 131 | } 132 | i++; 133 | } 134 | } 135 | 136 | void stringReorder(char str[], int index1, int index2) 137 | { 138 | int i = 0; 139 | int j = 0; 140 | int k = 0; 141 | char substring1[LENGTH]; 142 | char substring2[LENGTH]; 143 | char substring3[LENGTH]; 144 | while (str[i] != '\0') 145 | { 146 | if (i < index1) 147 | { 148 | substring1[j] = str[i]; 149 | j++; 150 | } 151 | else if (i < index2) 152 | { 153 | substring2[k] = str[i]; 154 | k++; 155 | } 156 | else 157 | { 158 | substring3[i - index2] = str[i]; 159 | } 160 | i++; 161 | } 162 | substring1[j] = '\0'; 163 | substring2[k] = '\0'; 164 | substring3[i - index2] = '\0'; 165 | i = 0; 166 | j = 0; 167 | k = 0; 168 | while (substring3[i] != '\0') 169 | { 170 | str[i] = substring3[i]; 171 | i++; 172 | } 173 | while (substring2[j] != '\0') 174 | { 175 | str[i] = substring2[j]; 176 | i++; 177 | j++; 178 | } 179 | while (substring1[k] != '\0') 180 | { 181 | str[i] = substring1[k]; 182 | i++; 183 | k++; 184 | } 185 | str[i] = '\0'; 186 | } 187 | 188 | 189 | int main() 190 | { 191 | do 192 | { 193 | printf("Command? "); 194 | readLine(inputChar, LENGTH); 195 | switch (inputChar[0]) 196 | { 197 | 198 | case 'q': 199 | printf("Bye!\n"); 200 | break; 201 | 202 | case 'p': 203 | printf("The current string is: "); 204 | printString(inputString); 205 | break; 206 | 207 | case 'a': 208 | printf("The current array is: "); 209 | printArray(inputString, LENGTH); 210 | break; 211 | 212 | case 's': 213 | printf("Please enter a string? "); 214 | readLine(inputString, LENGTH); 215 | break; 216 | 217 | case 'o': 218 | printf("Find first occurrence of which character? "); 219 | readLine(inputOccurrence, LENGTH); 220 | int index = findFirstOccurrence(inputString, inputOccurrence[0]); 221 | printf("The first occurrence of '%c' is at index %d\n", inputOccurrence[0], index); 222 | break; 223 | 224 | case 'i': 225 | printf("Insert which character? "); 226 | readLine(inputCharacter, LENGTH); 227 | printf("At what index? "); 228 | readLine(inputPosition, LENGTH); 229 | int finalIndex = readInt(inputPosition, LENGTH); 230 | insertChar(inputString, inputCharacter[0], finalIndex); 231 | break; 232 | 233 | case 'r': 234 | printf("Replace which characters? "); 235 | readLine(inputReplace, LENGTH); 236 | printf("with which character? "); 237 | readLine(inputReplaceWith, LENGTH); 238 | replaceChars(inputString, inputReplace, inputReplaceWith[0]); 239 | break; 240 | 241 | case 'R': 242 | printf("Please enter index 1? "); 243 | readLine(inputIndex1, LENGTH); 244 | printf("Please enter index 2? "); 245 | readLine(inputIndex2, LENGTH); 246 | 247 | int index1 = readInt(inputIndex1, LENGTH); 248 | int index2 = readInt(inputIndex2, LENGTH); 249 | 250 | stringReorder(inputString, index1, index2); 251 | break; 252 | 253 | default: 254 | printf("Unknown command '%c'\n", inputChar[0]); 255 | break; 256 | } 257 | } while (inputChar[0] != 'q'); 258 | } 259 | -------------------------------------------------------------------------------- /week2/other/binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int u; 7 | printf("number? "); 8 | // undefined binary 9 | scanf("%u", &u); 10 | for (int i = 0; i < 32; i++) 11 | { 12 | // binary representation 13 | if (u & (1 << i)) 14 | printf("bit %d is 1\n", i); 15 | } 16 | } -------------------------------------------------------------------------------- /week2/other/book-program.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void rev(char s[]) 5 | { 6 | int c, i, j; 7 | for (i = 0, j = strlen(s) - 1; i < j; i++, j--) 8 | { 9 | c = s[i]; 10 | s[i] = s[j]; 11 | s[j] = c; 12 | } 13 | printf("%s\n", s); 14 | } 15 | 16 | int main(void) 17 | { 18 | char s[100]; 19 | printf("Enter a string: "); 20 | // including spaces 21 | scanf("%[^\n]s", s); 22 | rev(s); 23 | } -------------------------------------------------------------------------------- /week2/other/char-input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | char c; 7 | int first = 1; 8 | printf("one line? "); 9 | do 10 | { 11 | scanf("%c", &c); 12 | if (first) 13 | { 14 | printf("\""); 15 | } 16 | // so the if statement is not called again 17 | first = 0; 18 | 19 | if (c != "\n") 20 | { 21 | printf("%c", c); 22 | } 23 | } while (c != "\n"); 24 | printf("\"\n"); 25 | } -------------------------------------------------------------------------------- /week2/other/extra-excercises.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void basic_data_types() 7 | { 8 | int a = 1; 9 | float b = 2; // automatically converted to 2.0 10 | double c = 3; // automatically converted to 3.0 11 | printf("integer %d\n", a); 12 | printf("float %f\n", b); 13 | printf("double %e\n", c); 14 | 15 | int d; 16 | float e; 17 | printf("integer? "); 18 | scanf("%d", &d); 19 | printf("float? "); 20 | scanf("%f", &e); 21 | printf("integer %+06d\n", d); 22 | printf("float %06.2d\n", e); 23 | printf("float %+06.2e\n", e); 24 | 25 | // with such method its possible to skip new lines and spaces 26 | char f; 27 | printf("character? "); 28 | scanf(" %c", &f); 29 | printf("character ’%c’\n", f); 30 | 31 | char g; 32 | printf("character? "); 33 | scanf("%c", &g); 34 | // or c = getchar(); 35 | printf("character ’%c’\n", g); 36 | } 37 | 38 | void integer_arrays() 39 | { 40 | #define N 6 41 | 42 | int a[N] = {0, 0, 0, 0, 0, 0}; 43 | // int a[] = { 0, 0, 0, 0, 0, 0 }; // the length 6 is computed automatically 44 | // int a[N] = { 0 }; // the remaining values are initialised to 0 automatically 45 | for (int i = 0; i < N; i++) 46 | { 47 | printf("%d,", a[i]); 48 | } 49 | printf("\n"); 50 | 51 | int b[N]; // unitialised, contains random values 52 | for (int i = 0; i < N; i++) 53 | { 54 | printf("value %d? ", i); 55 | scanf("%d", &b[i]); 56 | } 57 | for (int i = 0; i < N; i++) 58 | { 59 | printf("%d,", b[i]); 60 | } 61 | printf("\n"); 62 | 63 | int c[N]; // unitialised, contains random values 64 | for (int i = 0; i < N; i++) 65 | { 66 | printf("value %d? ", i); 67 | scanf("%d", &c[i]); 68 | } 69 | for (int i = 0; i < N; i++) 70 | { 71 | if (i != N - 1) 72 | printf("%d,", c[i]); 73 | else 74 | printf("%d\n", c[i]); 75 | } 76 | // alternatively: 77 | for (int i = 0; i < N - 1; i++) 78 | printf("%d,", c[i]); 79 | printf("%d\n", c[N - 1]); 80 | // be careful not to accidentally use a[N], which is undefined (and wrong) 81 | 82 | int d[N]; // unitialised, contains random values 83 | int min, max, sum; 84 | for (int i = 0; i < N; i++) 85 | { 86 | printf("value %d? ", i); 87 | scanf("%d", &d[i]); 88 | } 89 | min = d[0]; 90 | max = d[0]; 91 | sum = 0; 92 | for (int i = 0; i < N; i++) 93 | { 94 | printf("%d,", d[i]); 95 | if (d[i] < min) 96 | min = d[i]; 97 | if (d[i] > max) 98 | max = d[i]; 99 | sum = sum + d[i]; 100 | } 101 | printf("\n"); 102 | printf("min=%d\n", min); 103 | printf("max=%d\n", max); 104 | printf("sum=%d\n", sum); 105 | 106 | int e[N]; 107 | int reverse[N]; 108 | for (int i = 0; i < N; i++) 109 | { 110 | printf("value %d? ", i); 111 | scanf("%d", &e[i]); 112 | } 113 | for (int i = 0; i < N; i++) 114 | { 115 | reverse[N - 1 - i] = e[i]; 116 | } 117 | for (int i = 0; i < N; i++) 118 | { 119 | printf("%d,", reverse[i]); 120 | } 121 | printf("\n"); 122 | 123 | // Ask for the length of an array until it is between 1 and 100. Read all entries in the array from the 124 | // terminal. Print all even numbers in the array in reverse order, but without using a second array. 125 | #define B 100 126 | int f[B]; 127 | int length = 0; 128 | do 129 | { 130 | printf("length? "); 131 | scanf("%d", &length); 132 | } while (length < 1 || length > 100); 133 | for (int i = 0; i < length; i++) 134 | { 135 | printf("value %d? ", i); 136 | scanf("%d", &f[i]); 137 | } 138 | for (int i = length - 1; i >= 0; i--) 139 | { 140 | if (f[i] % 2 == 0) 141 | printf("%d\n", f[i]); 142 | } 143 | } 144 | 145 | void character_arrays() 146 | { 147 | #define A 6 148 | char a[A]; 149 | printf("characters? "); 150 | for (int i = 0; i < A; i++) 151 | { 152 | scanf("%c", &a[i]); 153 | } 154 | for (int i = 0; i < A; i++) 155 | { 156 | printf("’%c’,", a[i]); 157 | } 158 | printf("\n"); 159 | 160 | char b[A]; 161 | printf("characters? "); 162 | for (int i = 0; i < A; i++) 163 | { 164 | scanf("%c", &b[i]); 165 | } 166 | int digits = 0; 167 | for (int i = 0; i < A; i++) 168 | { 169 | if (b[i] >= '0' && b[i] <= '9') 170 | { 171 | printf("’%c’,", b[i]); 172 | digits++; 173 | } 174 | } 175 | if (digits > 0) 176 | printf("\n"); 177 | 178 | char c[A]; 179 | int number = 0; 180 | printf("characters? "); 181 | for (int i = 0; i < A; i++) 182 | { 183 | scanf("%c", &c[i]); 184 | } 185 | int debug = 0; 186 | for (int i = 0; i < A; i++) 187 | { 188 | if (c[i] >= '0' && c[i] <= '9') 189 | { 190 | number = number * 10; 191 | if (debug) 192 | printf("convert character ’%c’ to integer %d\n", c[i], c[i] - '0'); 193 | if (debug) 194 | printf("number = number(%d) + digit(%d)\n", number, c[i] - '0'); 195 | number += (c[i] - '0'); 196 | } 197 | } 198 | printf("%d\n", number); 199 | 200 | // Write a program that reads N=6 characters into an array. Compute and print the binary number corre- 201 | // sponding to the characters that are binary digits. 202 | char d[N]; 203 | int number1 = 0; 204 | printf("characters? "); 205 | for (int i = 0; i < N; i++) 206 | { 207 | scanf("%c", &d[i]); 208 | } 209 | for (int i = 0; i < N; i++) 210 | { 211 | if (d[i] >= '0' && d[i] <= '1') 212 | number1 = number1 * 2 + (a[i] - '1'); 213 | } 214 | printf("%d\n", number1); 215 | } 216 | 217 | int plus2(int a, int b) 218 | { 219 | return a + b; 220 | } 221 | int plus3(int a, int b, int c) 222 | { 223 | int ab = plus2(a, b); 224 | int abc = plus2(ab, c); 225 | return abc; 226 | } 227 | int plus6(int a[N]) 228 | { 229 | int sum = 0; 230 | for (int i = 0; i < N; i++) 231 | sum += a[i]; 232 | return sum; 233 | } 234 | int plusn(int a[], int n) 235 | { 236 | int sum = 0; 237 | for (int i = 0; i < n; i++) 238 | sum += a[i]; 239 | return sum; 240 | } 241 | int vectormult(int a[N], int b[N]) 242 | { 243 | int sum = 0; 244 | for (int i = 0; i < N; i++) 245 | sum += (a[i] * b[i]); 246 | return sum; 247 | } 248 | int vectormult2(int a[N], int b[N]) 249 | { 250 | int sum = 0; 251 | for (int i = 0; i < N; i++) 252 | sum = plus2(sum, (a[i] * b[i])); 253 | return sum; 254 | } 255 | float powr(float b, int e) 256 | { 257 | float r = 1; 258 | for (int i = 0; i < e; i++) 259 | r *= b; 260 | return r; 261 | } 262 | // recursive function version - calls itself 263 | float powr2(float b, int e) 264 | { 265 | if (e < 1) 266 | return 1.0; 267 | float powr_e_minus_one = powr2(b, e - 1); 268 | return b * powr_e_minus_one; 269 | } 270 | int sumn(int n) 271 | { 272 | if (n < 1) 273 | return 0; 274 | int sum_of_0_to_n_minus_one = sumn(n - 1); 275 | return sum_of_0_to_n_minus_one + n; 276 | } 277 | int mystrlen(char s[]) 278 | { 279 | int len = 0; 280 | while (s[len] != '\0') 281 | len++; 282 | return len; 283 | } 284 | void mystrcpy(char s[], char t[]) 285 | { 286 | int len = 0; 287 | while (s[len] != '\0') 288 | { 289 | t[len] = s[len]; 290 | len++; 291 | } 292 | } 293 | void functions() 294 | { 295 | printf("1\n"); 296 | int x, y; 297 | printf("integers? "); 298 | scanf("%d %d", &x, &y); 299 | printf("the sum is %d\n", plus2(x, y)); 300 | 301 | #define Z 6 302 | printf("3\n"); 303 | int a[Z], sum = 0; 304 | printf("integers? "); 305 | for (int i = 0; i < Z; i++) 306 | { 307 | scanf("%d", &a[i]); 308 | sum = plus2(sum, a[i]); 309 | } 310 | printf("the sum is %d\n", sum); 311 | 312 | printf("4\n"); 313 | int b[Z]; 314 | printf("integers? "); 315 | for (int i = 0; i < Z; i++) 316 | { 317 | scanf("%d", &b[i]); 318 | } 319 | printf("the sum is %d\n", plus3(0, plus3(b[0], b[1], b[2]), plus3(b[3], b[4], b[5]))); 320 | // of course, it would have been smarter to do: 321 | // printf("the sum is %d\n", plus2(plus3(a[0], a[1], a[2]), plus3(a[3], a[4], a[5]))); 322 | 323 | printf("5\n"); 324 | int c[Z]; 325 | printf("integers? "); 326 | for (int i = 0; i < Z; i++) 327 | { 328 | scanf("%d", &c[i]); 329 | } 330 | printf("the sum is %d\n", plus6(c)); 331 | 332 | printf("6\n"); 333 | int d[Z]; 334 | printf("integers? "); 335 | for (int i = 0; i < Z; i++) 336 | { 337 | scanf("%d", &d[i]); 338 | } 339 | printf("the sum is %d\n", plusn(d, Z)); 340 | 341 | printf("7\n"); 342 | int e[Z]; 343 | printf("integers? "); 344 | for (int i = 0; i < Z; i++) 345 | { 346 | scanf("%d", &e[i]); 347 | } 348 | for (int i = 1; i <= Z; i++) 349 | { 350 | printf("the sum of the first %d elements is %d\n", i, plusn(e, i)); 351 | } 352 | 353 | printf("8\n"); 354 | int f[Z], g[Z]; 355 | printf("f? "); 356 | for (int i = 0; i < Z; i++) 357 | scanf("%d", &f[i]); 358 | printf("g? "); 359 | for (int i = 0; i < Z; i++) 360 | scanf("%d", &g[i]); 361 | printf("the product is %d\n", vectormult(f, g)); 362 | 363 | printf("9\n"); 364 | int h[Z], j[Z]; 365 | printf("h? "); 366 | for (int i = 0; i < Z; i++) 367 | scanf("%d", &h[i]); 368 | printf("j? "); 369 | for (int i = 0; i < Z; i++) 370 | scanf("%d", &j[i]); 371 | printf("the product is %d\n", vectormult2(h, j)); 372 | 373 | printf("10\n"); 374 | float k; 375 | int l; 376 | printf("base? "); 377 | scanf("%f", &k); 378 | printf("exponent? "); 379 | scanf("%d", &l); 380 | printf("the result is %f\n", powr(k, l)); 381 | 382 | printf("11\n"); 383 | float m; 384 | int n; 385 | printf("base? "); 386 | scanf("%f", &m); 387 | printf("exponent? "); 388 | scanf("%d", &n); 389 | printf("the result is %f\n", powr2(m, n)); 390 | 391 | printf("12\n"); 392 | int o; 393 | printf("sum up to? "); 394 | scanf("%d", &o); 395 | printf("the sum is %d\n", sumn(o)); 396 | 397 | printf("13\n"); 398 | char string[100]; 399 | printf("string? "); 400 | scanf("%s", &string[0]); 401 | printf("length is %d\n", mystrlen(string)); 402 | 403 | printf("14\n"); 404 | char string1[100] = "you should not see this printed 1"; 405 | char string2[100] = "you should not see this printed 2"; 406 | printf("string? "); 407 | scanf("%s", &string1[0]); 408 | mystrcpy(string1, string2); 409 | printf("string1=\"%s\"", string1); 410 | printf("string2=\"%s\"", string2); 411 | } 412 | 413 | void print_binary(unsigned int u) 414 | { 415 | // start with the most significant bit (31) 416 | for (int i = 31; i >= 0; i--) 417 | { 418 | if (u & (1 << i)) 419 | printf("1"); 420 | else 421 | printf("0"); 422 | // or: printf("%c", ’0’ + ((u & (1 << i)) != 0)); 423 | } 424 | } 425 | void print_binary(unsigned int u) 426 | { 427 | for (int i = 31; i >= 0; i--) 428 | if (u & (1 << i)) 429 | printf("1"); 430 | else 431 | printf("0"); 432 | } 433 | unsigned int read_binary(void) 434 | { 435 | char s[32]; 436 | unsigned int u; 437 | printf("32-bit binary number? "); 438 | // start with the most significant bit (31) 439 | for (int i = 31; i >= 0; i--) 440 | { 441 | // use " %c" to skip spaces 442 | scanf(" %c", &s[i]); 443 | if (s[i] >= '0' && s[i] <= '1') 444 | { 445 | u = 2 * u + s[i] - '0'; 446 | } 447 | else 448 | { 449 | printf("error: ’%c’ is not a binary digit\n", s[i]); 450 | } 451 | } 452 | return u; 453 | } 454 | void operators() 455 | { 456 | // << is a shift bit left operator 457 | // >> is a shift bit right operator 458 | 459 | // & binqary AND operator 460 | // | binary OR operator 461 | // ^ binary XOR operator 462 | // ~ binary NOT operator 463 | 464 | for (int i = 0; i < 32; i++) 465 | printf("%010u\n", 1 << i); 466 | 467 | int u; 468 | printf("number? "); 469 | scanf("%u", &u); 470 | for (int i = 0; i < 32; i++) 471 | { 472 | if (u & (1 << i)) 473 | printf("bit %d is 1\n", i); 474 | } 475 | 476 | int v; 477 | printf("number? "); 478 | scanf("%u", &v); 479 | print_binary(v); 480 | printf("\n"); 481 | 482 | unsigned int w = read_binary(); 483 | print_binary(w); 484 | printf("\n"); 485 | print_binary(~w); 486 | printf("\n"); 487 | 488 | // Write a program that asks for two 32-bit unsigned binary numbers and prints their bitwise AND, OR, 489 | // and exclusive OR. 490 | unsigned int u1, u2; 491 | u1 = read_binary(); 492 | u2 = read_binary(); 493 | print_binary(u1); 494 | printf(" & "); 495 | print_binary(u2); 496 | printf(" = "); 497 | print_binary(u1 & u2); 498 | printf("\n"); 499 | print_binary(u1); 500 | printf(" | "); 501 | print_binary(u2); 502 | printf(" = "); 503 | print_binary(u1 | u2); 504 | printf("\n"); 505 | print_binary(u1); 506 | printf(" ^ "); 507 | print_binary(u2); 508 | printf(" = "); 509 | print_binary(u1 ^ u2); 510 | printf("\n"); 511 | } 512 | 513 | void advacned_char_input() 514 | { 515 | char c; 516 | printf("characters? "); 517 | while (1) 518 | { 519 | scanf("%c", &c); // or c = getchar(); 520 | printf("’%c’\n", c); 521 | } 522 | 523 | char d; 524 | printf("one line? "); 525 | do 526 | { 527 | scanf("%c", &d); // or c = getchar(); 528 | printf("%c", d); 529 | } while (d != '\n'); 530 | 531 | char e; 532 | printf("one line? "); 533 | printf("\""); 534 | do 535 | { 536 | scanf("%c", &e); 537 | printf("%c", e); 538 | } while (e != '\n'); 539 | printf("\"\n"); 540 | // the above version is not right as it would print a \n trailing 541 | 542 | char e1; 543 | int first = 1; 544 | printf("one line? "); 545 | do 546 | { 547 | scanf("%c", &e1); // or c = getchar(); 548 | if (first) 549 | printf("\""); 550 | first = 0; 551 | if (e1 != '\n') 552 | printf("%c", e1); 553 | } while (e1 != '\n'); 554 | printf("\"\n"); 555 | 556 | char f; 557 | do 558 | { 559 | printf("one line? "); 560 | int first = 1; 561 | do 562 | { 563 | scanf("%c", &f); // or c = getchar(); 564 | if (first) 565 | printf("\""); 566 | first = 0; 567 | if (f != '\n') 568 | printf("%c", f); 569 | } while (c != '\n'); 570 | printf("\"\n"); 571 | } while (1); 572 | 573 | // check if letter is before or after k in alphabet 574 | char g; 575 | printf("one line? "); 576 | do 577 | { 578 | scanf("%c", &g); 579 | if (g >= 'a' && g <= 'z') 580 | { 581 | if (g < 'k') 582 | printf("’%c’ comes %d letters earlier in the alphabet than ’k’\n", g, 'k' - g); 583 | if (g > 'k') 584 | printf("’%c’ comes %d letters later in the alphabet than ’k’\n", g, g - 'k'); 585 | } 586 | 587 | } while (g != '\n'); 588 | 589 | // Write a program (called rot1) that reads a line of text and rotates the letters ’a’ to ’z’ in the alpha- 590 | // bet. Thus ’a’ becomes ’b’, ’b’ becomes ’c’, etc. You have to ensure that ’z’ becomes ’a’. 591 | char h; 592 | printf("one line? "); 593 | do 594 | { 595 | scanf("%c", &h); // or c = getchar(); 596 | if (h >= 'a' && h < 'z') 597 | printf("%c", h + 1); 598 | else if (h == 'z') 599 | printf("a"); 600 | else 601 | printf("%c", h); 602 | } while (h != '\n'); 603 | 604 | char j; 605 | printf("one line? "); 606 | do 607 | { 608 | scanf("%c", &j); // or c = getchar(); 609 | if (j >= 'a' && j <= 'z') 610 | printf("%c", 'a' + ((j - 'a') + 1) % 26); 611 | else 612 | printf("%c", j); 613 | } while (j != '\n'); 614 | } 615 | 616 | int main(void) 617 | { 618 | int n; 619 | printf("Which function would you like to run? "); 620 | scanf("%d", &n); 621 | 622 | if (n == 2) 623 | { 624 | basic_data_types(); 625 | } 626 | else if (n == 3) 627 | { 628 | integer_arrays(); 629 | } 630 | else if (n == 4) 631 | { 632 | character_arrays(); 633 | } 634 | else if (n == 5) 635 | { 636 | functions(); 637 | } 638 | else if (n == 6) 639 | { 640 | operators(); 641 | } 642 | else if (n == 7) 643 | { 644 | advacned_char_input(); 645 | } 646 | else 647 | { 648 | printf("Invalid input. Please try again.\n"); 649 | } 650 | } -------------------------------------------------------------------------------- /week2/other/learning.c: -------------------------------------------------------------------------------- 1 | // THIS FILE IS FOR INDIVIDUAL TESTS. 2 | 3 | #include 4 | #include 5 | // strcpy and strlen would need library string.h: 6 | #include 7 | 8 | // conditions: 9 | // || or, && and, ! not 10 | 11 | #define ROWS 3 12 | #define COLS 4 13 | 14 | void stringReverseByCopy() 15 | { 16 | // initial 8 characters 17 | char str[8] = "0123456"; 18 | // declared reversed empty array 19 | char reverse[8]; 20 | // len = 7 -- counts from 0 21 | int len = strlen(str); 22 | 23 | for (int i = 0; i < len; i++) 24 | { 25 | reverse[(len - 1) - i] = str[i]; 26 | // len - 1 -- to ensure final value is always \0 and make sure starts from right to left 27 | // then goes 7-0, 7-1, 7,2 ... 28 | // Continues until all values from str assigned to reverse 29 | printf("Loop %d Reverse: %c\n", i, reverse[i]); 30 | } 31 | 32 | // assign to final position \0 which is 0 in ASCII 33 | reverse[len] = '\0'; 34 | // copies allfrom reverse to str 35 | strcpy(str, reverse); 36 | printf("%s\n", str); 37 | } 38 | 39 | void stringReverseInPlace() 40 | { 41 | // initial 8 characters 42 | char str[] = "0123456"; 43 | // len = 7 -- counts from 0 44 | int len = strlen(str); 45 | // shorter loop to avoid swapping twice 46 | // directly swaps values from both ends 47 | 48 | // str[0] <-> str[len - 1 - 0] 49 | // str[1] <-> str[len - 1 - 1] 50 | // str[2] <-> str[len - 1 - 2] 51 | // etc ... 52 | 53 | for (int i = 0; i < len / 2; i++) 54 | { 55 | // value from left 56 | char swap = str[i]; 57 | // value from right gets assigned to left 58 | str[i] = str[len - 1 - i]; 59 | // value from left gets assigned to right 60 | str[len - 1 - i] = swap; 61 | } 62 | printf("%s\n", str); 63 | } 64 | 65 | void readingString() 66 | { 67 | // when we scanf: 68 | // entering " hello you!" will set myString to "hello" 69 | 70 | // below is a way possible to read a string with spaces 71 | // enfing on \n 72 | 73 | char myString[100]; 74 | printf("Enter string: "); 75 | int i = 0; 76 | do 77 | { 78 | // keeps assigning value to prevailing index 79 | myString[i] = getchar(); 80 | i++; 81 | } while (myString[i - 1] != '\n'); 82 | // final position is \0 83 | myString[i] = '\0'; 84 | } 85 | 86 | void twoDArray() 87 | { 88 | 89 | // in C a constant is a defined value as above not "const" 90 | int twod[ROWS][COLS]; 91 | 92 | // init array with variables in 2d plane 93 | // requires double loop 94 | for (int i = 0; i < ROWS; i++) 95 | { 96 | for (int j = 0; j < COLS; j++) 97 | { 98 | twod[i][j] = (i + 1) * (j + 1); 99 | } 100 | } 101 | 102 | // print the array in 2d visual plane 103 | for (int row = 0; row < ROWS; row++) 104 | { 105 | for (int column = 0; column < COLS; column++) 106 | printf("%4d", twod[row][column]); 107 | printf("\n"); 108 | } 109 | } 110 | 111 | void heapAndStack() 112 | { 113 | // stack 114 | int a; 115 | 116 | // heap 117 | int *p; 118 | p = (int *)malloc(sizeof(int)); 119 | *p = 10; 120 | free(p); 121 | p = NULL; 122 | frea(p); 123 | p = (int *)malloc(20 * sizeof(int)); 124 | } 125 | 126 | int main(void) 127 | { 128 | int choice; 129 | 130 | printf("Which program would you like to run? "); 131 | scanf("%d", &choice); 132 | if (choice == 1) 133 | { 134 | stringReverseByCopy(); 135 | } 136 | else if (choice == 2) 137 | { 138 | stringReverseInPlace(); 139 | } 140 | else if (choice == 3) 141 | { 142 | readingString(); 143 | } 144 | else if (choice == 4) 145 | { 146 | twoDArray(); 147 | } 148 | else if (choice == 5) 149 | { 150 | heapAndStack(); 151 | } 152 | else 153 | { 154 | printf("Invalid choice"); 155 | } 156 | } -------------------------------------------------------------------------------- /week2/other/powers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float powr(float b, int e) 5 | { 6 | if (e < 1) 7 | { 8 | printf("2. HERE\n"); 9 | return 1.0; 10 | } 11 | printf("1. NO HERE\n"); 12 | // runs the recursive func certain amount of time until it returned 1 13 | // them does recursion under this call 5 times. 14 | float powr_e_minus_one = powr(b, e - 1); 15 | 16 | // the amount of times recursive function is run. 17 | // anything below is executed the amount of times recurssion happened. 18 | // this part is executed 5 times. 19 | { 20 | printf("3. we are here: %f\n", powr_e_minus_one); 21 | return b * powr_e_minus_one; 22 | } 23 | } 24 | 25 | int main(void) 26 | { 27 | float b; 28 | int e; 29 | printf("base? "); 30 | scanf("%f", &b); 31 | printf("exponent? "); 32 | scanf("%d", &e); 33 | // b=3 e=5 34 | // 243 35 | // 3*3*3*3*3 36 | printf("the result is %f\n", powr(b, e)); 37 | } -------------------------------------------------------------------------------- /week3/matlab.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAXSIZE 10 5 | 6 | void print(float matrix[MAXSIZE][MAXSIZE], int rows, int columns, char name) 7 | { 8 | printf("Matrix %c (%d X %d):\n", name, rows, columns); 9 | for (int i = 0; i < rows; i++) 10 | { 11 | for (int j = 0; j < columns; j++) 12 | { 13 | printf("%8.2f", matrix[i][j]); 14 | } 15 | printf("\n"); 16 | } 17 | } 18 | 19 | int add(float matrixA[MAXSIZE][MAXSIZE], int rowsA, int columnsA, 20 | float matrixB[MAXSIZE][MAXSIZE], int rowsB, int columnsB, 21 | float matrixC[MAXSIZE][MAXSIZE], int rowsC, int columnsC) 22 | { 23 | if (rowsA != rowsB || columnsA != columnsB) 24 | { 25 | printf("Dimensions of A & B do not match\n"); 26 | return 1; 27 | } 28 | 29 | for (int i = 0; i < rowsA; i++) 30 | { 31 | for (int j = 0; j < columnsA; j++) 32 | { 33 | matrixC[i][j] = matrixA[i][j] + matrixB[i][j]; 34 | } 35 | } 36 | return 0; 37 | } 38 | 39 | int mult( 40 | float matrixA[MAXSIZE][MAXSIZE], int rowsA, int columnsA, 41 | float matrixB[MAXSIZE][MAXSIZE], int rowsB, int columnsB, 42 | float matrixC[MAXSIZE][MAXSIZE], int rowsC, int columnsC) 43 | { 44 | 45 | if (columnsA != rowsB) 46 | { 47 | printf("Dimensions of A & B do not match\n"); 48 | return 1; 49 | } 50 | 51 | for (int i = 0; i < rowsC; i++) 52 | { 53 | for (int j = 0; j < columnsC; j++) 54 | { 55 | matrixC[i][j] = 0; 56 | } 57 | } 58 | 59 | for (int i = 0; i < rowsA; i++) 60 | { 61 | for (int j = 0; j < columnsB; j++) 62 | { 63 | for (int k = 0; k < columnsA; k++) 64 | { 65 | matrixC[i][j] += matrixA[i][k] * matrixB[k][j]; 66 | } 67 | } 68 | } 69 | return 0; 70 | } 71 | 72 | void printRecursive(float matrixA[MAXSIZE][MAXSIZE], 73 | int rowsA, int columnsA, int currentRow, int currentColumn) 74 | { 75 | if (currentRow == rowsA) 76 | { 77 | printf("enter printRecursive with current row=%d column=%d\n", currentRow, currentColumn); 78 | printf("finished the recursion with current row=%d column=%d\n", currentRow, currentColumn); 79 | return; 80 | } 81 | 82 | printf("enter printRecursive with current row=%d column=%d\n", currentRow, currentColumn); 83 | 84 | int nextRow = currentRow; 85 | int nextColumn = currentColumn + 1; 86 | 87 | if (nextColumn == columnsA) 88 | { 89 | nextRow = currentRow + 1; 90 | nextColumn = 0; 91 | } 92 | 93 | printRecursive(matrixA, rowsA, columnsA, nextRow, nextColumn); 94 | 95 | printf("%8.2f\n", matrixA[currentRow][currentColumn]); 96 | printf("exit printRecursive with current row=%d column=%d\n", currentRow, currentColumn); 97 | } 98 | 99 | void minorMatrix(float matrixA[MAXSIZE][MAXSIZE], int rowsA, int columnsA, 100 | int r, int c, float min[MAXSIZE][MAXSIZE], float matrixC[MAXSIZE][MAXSIZE]) 101 | { 102 | 103 | if (rowsA < 2 || columnsA < 2) 104 | { 105 | printf("Matrix A must have at least two rows & columns\n"); 106 | return; 107 | } 108 | 109 | if (r < 0 || r >= rowsA || c < 0 || c >= columnsA) 110 | { 111 | printf("Rows & columns must be between 0 and %d & %d, respectively\n", rowsA - 1, columnsA - 1); 112 | return; 113 | } 114 | 115 | for (int i = 0; i < rowsA; i++) 116 | { 117 | for (int j = 0; j < columnsA; j++) 118 | { 119 | if (i < r && j < c) 120 | { 121 | min[i][j] = matrixA[i][j]; 122 | } 123 | else if (i < r && j > c) 124 | { 125 | min[i][j - 1] = matrixA[i][j]; 126 | } 127 | else if (i > r && j < c) 128 | { 129 | min[i - 1][j] = matrixA[i][j]; 130 | } 131 | else if (i > r && j > c) 132 | { 133 | min[i - 1][j - 1] = matrixA[i][j]; 134 | } 135 | } 136 | } 137 | 138 | for (int i = 0; i < rowsA - 1; i++) 139 | { 140 | for (int j = 0; j < columnsA - 1; j++) 141 | { 142 | matrixC[i][j] = min[i][j]; 143 | } 144 | } 145 | } 146 | 147 | float determinant(float matrixA[MAXSIZE][MAXSIZE], int rowsA, int columnsA) 148 | { 149 | 150 | float det = 0.0; 151 | float min[MAXSIZE][MAXSIZE] = {0}; 152 | float matrixC[MAXSIZE][MAXSIZE] = {0}; 153 | 154 | if (rowsA == 1 && columnsA == 1) 155 | { 156 | det = matrixA[0][0]; 157 | } 158 | else if (rowsA == 2) 159 | { 160 | det = matrixA[0][0] * matrixA[1][1] - matrixA[0][1] * matrixA[1][0]; 161 | } 162 | else 163 | { 164 | int c = 0; 165 | // recursively call determinant on the columnsA minors of A along row 0 166 | int sign = (c % 2 ? -1 : +1); // the sign for minor(matrixA,0,c) 167 | for (int i = 0; i < columnsA; i++) 168 | { 169 | minorMatrix(matrixA, rowsA, columnsA, 0, i, min, matrixC); 170 | det += matrixA[0][i] * determinant(matrixC, rowsA - 1, columnsA - 1) * sign; 171 | sign = -sign; 172 | } 173 | } 174 | printf("%d", rowsA); 175 | return det; 176 | } 177 | 178 | int main(void) 179 | { 180 | char cmd; 181 | 182 | float matrixA[MAXSIZE][MAXSIZE] = {0}; 183 | float matrixB[MAXSIZE][MAXSIZE] = {0}; 184 | float matrixC[MAXSIZE][MAXSIZE] = {0}; 185 | int rowsA = 1; 186 | int columnsA = 1; 187 | int rowsB = 1; 188 | int columnsB = 1; 189 | int rowsC = 1; 190 | int columnsC = 1; 191 | do 192 | { 193 | printf("Command? "); 194 | scanf(" %c", &cmd); 195 | switch (cmd) 196 | { 197 | case 'q': 198 | { 199 | printf("Bye!\n"); 200 | break; 201 | } 202 | case 'a': 203 | { 204 | print(matrixA, rowsA, columnsA, 'A'); 205 | break; 206 | } 207 | 208 | case 'b': 209 | { 210 | print(matrixB, rowsB, columnsB, 'B'); 211 | break; 212 | } 213 | case 'c': 214 | { 215 | print(matrixC, rowsC, columnsC, 'C'); 216 | break; 217 | } 218 | case 't': 219 | { 220 | 221 | float temp[MAXSIZE][MAXSIZE] = {0}; 222 | 223 | for (int i = 0; i < rowsA; i++) 224 | { 225 | for (int j = 0; j < columnsA; j++) 226 | { 227 | temp[j][i] = matrixA[i][j]; 228 | } 229 | } 230 | 231 | for (int i = 0; i < columnsA; i++) 232 | { 233 | for (int j = 0; j < rowsA; j++) 234 | { 235 | matrixA[i][j] = temp[i][j]; 236 | } 237 | } 238 | 239 | int tempRt = rowsA; 240 | rowsA = columnsA; 241 | columnsA = tempRt; 242 | 243 | break; 244 | } 245 | case 'p': 246 | { 247 | printRecursive(matrixA, rowsA, columnsA, 0, 0); 248 | break; 249 | } 250 | case 'm': 251 | { 252 | float min[MAXSIZE][MAXSIZE] = {0}; 253 | int r = 0; 254 | int c = 0; 255 | 256 | printf("Remove which row & column of matrix A? "); 257 | scanf("%d %d", &r, &c); 258 | 259 | minorMatrix(matrixA, rowsA, columnsA, r, c, min, matrixC); 260 | 261 | rowsC = rowsA - 1; 262 | columnsC = columnsA - 1; 263 | 264 | break; 265 | } 266 | case 'd': 267 | { 268 | if (rowsA != columnsA) 269 | { 270 | printf("Matrix A must be square\n"); 271 | break; 272 | } 273 | float det = determinant(matrixA, rowsA, columnsA); 274 | printf("\nThe determinant is %f\n", det); 275 | } 276 | case 'B': 277 | { 278 | for (int i = 0; i < rowsA; i++) 279 | { 280 | for (int j = 0; j < columnsA; j++) 281 | { 282 | matrixB[i][j] = matrixA[i][j]; 283 | } 284 | } 285 | rowsB = rowsA; 286 | columnsB = columnsA; 287 | break; 288 | } 289 | case 'I': 290 | { 291 | 292 | int tempR = 1; 293 | int tempC = 1; 294 | 295 | printf("Size of matrix A (rows columns)? "); 296 | scanf("%d %d", &tempR, &tempC); 297 | 298 | if (tempR != tempC || tempR > MAXSIZE || tempC > MAXSIZE || tempR < 1 || tempC < 1 || tempR > 10 || tempC > 10) 299 | { 300 | printf("Rows & columns must be equal and between 1 and 10\n"); 301 | break; 302 | } 303 | 304 | for (int i = 0; i < tempR; i++) 305 | { 306 | for (int j = 0; j < tempC; j++) 307 | { 308 | if (i == j) 309 | { 310 | matrixA[i][j] = 1; 311 | } 312 | else 313 | { 314 | matrixA[i][j] = 0; 315 | } 316 | } 317 | } 318 | 319 | rowsA = tempR; 320 | columnsA = tempC; 321 | break; 322 | } 323 | case 'A': 324 | { 325 | 326 | int tempRA = 1; 327 | int tempCA = 1; 328 | 329 | printf("Size of matrix A (rows columns)? "); 330 | scanf("%d %d", &tempRA, &tempCA); 331 | 332 | if (tempRA > MAXSIZE || tempCA > MAXSIZE || tempRA < 1 || tempCA < 1 || tempRA > 10 || tempCA > 10) 333 | { 334 | printf("Rows & columns must be between 1 and 10\n"); 335 | break; 336 | } 337 | 338 | for (int i = 0; i < tempRA; i++) 339 | { 340 | printf("Row %d? ", i); 341 | for (int j = 0; j < tempCA; j++) 342 | { 343 | scanf("%f", &matrixA[i][j]); 344 | } 345 | } 346 | 347 | rowsA = tempRA; 348 | columnsA = tempCA; 349 | break; 350 | } 351 | case '+': 352 | { 353 | int addition = add(matrixA, rowsA, columnsA, matrixB, rowsB, columnsB, matrixC, rowsC, columnsC); 354 | if (addition == 0) 355 | { 356 | rowsC = rowsA; 357 | columnsC = columnsA; 358 | } 359 | break; 360 | } 361 | case '*': 362 | { 363 | int multiplication = mult(matrixA, rowsA, columnsA, matrixB, rowsB, columnsB, matrixC, rowsC, columnsC); 364 | if (multiplication == 0) 365 | { 366 | rowsC = rowsA; 367 | columnsC = columnsB; 368 | } 369 | break; 370 | } 371 | default: 372 | { 373 | printf("Unknown command '%c'\n", cmd); 374 | break; 375 | } 376 | } 377 | } while (cmd != 'q'); 378 | } -------------------------------------------------------------------------------- /week3/other/bubble-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // swaps numbers next to each other 4 | // loops over array by its length 5 | 6 | void bubble_sort(int a[], int length) 7 | { 8 | for (int i = 0; i < length; i++) 9 | { 10 | // here length - 1 - i is same as length - 1 just we make less checks 11 | for (int j = 0; j < length - 1 - i; j++) 12 | { 13 | if (a[j] > a[j + 1]) 14 | { 15 | int temp = a[j]; 16 | a[j] = a[j + 1]; 17 | a[j + 1] = temp; 18 | } 19 | } 20 | } 21 | } 22 | 23 | // if array sorted stop loop 24 | void optimized_bubblesort(float values[], int size) 25 | { 26 | int i, swap; 27 | float tmp; 28 | do 29 | { 30 | swap = 0; 31 | for (i = 0; i < size - 1; i++) 32 | { 33 | if (values[i] > values[i + 1]) 34 | { 35 | tmp = values[i]; 36 | values[i] = values[i + 1]; 37 | values[i + 1] = tmp; 38 | swap = 1; 39 | } 40 | } 41 | } while (swap == 1); 42 | } 43 | 44 | int main(void) 45 | { 46 | int a[] = {3, 9, 27, 38, 43, 10, 82}; 47 | int length = sizeof(a) / sizeof(a[0]); 48 | bubble_sort(a, length); 49 | for (int i = 0; i < length; i++) 50 | { 51 | printf("%i ", a[i]); 52 | } 53 | printf("\n"); 54 | 55 | float b[] = {3.0, 9.0, 27.0, 38.0, 43.0, 10.0, 82.0}; 56 | int length2 = sizeof(b) / sizeof(b[0]); 57 | optimized_bubblesort(b, length2); 58 | for (int i = 0; i < length2; i++) 59 | { 60 | printf("%f ", b[i]); 61 | } 62 | printf("\n"); 63 | } -------------------------------------------------------------------------------- /week3/other/merge-sort.c: -------------------------------------------------------------------------------- 1 | // remember divide and conquoer 2 | // Example visualization of the merge sort algorithm: 3 | // 4 | // [38, 27, 43, 3, 9, 82, 10] 5 | // / \ 6 | // [38, 27, 43, 3] [9, 82, 10] 7 | // / | | \ 8 | // [38, 27] [43, 3] [9, 82] [10] 9 | // / | / | / \ | 10 | // [38] [27] [43] [3] [9] [82] [10] 11 | // \ / \ / \ / | 12 | // [27, 38] [3, 43] [9, 82] [10] 13 | // \ / \ / 14 | // [3, 27, 38, 43] [9, 10, 82] 15 | // \ / 16 | // [3, 9, 10, 27, 38, 43, 82] 17 | // 18 | // The array is first broken up into progressively smaller unsorted portions of 19 | // the array, and once we have "sub-arrays" of 1 element they are by definition 20 | // sorted arrays. From here the "sorted arrays" are merged together until we 21 | // arrive at the complete sorted array. 22 | 23 | #include 24 | 25 | void merge_sorted_arrays(int a[], int l, int m, int r) 26 | { 27 | int left_length = m - l + 1; 28 | int right_length = r - m; 29 | 30 | // create temp arrays 31 | int temp_left[left_length]; 32 | int temp_right[right_length]; 33 | 34 | // used as index/counter variables for the 3 arrays a, temp_left, temp_right 35 | int i, j, k; 36 | 37 | // copy to right and left temp arrays 38 | for (int i = 0; i < left_length; i++) 39 | { 40 | temp_left[i] = a[l + i]; 41 | } 42 | 43 | for (int j = 0; j < right_length; j++) 44 | { 45 | temp_right[j] = a[m + 1 + j]; 46 | } 47 | 48 | // Use i to move through the indexes of temp_left, j to move through the 49 | // indexes of temp_right, and k to move through the portion of the array 50 | // a from l ... r. We basically keep checking the "head" of temp_left 51 | // and temp_right (knowing both arrays are sorted) and put the smaller of 52 | // the two into array a (using i, j, k to move through the arrays). When 53 | // we run out elements in either temp_left or temp_right, the remaining 54 | // elements from the other array will be copied over into a. 55 | 56 | // whats going on before the loop 57 | // temp_left = [3, 27, 38, 43] 58 | // temp_right = [9, 10, 82] 59 | 60 | for (i = 0, j = 0, k = l; k <= r; k++) 61 | { 62 | // compares i and j if i<=j then add it to a[k] and do i++ 63 | // else add j to a[k] and do j++ 64 | // then do k++ 65 | 66 | if ((i < left_length) && 67 | (j >= right_length || temp_left[i] <= temp_right[j])) 68 | { 69 | a[k] = temp_left[i]; 70 | i++; 71 | } 72 | 73 | else 74 | { 75 | a[k] = temp_right[j]; 76 | j++; 77 | } 78 | } 79 | } 80 | 81 | void merge_sort_recursion(int a[], int l, int r) 82 | { 83 | /// we stop recursion when l >= r 84 | if (l < r) 85 | { 86 | // find the midpoint of l and r 87 | int m = l + (r - l) / 2; 88 | 89 | // apply the function recursively to the left and right portions split 90 | // at the midpoint 91 | merge_sort_recursion(a, l, m); 92 | merge_sort_recursion(a, m + 1, r); 93 | 94 | // at this point both portions of the array have been sorted, and we now 95 | // merge the sorted portions of the array 96 | merge_sorted_arrays(a, l, m, r); 97 | } 98 | } 99 | 100 | void merge_sort(int array[], int length) 101 | { 102 | // 0 is left most index (start of array) and length - 1 is right most index (excluding null) 103 | merge_sort_recursion(array, 0, length - 1); 104 | } 105 | 106 | int main(void) 107 | { 108 | int array[] = {38, 27, 43, 3, 9, 82, 10}; 109 | int length = sizeof(array) / sizeof(array[0]); 110 | 111 | // sort array using merge sort 112 | merge_sort(array, length); 113 | 114 | for (int i = 0; i < length; i++) 115 | { 116 | printf("%d ", array[i]); 117 | } 118 | printf("\n"); 119 | } -------------------------------------------------------------------------------- /week4/other/ca.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int b; 6 | int *c; 7 | int a = b + c[4]; 8 | } -------------------------------------------------------------------------------- /week4/other/extra-excercises.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Pointers and Addresses 7 | void func1() 8 | { 9 | int i = 1; 10 | float f = 2; 11 | double d = 3; 12 | printf("i=%d at address %p\n", i, &i); 13 | printf("f=%f at address %p\n", f, &f); 14 | printf("d=%e at address %p\n", d, &d); 15 | } 16 | 17 | void func2() 18 | { 19 | int i[3] = {1, 2, 3}; 20 | char c[5] = "hell"; 21 | for (int j = 0; j < 3; j++) 22 | printf("i[%d]=%d at address %p\n", j, i[j], &i[j]); 23 | for (int j = 0; j < 5; j++) 24 | printf("c[%d]=’%c’ at address %p\n", j, c[j], &c[j]); 25 | } 26 | 27 | void func3() 28 | { 29 | int i = 10; 30 | int *p1 = &i; 31 | int *p2 = &i; 32 | printf("i=%d\n", i); 33 | printf("&i=%p\n", &i); 34 | printf("p1=%p\n", p1); 35 | printf("p2=%p\n", p2); 36 | printf("&p1=%p\n", &p1); 37 | printf("&p2=%p\n", &p2); 38 | } 39 | 40 | void func4() 41 | { 42 | int i = 10; 43 | int *p1 = &i; 44 | int *p2 = &i; 45 | printf("i=%d\n", i); 46 | printf("&i=%p\n", &i); 47 | printf("p1=%p\n", p1); 48 | printf("p2=%p\n", p2); 49 | printf("&p1=%p\n", &p1); 50 | printf("&p2=%p\n", &p2); 51 | *p1 = 20; 52 | // or: *p2 = 20; 53 | printf("i=%d\n", i); 54 | } 55 | 56 | void func5() 57 | { 58 | int a[7] = {7, 6, 5, 4, 3, 2, 1}; 59 | int *p = &a[0]; 60 | for (int i = 0; i < 7; i++) 61 | printf("a[%d]=%d\n", i, a[i]); 62 | for (int i = 0; i < 7; i++) 63 | printf("p=%p+%d=%p a[%d]=%d\n", p, i, p + i, i, *(p + i)); 64 | } 65 | 66 | // Pointers and Function Arguments 67 | 68 | void swap(int *a, int *b) 69 | { 70 | int temp = *a; 71 | *a = *b; 72 | *b = temp; 73 | } 74 | void func6() 75 | { 76 | double a = 10, b = 20; 77 | printf("a=%f b=%f\n", a, b); 78 | swap(&a, &b); 79 | printf("a=%f b=%f\n", a, b); 80 | } 81 | 82 | int *next_element(int a[10], int i) 83 | { 84 | // (i+1) % 10 gives the next element, wrapping around the end 85 | // a[(i+1)%10] is that element in the array 86 | // &a[(i+1)%10] is the address of that element 87 | return &a[(i + 1) % 10]; 88 | // alternatively, just work with the address: 89 | return a + (i + 1) % 10; 90 | } 91 | void func7() 92 | { 93 | int a[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 94 | for (int i = 0; i < 10; i++) 95 | { 96 | int *next = next_element(a, i); 97 | printf("a[%d]=%d is stored at %p; the next element a[%d]=%d is stored at %p\n", 98 | i, a[i], &a[i], i, *next, next); 99 | } 100 | } 101 | 102 | int len(char *s) 103 | { 104 | int l = 0; 105 | while (*s != '\0') 106 | { 107 | l++; 108 | s++; 109 | } 110 | // or: while (*s) { l++; s++; } 111 | // or: while (*s++) l++; 112 | return l; 113 | } 114 | void func8() 115 | { 116 | char s[100] = {'\0'}; 117 | printf("String? "); 118 | scanf("%s", s); 119 | // same as scanf("%s", &s[0]); 120 | printf("len(\"%s\")=%d\n", s, len(s)); 121 | } 122 | 123 | void rev(char *s) 124 | { 125 | int l = len(s); 126 | if (l == 0) 127 | return; 128 | char copy[l]; 129 | for (int i = 0; i < l; i++) 130 | *(copy + l - 1 - i) = *(s + i); 131 | // now copy the reversed string back into s 132 | for (int i = 0; i < l; i++) 133 | *(s + i) = *(copy + i); 134 | } 135 | void func9() 136 | { 137 | char s[100] = {'\0'}; 138 | printf("String? "); 139 | scanf("%s", s); 140 | printf("rev(\"%s\")=", s); 141 | rev(s); 142 | printf("%s\n", s); 143 | } 144 | 145 | void rev2(char *s) 146 | { 147 | int l = len(s); 148 | if (l == 0) 149 | return; 150 | char copy[l]; 151 | char *sp = s; 152 | char *cp = copy + l - 1; 153 | for (int i = 0; i < l; i++) 154 | { 155 | *cp-- = *sp++; 156 | } 157 | // now copy the reversed string back into s 158 | sp = s; 159 | cp = copy; 160 | for (int i = 0; i < l; i++) 161 | { 162 | *sp = *cp; 163 | sp++; 164 | cp++; 165 | // or: *sp++ = *cp++; 166 | } 167 | } 168 | void func10() 169 | { 170 | char s[100] = {'\0'}; 171 | printf("String? "); 172 | scanf("%s", s); 173 | printf("rev(\"%s\")=", s); 174 | rev(s); 175 | printf("%s\n", s); 176 | } 177 | 178 | // SWAPPING USING POINTER VARIABLES - NO RECURSION 179 | // void rev(char *s) 180 | // { 181 | // int l = len(s); 182 | // for (int i = 0; i < l / 2; i++) 183 | // { 184 | // char swap = *(s + i); 185 | // *(s + i) = *(s + l - 1 - i); 186 | // *(s + l - 1 - i) = swap; 187 | // } 188 | // } 189 | // 190 | // SWAPPING USING POINTER VARIABLES - YES RECURSION 191 | // void rev(char *s, int l) 192 | // { 193 | // if (l < 2) 194 | // return; 195 | // char swap = *s; 196 | // *s = *(s + l - 1); 197 | // *(s + l - 1) = swap; 198 | // rev(s + 1, l - 2); 199 | // } 200 | int main(void) 201 | { 202 | } -------------------------------------------------------------------------------- /week4/other/extra-excercises3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Arrays of Pointers 7 | void func1() 8 | { 9 | int i = 11, j = 22, k = 33; 10 | int a[3]; 11 | int *pa[3]; 12 | a[0] = i; 13 | a[1] = j; 14 | a[2] = k; 15 | pa[0] = &i; 16 | pa[1] = &j; 17 | pa[2] = &k; 18 | for (int x = 0; x < 3; x++) 19 | printf("a[%d]=%d\n", x, a[x]); 20 | for (int x = 0; x < 3; x++) 21 | printf("pa[%d]=%p\n", x, pa[x]); 22 | for (int x = 0; x < 3; x++) 23 | printf("*pa[%d]=%d\n", x, *pa[x]); 24 | } 25 | 26 | void func2() 27 | { 28 | double a[4]; 29 | double *pa[4]; 30 | for (int x = 0; x < 4; x++) 31 | a[x] = 100 * x; 32 | for (int x = 0; x < 4; x++) 33 | pa[x] = &a[x]; 34 | for (int x = 0; x < 4; x++) 35 | printf("a[%d]=%f\n", x, a[x]); 36 | for (int x = 0; x < 4; x++) 37 | printf("pa[%d]=%p\n", x, pa[x]); 38 | for (int x = 0; x < 4; x++) 39 | printf("*pa[%d]=%f\n", x, *pa[x]); 40 | } 41 | 42 | void func3() 43 | { 44 | char a[] = "another one bites the dust"; 45 | char *pa[5] = {&a[0], &a[8], &a[12], &a[18], &a[22]}; 46 | // or: char *pa[5] = { a+0, a+8, a+12, a+18, a+22 }; 47 | printf("a=\"%s\"\n", a); 48 | for (int x = 0; x < 5; x++) 49 | printf("pa[%d]=%p\n", x, pa[x]); 50 | for (int x = 0; x < 5; x++) 51 | printf("pa[%d]=\"%s\"\n", x, pa[x]); 52 | for (int x = 0; x < 5; x++) 53 | printf("*pa[%d]=’%c’\n", x, *pa[x]); 54 | } 55 | 56 | int main(void) 57 | { 58 | } -------------------------------------------------------------------------------- /week4/other/extra-exercises2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Pointers into Arrays 7 | char *find_pos(char *s, char c) 8 | { 9 | while (*s != '\0' && *s != c) 10 | s++; 11 | return s; 12 | } 13 | 14 | int main(void) 15 | { 16 | char letters[53] = {'\0'}; 17 | char letter; 18 | for (int i = 0; i < 26; i++) 19 | { 20 | letters[i] = 'a' + i; 21 | letters[i + 26] = 'A' + i; 22 | } 23 | // note that we moved these pi initialisations 24 | // because letters needs to be initialised first! 25 | char *p1 = find_pos(letters, 'a'); 26 | char *p2 = find_pos(letters, 'n'); 27 | char *p3 = find_pos(letters, 'A'); 28 | char *p4 = find_pos(letters, 'N'); 29 | printf("letter? "); 30 | scanf(" %c", &letter); 31 | printf("the start address of the array is %p\n", letters); 32 | printf("the array index of '%c' in the array is %ld\n", letter, find_pos(letters, letter) - letters); 33 | printf("the address of '%c' in the array is %p\n", letter, find_pos(letters, letter)); 34 | printf("p1=%p strlen(%s)=%ld\n", p1, p1, strlen(p1)); 35 | printf("p2=%p strlen(%s)=%ld\n", p2, p2, strlen(p2)); 36 | printf("p3=%p strlen(%s)=%ld\n", p3, p3, strlen(p3)); 37 | printf("p4=%p strlen(%s)=%ld\n", p4, p4, strlen(p4)); 38 | } -------------------------------------------------------------------------------- /week4/other/index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | char s[100] = {NULL}; 6 | char c; 7 | printf("String? "); 8 | scanf("%s", s); 9 | printf("Character? "); 10 | scanf(" %c", &c); 11 | int i = 0; 12 | while (s[i] != '\0') 13 | { 14 | if (s[i] == c) 15 | { 16 | printf("%d\n", i+1); 17 | return 0; 18 | } 19 | i++; 20 | } 21 | } -------------------------------------------------------------------------------- /week4/other/pointer-adress.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int a[7] = {7, 6, 5, 4, 3, 2, 1}; 7 | int *p = &a[0]; 8 | for (int i = 0; i < 7; i++) 9 | printf("a[%d]=%d\n", i, a[i]); 10 | for (int i = 0; i < 7; i++) 11 | printf("p=%p+%d=%p a[%d]=%d\n", p, i, p + i, i, *(p + i)); 12 | 13 | // inside pointer 14 | printf("%d\n", *p); 15 | // pointer address 16 | printf("%p\n", &p); 17 | } -------------------------------------------------------------------------------- /week4/other/pointers.c: -------------------------------------------------------------------------------- 1 | // general practice 2 | #include 3 | 4 | // if we didnt use pointers here and just passed vars like a,b 5 | // we would be passing copies of the values of a and b 6 | // so the swap function would be swapping the copies 7 | // and the original values of a and b would not be changed 8 | void swap(int *a, int *b) 9 | { 10 | int temp = *a; 11 | *a = *b; 12 | *b = temp; 13 | } 14 | 15 | // 1D ARRAY 16 | void print_array(int *array, int length) 17 | { 18 | for (int i = 0; i < length; i++) 19 | { 20 | printf("%i ", array[i]); 21 | } 22 | printf("\n"); 23 | } 24 | 25 | void rev(char s[], int l) 26 | { 27 | if (l < 2) 28 | return; 29 | char tmp = s[0]; 30 | s[0] = s[l - 1]; 31 | s[l - 1] = tmp; 32 | rev(s + 1, l - 2); 33 | } 34 | 35 | void pointer_array() 36 | { 37 | char str1[] = "hello"; 38 | char str2[] = ""; 39 | char str3[] = "a bit longer"; 40 | 41 | char *array_of_pointers[] = {str1, str2, str3, "last string"}; 42 | 43 | for (int i = 0; i < 4; i++) 44 | { 45 | printf("array[%d]=\"%s\"\n", i, array_of_pointers[i]); 46 | } 47 | } 48 | 49 | #define ENTRIES 4 50 | #define LENGTH 5 51 | int main(void) 52 | { 53 | int a = 5; 54 | int *p = &a; 55 | printf("%i\n", *p); 56 | 57 | int x = 10; 58 | int y = 20; 59 | // PASSING POINTERS WITH & 60 | swap(&x, &y); 61 | printf("x is %i and y is %i\n", x, y); 62 | 63 | // 1D ARRAY 64 | int array[5] = {1, 2, 3, 4, 5}; 65 | print_array(array, 5); 66 | 67 | // array of pointers 68 | pointer_array(); 69 | 70 | // 2D ARRAY 71 | char array_2_d[ENTRIES][LENGTH] = {"1234", "AB", "xyz"}; 72 | for (int i = 0; i < ENTRIES; i++) 73 | printf("array[%d] = \"%s\"\n", i, array_2_d[i]); 74 | } -------------------------------------------------------------------------------- /week4/other/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int len(char *s){ 5 | int l = 0; 6 | while (*s != '\0') 7 | { 8 | l++; 9 | s++; 10 | } 11 | return l; 12 | } 13 | 14 | void rev(char *s, int l) 15 | { 16 | printf("THE LENGTH NOW IS: %d\n", l); 17 | if (l<2) 18 | { 19 | return; 20 | } 21 | // swap init to current s 22 | char swap = *s; 23 | // first char becomes last 24 | *s = *(s+l-1); 25 | // last char becomes first 26 | *(s+l-1) = swap; 27 | 28 | rev(s+1, l-2); 29 | } 30 | 31 | int main(void) 32 | { 33 | char s[100] = {NULL}; 34 | // we can also say '\0' instead of NULL 35 | printf("String? "); 36 | scanf("%s",s); 37 | 38 | printf("rev(\"%s\")=\n", s); 39 | int length = len(s); 40 | // printf(" %d", length); 41 | 42 | rev(s, length); 43 | printf("%s\n", s); 44 | } -------------------------------------------------------------------------------- /week4/sorting-and-pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LENGTH 100 5 | 6 | void printString(char string[]) 7 | { 8 | for (int i = 0; i < LENGTH; i++) 9 | { 10 | if (string[i] == '\0') 11 | { 12 | printf("\\0"); 13 | } 14 | else 15 | { 16 | printf("%c", string[i]); 17 | } 18 | } 19 | printf("\n"); 20 | } 21 | 22 | void insertName(char *startPos[], int *nrNames, char newName[]) 23 | { 24 | 25 | int i = 0; 26 | while (newName[i] != '\0') 27 | { 28 | startPos[*nrNames][i] = newName[i]; 29 | i++; 30 | } 31 | startPos[*nrNames][i] = '\0'; 32 | startPos[*nrNames + 1] = startPos[*nrNames] + i + 1; 33 | 34 | *nrNames = *nrNames + 1; 35 | } 36 | 37 | void printNames(char names[], char *startPos[], int nrNames) 38 | { 39 | for (int i = 0; i < nrNames; i++) 40 | { 41 | printf("startPos[%d]=%02d length=%02d string=\"%s\"\n", i, (int)(startPos[i] - names), (int)strlen(startPos[i]) + 1, startPos[i]); 42 | } 43 | } 44 | 45 | int totalLength(char *startPos[], int nrNames) 46 | { 47 | int totalLength = 0; 48 | for (int i = 0; i < nrNames; i++) 49 | { 50 | totalLength += strlen(startPos[i]) + 1; 51 | } 52 | return totalLength; 53 | } 54 | 55 | int lookupNamePos(char names[], char *startPos[], int nrNames, char name[]) 56 | { 57 | int index = -1; 58 | for (int i = 0; i < nrNames; i++) 59 | { 60 | if (strcmp(startPos[i], name) == 0) 61 | { 62 | // if the index of the name in names[] is higher than the current index 63 | // then update the index 64 | if (startPos[i] - names > index) 65 | { 66 | index = startPos[i] - names; 67 | } 68 | } 69 | } 70 | return index; 71 | } 72 | 73 | void removeName(char *startPos[], int *nrNames, char remName[]) 74 | { 75 | // Example 76 | // startPos = ["one", "two", "three", "four"] 77 | // removeName("two") 78 | // startPos = ["one", "four", "three"] 79 | 80 | int flag = 0; 81 | int index = 0; 82 | for (int i = 0; i < *nrNames; i++) 83 | { 84 | if (strcmp(startPos[i], remName) == 0) 85 | { 86 | flag = 1; 87 | break; 88 | } 89 | index++; 90 | } 91 | if (flag == 1) 92 | { 93 | startPos[index] = startPos[*nrNames - 1]; 94 | startPos[*nrNames - 1] = startPos[*nrNames]; 95 | startPos[*nrNames] = NULL; 96 | *nrNames = *nrNames - 1; 97 | } 98 | } 99 | 100 | void swapNames(char *startPos[], int i, int j) 101 | { 102 | char *temp = startPos[i]; 103 | startPos[i] = startPos[j]; 104 | startPos[j] = temp; 105 | } 106 | 107 | void insertionSortNames(char *startPos[], int nrNames) 108 | { 109 | for (int i = 1; i < nrNames; i++) 110 | { 111 | for (int j = i; j > 0; j--) 112 | { 113 | if (strcmp(startPos[j], startPos[j - 1]) < 0) 114 | { 115 | swapNames(startPos, j, j - 1); 116 | } 117 | } 118 | } 119 | } 120 | 121 | void recoverNames(char names[], char *startPos[], int *nrNames) 122 | { 123 | for (int i = 0; i < LENGTH; i++) 124 | { 125 | // if the name is not \0 126 | if (names[i] != '\0') 127 | { 128 | // if the name is not in startPos[] 129 | if (lookupNamePos(names, startPos, *nrNames, &names[i]) == -1) 130 | { 131 | // print that the name is garbage 132 | printf("Name \"%s\" starts at %d and is garbage\n", &names[i], i); 133 | 134 | // recover the name by adding it to startPos[] from names[] with its original start position 135 | startPos[*nrNames] = &names[i]; 136 | *nrNames = *nrNames + 1; 137 | } 138 | else 139 | { 140 | // print that the name is not garbage 141 | printf("Name \"%s\" starts at %d and is not garbage\n", &names[i], i); 142 | } 143 | 144 | // move i to the next name 145 | i += strlen(&names[i]); 146 | } 147 | } 148 | } 149 | 150 | int main(void) 151 | { 152 | char cmd; 153 | 154 | char names[LENGTH] = {0}; 155 | char name[LENGTH] = {0}; 156 | int nrNames = 0; 157 | char *startPos[LENGTH] = {0}; 158 | startPos[0] = &names[0]; 159 | 160 | // task 1 161 | // print next line 162 | // strcpy(names, "Asterix"); 163 | // printString(names); 164 | 165 | do 166 | { 167 | printf("Command? "); 168 | scanf(" %c", &cmd); 169 | switch (cmd) 170 | { 171 | case 'q': 172 | { 173 | printf("Bye!\n"); 174 | break; 175 | } 176 | case 'p': 177 | { 178 | printString(names); 179 | break; 180 | } 181 | case 'i': 182 | { 183 | printf("Name? "); 184 | scanf("%s", name); 185 | insertName(startPos, &nrNames, name); 186 | break; 187 | } 188 | case 'n': 189 | { 190 | printNames(names, startPos, nrNames); 191 | break; 192 | } 193 | case 'e': 194 | { 195 | printf("Total length is %d\n", totalLength(startPos, nrNames)); 196 | break; 197 | } 198 | case 'l': 199 | { 200 | printf("Name? "); 201 | scanf("%s", name); 202 | int pos = lookupNamePos(names, startPos, nrNames, name); 203 | printf("\"%s\" has index %d\n", name, pos); 204 | break; 205 | } 206 | case 'r': 207 | { 208 | printf("Name? "); 209 | scanf("%s", name); 210 | removeName(startPos, &nrNames, name); 211 | break; 212 | } 213 | case 's': 214 | { 215 | int i, j; 216 | printf("Indices? "); 217 | scanf("%d %d", &i, &j); 218 | swapNames(startPos, i, j); 219 | break; 220 | } 221 | case 'o': 222 | { 223 | insertionSortNames(startPos, nrNames); 224 | break; 225 | } 226 | case 'v': 227 | { 228 | recoverNames(names, startPos, &nrNames); 229 | break; 230 | } 231 | default: 232 | { 233 | printf("Unknown command '%c'\n", cmd); 234 | break; 235 | } 236 | } 237 | } while (cmd != 'q'); 238 | } -------------------------------------------------------------------------------- /week5/malloc-sorting.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define LENGTH 30 5 | 6 | // use this on exam to keep track of your malloc 7 | // #include 8 | 9 | struct person 10 | { 11 | char *name; 12 | float height; 13 | }; 14 | 15 | void removeAllPersons(struct person *persons, int nrPersons) 16 | { 17 | for (int i = 0; i < nrPersons; i++) 18 | { 19 | free(persons[i].name); 20 | } 21 | free(persons); 22 | } 23 | 24 | void printPersons(struct person persons[], int nrPersons, int from, int to) 25 | { 26 | int print = 0; 27 | printf("["); 28 | for (int i = 0; i < nrPersons; i++) 29 | { 30 | if (!(persons[i].name == NULL || persons[i].height == 0.0)) 31 | { 32 | if (print > 0) 33 | { 34 | printf(",(\"%s\",%.3f)", persons[i].name, persons[i].height); 35 | } 36 | else 37 | { 38 | printf("(\"%s\",%.3f)", persons[i].name, persons[i].height); 39 | print++; 40 | } 41 | } 42 | } 43 | printf("]\n"); 44 | } 45 | 46 | void insertPerson(struct person persons[], int nrPersons, int newEntry, char *newName, float newHeight) 47 | { 48 | 49 | if (persons[newEntry].height == 0.0) 50 | { 51 | persons[newEntry].name = (char *)malloc((strlen(newName) + 1) * sizeof(char)); 52 | strcpy(persons[newEntry].name, newName); 53 | persons[newEntry].height = newHeight; 54 | } 55 | else 56 | { 57 | printf("Entry %i is already occupied by (\"%s\",%.3f)\n", newEntry, persons[newEntry].name, persons[newEntry].height); 58 | } 59 | } 60 | 61 | void maxHeight(struct person persons[], int nrPersons, float *max) 62 | { 63 | for (int i = 0; i < nrPersons; i++) 64 | { 65 | if (persons[i].height > *max) 66 | { 67 | *max = persons[i].height; 68 | } 69 | } 70 | } 71 | 72 | float minHeight(struct person persons[], int nrPersons) 73 | { 74 | float minT = 0; 75 | for (int i = 0; i < nrPersons; i++) 76 | { 77 | if (minT == 0) 78 | { 79 | minT = persons[i].height; 80 | } 81 | if (persons[i].height < minT && persons[i].height != 0) 82 | { 83 | minT = persons[i].height; 84 | } 85 | } 86 | return minT; 87 | } 88 | 89 | void replacePerson(struct person persons[], int nrPersons, 90 | int newEntry, char *newName, float newHeight) 91 | { 92 | if (persons[newEntry].height == 0 && persons[newEntry].name == NULL) 93 | { 94 | insertPerson(persons, nrPersons, newEntry, newName, newHeight); 95 | } 96 | else 97 | { 98 | free(persons[newEntry].name); 99 | persons[newEntry].name = (char *)malloc((strlen(newName) + 1) * sizeof(char)); 100 | strcpy(persons[newEntry].name, newName); 101 | persons[newEntry].height = newHeight; 102 | } 103 | } 104 | 105 | void swapPersons(struct person persons[], struct person *a, struct person *b) 106 | { 107 | struct person temp = *a; 108 | *a = *b; 109 | *b = temp; 110 | } 111 | 112 | void bubbleSort(struct person persons[], int nrPersons, int *swapped) 113 | { 114 | int swap = 1; 115 | int j = nrPersons; 116 | int count = 0; 117 | 118 | while (swap) 119 | { 120 | swap = 0; 121 | for (int i = 1; i < j; i++) 122 | { 123 | if (persons[i].height < persons[i - 1].height || (persons[i].height == persons[i - 1].height && strcmp(persons[i].name, persons[i - 1].name) < 0)) 124 | { 125 | swapPersons(persons, &persons[i], &persons[i - 1]); 126 | swap = 1; 127 | count++; 128 | printf("after swapping: "); 129 | printPersons(persons, nrPersons, 0, 0); 130 | } 131 | } 132 | j--; 133 | } 134 | printf("Swapped %d times\n", count); 135 | } 136 | 137 | void merge(struct person persons[], int l, int m, int r) 138 | { 139 | int i, j, k; 140 | int n1 = m - l + 1; 141 | int n2 = r - m; 142 | 143 | struct person L[n1], R[n2]; 144 | 145 | for (i = 0; i < n1; i++) 146 | { 147 | L[i] = persons[l + i]; 148 | } 149 | for (j = 0; j < n2; j++) 150 | { 151 | R[j] = persons[m + 1 + j]; 152 | } 153 | 154 | i = 0; 155 | j = 0; 156 | k = l; 157 | 158 | while (i < n1 && j < n2) 159 | { 160 | if (L[i].height <= R[j].height) 161 | { 162 | persons[k] = L[i]; 163 | i++; 164 | } 165 | else 166 | { 167 | persons[k] = R[j]; 168 | j++; 169 | } 170 | k++; 171 | } 172 | 173 | while (i < n1) 174 | { 175 | persons[k] = L[i]; 176 | i++; 177 | k++; 178 | } 179 | 180 | while (j < n2) 181 | { 182 | persons[k] = R[j]; 183 | j++; 184 | k++; 185 | } 186 | } 187 | 188 | void mergeSort(struct person persons[], int left, int right) 189 | { 190 | if (left < right) 191 | { 192 | int m = left + (right - left) / 2; 193 | 194 | mergeSort(persons, left, m); 195 | mergeSort(persons, m + 1, right); 196 | 197 | merge(persons, left, m, right); 198 | } 199 | } 200 | 201 | int main(void) 202 | { 203 | struct person *persons = NULL; 204 | int nrPersons = 0; 205 | char cmd = 0; 206 | int nrPersonsT = 0; 207 | float newHeight = 0; 208 | float newHeightT = 0; 209 | char newName[LENGTH] = {0}; 210 | int newEntry = 0; 211 | float max = 0; 212 | int swapped = 0; 213 | 214 | do 215 | { 216 | 217 | printf("Command? "); 218 | scanf(" %c", &cmd); 219 | switch (cmd) 220 | { 221 | case 'q': 222 | { 223 | printf("Bye!\n"); 224 | removeAllPersons(persons, nrPersons); 225 | break; 226 | } 227 | case 'n': 228 | { 229 | printf("Entries? "); 230 | scanf("%i", &nrPersonsT); 231 | removeAllPersons(persons, nrPersons); 232 | nrPersons = nrPersonsT; 233 | persons = (struct person *)malloc(nrPersons * sizeof(struct person)); 234 | if (persons == NULL) 235 | { 236 | printf("malloc returned NULL"); 237 | break; 238 | } 239 | else 240 | { 241 | for (int i = 0; i < nrPersons; i++) 242 | { 243 | persons[i].name = NULL; 244 | persons[i].height = 0.0; 245 | } 246 | } 247 | break; 248 | } 249 | case 'p': 250 | { 251 | printPersons(persons, nrPersons, 0, 1); 252 | break; 253 | } 254 | case 'i': 255 | { 256 | 257 | printf("Index? "); 258 | scanf("%i", &newEntry); 259 | printf("Name? "); 260 | scanf("%s", newName); 261 | printf("Height? "); 262 | scanf("%f", &newHeightT); 263 | if (newHeightT > 0) 264 | { 265 | newHeight = newHeightT; 266 | insertPerson(persons, nrPersons, newEntry, newName, newHeight); 267 | } 268 | else 269 | { 270 | printf("Height must be larger than zero\n"); 271 | } 272 | break; 273 | } 274 | case 'h': 275 | { 276 | printf("Min: %.3f\n", minHeight(persons, nrPersons)); 277 | maxHeight(persons, nrPersons, &max); 278 | printf("Max: %.3f\n", max); 279 | printf("Range: %.3f\n", max - minHeight(persons, nrPersons)); 280 | break; 281 | } 282 | case 'r': 283 | { 284 | printf("Index? "); 285 | scanf("%i", &newEntry); 286 | printf("Name? "); 287 | scanf("%s", newName); 288 | printf("Height? "); 289 | scanf("%f", &newHeightT); 290 | if (newHeightT > 0) 291 | { 292 | newHeight = newHeightT; 293 | replacePerson(persons, nrPersons, newEntry, newName, newHeight); 294 | } 295 | else 296 | { 297 | printf("Height must be larger than zero\n"); 298 | } 299 | break; 300 | } 301 | case 'b': 302 | { 303 | bubbleSort(persons, nrPersons, &swapped); 304 | break; 305 | } 306 | case 'm': 307 | { 308 | mergeSort(persons, 0, nrPersons - 1); 309 | 310 | // if height is equal, sort by name 311 | for (int i = 0; i < nrPersons - 1; i++) 312 | { 313 | if (persons[i].height == persons[i + 1].height) 314 | { 315 | if (strcmp(persons[i].name, persons[i + 1].name) > 0) 316 | { 317 | swapPersons(persons, &persons[i], &persons[i + 1]); 318 | } 319 | } 320 | } 321 | break; 322 | } 323 | default: 324 | { 325 | printf("Unknown command '%c'\n", cmd); 326 | break; 327 | } 328 | } 329 | } while (cmd != 'q'); 330 | } -------------------------------------------------------------------------------- /week5/other/arrays-of-pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define ROWS 4 6 | 7 | int main(void) 8 | { 9 | int *a[ROWS]; 10 | for (int r = 0; r < ROWS; r++) 11 | { 12 | a[r] = (int *)malloc((r + 1) * sizeof(int)); 13 | for (int c = 0; c <= r; c++) 14 | { 15 | a[r][c] = 10 * r + c; 16 | } 17 | } 18 | for (int r = 0; r < ROWS; r++) 19 | { 20 | for (int c = 0; c <= r; c++) 21 | { 22 | printf("a[%d][%d]=%d address %p\n", r, c, a[r][c], &a[r][c]); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /week5/other/heap.c: -------------------------------------------------------------------------------- 1 | // dynamic data allocation. 2 | // call (malloc) to ask system for free space to allocate to. 3 | // call (free) to return the space previously allocated. 4 | 5 | // sample 6 | #include 7 | #include 8 | 9 | // using malloc dynamic data allocation 10 | void mallocStuff() 11 | { 12 | // here we allocate space of 10 characters 13 | char *c1 = (char *)malloc(10 * sizeof(char)); 14 | 15 | printf("input a character to test: "); 16 | scanf("%c", c1); 17 | 18 | printf("\nThe character is: %c\n", *c1); 19 | 20 | free(c1); 21 | } 22 | 23 | void pointerSwap(int **a, int **b) 24 | { 25 | int *tmp; 26 | tmp = *a; 27 | *a = *b; 28 | *b = tmp; 29 | } 30 | 31 | void print_array(int *a, int rows, int columns) 32 | { 33 | printf("Printing all values :\n"); 34 | for (int r = 0; r < rows; r++) 35 | { 36 | for (int c = 0; c < columns; c++) 37 | printf("%p: %d\n", &a[r * columns + c], a[r * columns + c]); 38 | printf("\n"); 39 | } 40 | printf("Printing all values (again):\n"); 41 | for (int r = 0; r < rows; r++) 42 | { 43 | for (int c = 0; c < columns; c++) 44 | printf("%p: %d\n", (a + r * columns + c), *(a + r * columns + c)); 45 | printf("\n"); 46 | } 47 | } 48 | 49 | int main(void) 50 | { 51 | mallocStuff(); 52 | 53 | int x = 1, y = 2; 54 | // storing the address of x and y in xp and yp 55 | int *xp = &x, *yp = &y; 56 | printf("input: %d %d\n", *xp, *yp); 57 | // pass in a pointer to xp and yp which are pointers to x and y 58 | pointerSwap(&xp, &yp); 59 | printf("swap xp&yp: %d %d\n", *xp, *yp); 60 | 61 | int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; 62 | print_array(&a[0][0], 2, 3); 63 | } -------------------------------------------------------------------------------- /week5/other/history.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define HISTORY 6 5 | 6 | // with stack its first come last leave - goes to bottom 7 | // with heap its first come first leave - goes to top 8 | 9 | int main(void) 10 | { 11 | char *history[HISTORY] = {NULL}; 12 | char cmd[100]; 13 | 14 | do 15 | { 16 | printf("Command? "); 17 | scanf("%s", cmd); 18 | 19 | if (history[HISTORY - 1] != NULL) 20 | { 21 | free(history[HISTORY - 1]); 22 | } 23 | 24 | // now we need to shift our heap 25 | // if string "ls" is in position 0 now its in position 1 26 | for (int i = HISTORY - 2; i >= 0; i--) 27 | { 28 | history[i + 1] = history[i]; 29 | } 30 | 31 | // now that we moved our heap we fill in the first empty space 32 | history[0] = (char *)malloc(strlen(cmd)); 33 | strcpy(history[0], cmd); 34 | 35 | if (!strcmp(cmd, "history")) 36 | { 37 | for (int i = HISTORY - 1; i >= 0; i--) 38 | { 39 | if (history[i] != NULL) 40 | { 41 | printf("%d %s\n", i, history[i]); 42 | } 43 | } 44 | } 45 | 46 | } while (strcmp(cmd, "quit")); 47 | printf("Bye!\n"); 48 | } -------------------------------------------------------------------------------- /week6/navigation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define START 'S' 5 | #define DESTINATION 'D' 6 | #define PATH '+' 7 | #define WALL '#' 8 | #define UNSEEN '.' 9 | #define SEEN '~' 10 | 11 | typedef struct 12 | { 13 | char *grid; 14 | int maxX; 15 | int maxY; 16 | } navigation_t; 17 | 18 | typedef struct 19 | { 20 | int x; 21 | int y; 22 | } position_t; 23 | 24 | void newGrid(navigation_t *nav) 25 | { 26 | if (nav->grid != NULL) 27 | { 28 | free(nav->grid); 29 | nav->grid = NULL; 30 | nav->maxX = 0; 31 | nav->maxY = 0; 32 | } 33 | printf("Number of rows? "); 34 | scanf(" %d", &nav->maxY); 35 | printf("Number of columns? "); 36 | scanf(" %d", &nav->maxX); 37 | if (nav->maxY < 2 || nav->maxX < 2) 38 | { 39 | printf("The number of rows and columns must be at least two\n"); 40 | return; 41 | } 42 | nav->grid = (char *)malloc(nav->maxX * nav->maxY * sizeof(char *)); 43 | } 44 | 45 | void inputGrid(navigation_t nav) 46 | { 47 | int sCount = 0; 48 | for (int y = 0; y < nav.maxY; y++) 49 | { 50 | printf("Input row %d: ", y); 51 | for (int x = 0; x < nav.maxX; x++) 52 | { 53 | scanf(" %c", &nav.grid[y * nav.maxX + x]); 54 | if (nav.grid[y * nav.maxX + x] == 'S') 55 | { 56 | sCount++; 57 | if (sCount > 1) 58 | { 59 | printf("Extra starting point\n"); 60 | nav.grid[y * nav.maxX + x] = '#'; 61 | } 62 | } 63 | } 64 | } 65 | } 66 | 67 | void printGrid(navigation_t nav) 68 | { 69 | for (int y = 0; y < nav.maxY; y++) 70 | { 71 | for (int x = 0; x < nav.maxX; x++) 72 | { 73 | printf("%c", nav.grid[y * nav.maxX + x]); 74 | } 75 | printf("\n"); 76 | } 77 | } 78 | 79 | position_t findStart(navigation_t nav) 80 | { 81 | position_t start; 82 | int startPoint = 0; 83 | int destinationPath = 0; 84 | for (int y = 0; y < nav.maxY; y++) 85 | { 86 | for (int x = 0; x < nav.maxX; x++) 87 | { 88 | if (nav.grid[y * nav.maxX + x] == 'S') 89 | { 90 | startPoint = 1; 91 | start.x = x; 92 | start.y = y; 93 | } 94 | if (nav.grid[y * nav.maxX + x] == 'D') 95 | { 96 | destinationPath = 1; 97 | } 98 | } 99 | } 100 | if (startPoint == 0) 101 | { 102 | printf("Grid contains no starting point\n"); 103 | start.x = -1; 104 | } 105 | if (destinationPath == 0) 106 | { 107 | printf("Grid contains no destination\n"); 108 | } 109 | return start; 110 | } 111 | 112 | int findPath(navigation_t nav, int x, int y, int length) 113 | { 114 | int nlength, elength, slength, wlength; 115 | if (x >= nav.maxX || x < 0 || y >= nav.maxY || y < 0) 116 | return 0; 117 | else if (nav.grid[y * nav.maxX + x] == WALL || nav.grid[y * nav.maxX + x] == SEEN || nav.grid[y * nav.maxX + x] == PATH || (nav.grid[y * nav.maxX + x] == START && length > 0)) 118 | return 0; 119 | else if (nav.grid[y * nav.maxX + x] == DESTINATION) 120 | return length; 121 | else if (nav.grid[y * nav.maxX + x] != START) 122 | nav.grid[y * nav.maxX + x] = PATH; 123 | if ((nlength = findPath(nav, x, y - 1, length + 1))) 124 | return nlength; 125 | if ((elength = findPath(nav, x + 1, y, length + 1))) 126 | return elength; 127 | if ((slength = findPath(nav, x, y + 1, length + 1))) 128 | return slength; 129 | if ((wlength = findPath(nav, x - 1, y, length + 1))) 130 | return wlength; 131 | if (nav.grid[y * nav.maxX + x] == UNSEEN || nav.grid[y * nav.maxX + x] == PATH) 132 | { 133 | nav.grid[y * nav.maxX + x] = SEEN; 134 | } 135 | return 0; 136 | } 137 | 138 | void resetPath(navigation_t nav) 139 | { 140 | // Reset all SEEN and PATH characters in the grid to UNSEEN, allowing findPath to be run again. 141 | for (int y = 0; y < nav.maxY; y++) 142 | { 143 | for (int x = 0; x < nav.maxX; x++) 144 | { 145 | if (nav.grid[y * nav.maxX + x] == SEEN || nav.grid[y * nav.maxX + x] == PATH) 146 | { 147 | nav.grid[y * nav.maxX + x] = UNSEEN; 148 | } 149 | } 150 | } 151 | } 152 | 153 | int max(int a, int b) 154 | { 155 | if (a > b) 156 | return a; 157 | else 158 | return b; 159 | } 160 | 161 | int longestPath(navigation_t nav, int x, int y, int length) 162 | { 163 | 164 | // Small change from findPath, which stops as soon as it finds a path, to not stop at the first match but keep going. 165 | int nlength, elength, slength, wlength; 166 | if (x >= nav.maxX || x < 0 || y >= nav.maxY || y < 0) 167 | return 0; 168 | else if (nav.grid[y * nav.maxX + x] == WALL || nav.grid[y * nav.maxX + x] == PATH || (nav.grid[y * nav.maxX + x] == START && length > 0)) 169 | return 0; 170 | else if (nav.grid[y * nav.maxX + x] == DESTINATION) 171 | { 172 | printf("Found a path of length %d\n", length); 173 | return length; 174 | } 175 | else if (nav.grid[y * nav.maxX + x] != START) 176 | nav.grid[y * nav.maxX + x] = PATH; 177 | nlength = longestPath(nav, x, y - 1, length + 1); 178 | elength = longestPath(nav, x + 1, y, length + 1); 179 | slength = longestPath(nav, x, y + 1, length + 1); 180 | wlength = longestPath(nav, x - 1, y, length + 1); 181 | if (nav.grid[y * nav.maxX + x] == UNSEEN || nav.grid[y * nav.maxX + x] == PATH) 182 | { 183 | nav.grid[y * nav.maxX + x] = SEEN; 184 | } 185 | return max(max(nlength, elength), max(slength, wlength)); 186 | } 187 | 188 | int main() 189 | { 190 | char cmd; 191 | navigation_t nav = {NULL}; 192 | position_t start; 193 | int pathLength = 0; 194 | do 195 | { 196 | printf("Command? "); 197 | scanf(" %c", &cmd); 198 | switch (cmd) 199 | { 200 | default: 201 | printf("Unknown command '%c'\n", cmd); 202 | break; 203 | case 'q': 204 | printf("Bye!\n"); 205 | break; 206 | case 'i': 207 | pathLength = 0; 208 | newGrid(&nav); 209 | if (nav.maxX >= 2 && nav.maxY >= 2) 210 | { 211 | inputGrid(nav); 212 | } 213 | break; 214 | case 'p': 215 | printGrid(nav); 216 | break; 217 | case 's': 218 | start = findStart(nav); 219 | if (start.x != -1) 220 | { 221 | printf("The start is at x=%d and y=%d\n", start.x, start.y); 222 | } 223 | break; 224 | case 'f': 225 | start = findStart(nav); 226 | pathLength = findPath(nav, start.x, start.y, pathLength); 227 | if (pathLength == 0) 228 | printf("No path found\n"); 229 | if (pathLength != 0) 230 | printf("Found a path of length %d\n", pathLength); 231 | printGrid(nav); 232 | break; 233 | case 'r': 234 | resetPath(nav); 235 | pathLength = 0; 236 | break; 237 | case 'l': 238 | start = findStart(nav); 239 | pathLength = longestPath(nav, start.x, start.y, pathLength); 240 | if (pathLength == 0) 241 | printf("No path found\n"); 242 | if (pathLength != 0) 243 | printf("The length of the longest path is %d\n", pathLength); 244 | break; 245 | } 246 | } while (cmd != 'q'); 247 | free(nav.grid); 248 | } -------------------------------------------------------------------------------- /week6/other/c-program.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int gcd(int a, int b) 7 | { 8 | while (a != b) 9 | { 10 | if (a > b) 11 | { 12 | return gcd(a - b, b); 13 | } 14 | else 15 | { 16 | return gcd(a, b - a); 17 | } 18 | } 19 | return a; 20 | } 21 | 22 | int read_int() 23 | { 24 | int x; 25 | scanf(" %d", &x); 26 | return x; 27 | } 28 | 29 | void print_string(char *s) 30 | { 31 | printf("%s", s); 32 | } 33 | 34 | void print_int(int x) 35 | { 36 | printf("%d", x); 37 | } 38 | 39 | int main(void) 40 | { 41 | int a, b, c; 42 | 43 | a = read_int(); 44 | b = read_int(); 45 | 46 | c = gcd(a, b); 47 | 48 | print_string("result = "); 49 | print_int(c); 50 | print_string("\n"); 51 | } -------------------------------------------------------------------------------- /week6/other/common-divisor.s: -------------------------------------------------------------------------------- 1 | .data 2 | str1: .ascii "result = \0" 3 | str2: .ascii "\n\0" 4 | 5 | .text 6 | 7 | .global main 8 | 9 | main: 10 | addi sp,sp,-32 11 | sw ra,28(sp) 12 | sw s0,24(sp) 13 | addi s0,sp,32 14 | call read_int 15 | mv s0,a0 16 | call read_int 17 | mv s1,a0 18 | mv a0,s0 19 | mv a1,s1 20 | call gcd 21 | back: 22 | mv s1,a0 23 | la a0,str1 24 | call print_string 25 | mv a0,s1 26 | call print_int 27 | la a0,str2 28 | call print_string 29 | lw ra,28(sp) 30 | lw s0,24(sp) 31 | addi sp,sp,32 32 | call show_pc 33 | call exit 34 | ret 35 | 36 | gcd: 37 | beq a0, a1, Return 38 | blt a0, a1, else 39 | sub a0, a0, a1 40 | call gcd 41 | 42 | else: sub a1, a1, a0 43 | call gcd 44 | 45 | Return: 46 | j back -------------------------------------------------------------------------------- /week7/concordance.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAXWORDS 60 7 | #define MAXINDEX 10 8 | typedef struct _entry_t { 9 | char *word; 10 | int indices[MAXINDEX]; 11 | } entry_t; 12 | 13 | void addWord(entry_t concordance[], char *word) { 14 | // check if word already in concord 15 | for (int i=0; i 0) { 46 | entry_t swap = concordance[j]; 47 | concordance[j] = concordance[j+1]; 48 | concordance[j+1] = swap; 49 | } 50 | } 51 | } 52 | } 53 | } 54 | 55 | void printConcordance(entry_t concordance[]) { 56 | // check if empty 57 | if (concordance[0].word == NULL) { 58 | printf("The concordance is empty\n"); 59 | return; 60 | } 61 | printf("Concordance\n"); 62 | for (int i=0; i *index) { 105 | *index = concordance[i].indices[j]; 106 | } 107 | } 108 | else { 109 | break; 110 | } 111 | } 112 | } 113 | 114 | if (*index != 0) { 115 | *index = *index+1; 116 | } 117 | 118 | char wordFile[100]; 119 | int counter = 0; 120 | // check if file exists 121 | FILE *fp; 122 | fp = fopen(filename, "r"); 123 | if (fp == NULL) { 124 | printf("Cannot open file %s\n", filename); 125 | return; 126 | } 127 | 128 | while (fscanf(fp, "%s",wordFile)!=EOF) { 129 | addWord(concordance, wordFile); 130 | addIndex(concordance, wordFile, *index+counter); 131 | counter++; 132 | } 133 | 134 | printf("Inserted %i words\n", counter); 135 | } 136 | 137 | void removeWord(entry_t concordance[], char *word) { 138 | int foundIndex = 0; 139 | for (int i = 0; i largestIndex) { 186 | largestIndex = concordance[i].indices[j]; 187 | } 188 | } 189 | } 190 | else { 191 | break; 192 | } 193 | } 194 | 195 | if (largestIndex == -1) { 196 | return; 197 | } 198 | 199 | // loop through continuously until matched then print if not print ? 200 | int found = 0; 201 | for (int k=0; k concordance[j+1].indices[0]) { 231 | entry_t temp = concordance[j]; 232 | concordance[j] = concordance[j+1]; 233 | concordance[j+1] = temp; 234 | } 235 | else if (concordance[j].indices[0] == concordance[j+1].indices[0]) { 236 | if (strcmp(concordance[j].word, concordance[j+1].word) > 0) { 237 | entry_t temp = concordance[j]; 238 | concordance[j] = concordance[j+1]; 239 | concordance[j+1] = temp; 240 | } 241 | } 242 | } 243 | } 244 | } 245 | 246 | } 247 | 248 | int main (void) 249 | { 250 | char cmd; 251 | entry_t concordance[MAXWORDS]; 252 | for (int i=0;i 2 | #include 3 | 4 | struct student 5 | { 6 | int age; 7 | double gpa; 8 | char grade; 9 | }; 10 | 11 | typedef char user[25]; 12 | 13 | typedef struct 14 | { 15 | int age; 16 | double gpa; 17 | char grade; 18 | } student2; 19 | 20 | int main() 21 | { 22 | 23 | // Pointers 24 | int var = 10; 25 | printf("var: %p\n", &var); 26 | printf("var: %d\n", var); 27 | 28 | int *pvar = &var; 29 | printf("pvar: %p\n", pvar); // whats stored pvar: address of var 30 | printf("pvar: %p\n", &pvar); // address of pvar 31 | printf("pvar: %d\n", *pvar); // where is the adress inside pvar pointing to 32 | 33 | printf("------------------\n"); 34 | 35 | // N dimensional array: 36 | // [arrays in array][vals in arrays of array] 37 | int numGrid[2][3] = {{1, 2, 3}, {4, 5, 6}}; 38 | // numGrid[0][1] = 2 39 | printf("%d\n", numGrid[0][1]); 40 | 41 | printf("------------------\n"); 42 | 43 | // Structs (Dictionaries) 44 | // custom data type 45 | struct student student1; 46 | student1.age = 19; 47 | student1.gpa = 3.4; 48 | printf("%d\n", student1.age); 49 | 50 | printf("------------------\n"); 51 | 52 | // Typedef - giving data type a nickname 53 | // char user1[25] = "Bro"; 54 | user userr1 = "Bro"; 55 | 56 | // struct student stu1 = {19, 3.4}; 57 | // printf("%d\n", stu1.age); 58 | 59 | // no need to use struct keyword 60 | student2 stu2 = {20, 5.0}; 61 | printf("%f\n", stu2.gpa); 62 | } -------------------------------------------------------------------------------- /week7/other/checking2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // malloc - heap memory 5 | 6 | // int *ptr = (int*)malloc(4) // allocate 4 bytes of memory 7 | 8 | int main() 9 | { 10 | int i, n; 11 | printf("Enter the num of ints: "); 12 | scanf("%d", &n); // & because we need the address 13 | 14 | int *ptr = (int *)malloc(n * sizeof(int)); 15 | 16 | if (ptr == NULL) 17 | { 18 | printf("Memory unavailable."); 19 | exit(1); 20 | } 21 | for (i = 0; i < n; i++) 22 | { 23 | printf("Enter an integer: "); 24 | // storing into the address the ptr provides us 25 | scanf("%d", ptr + i); // ptr is malloc so already gives us the address 26 | // + i 27 | // memory 1000 + 0 = 1000 28 | // memory 1000 + 1 = 1004 -- the next address 29 | } 30 | for (i = 0; i < n; i++) 31 | { 32 | // *() where is that address inside ptr e.g 1000 pointing to 33 | printf("%d \n", *(ptr + i)); 34 | } 35 | free(ptr); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /week7/other/files.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | FILE *fh; // pointer to file file type stored, file handle declared 6 | char filename[100]; 7 | 8 | int c; 9 | 10 | printf("file name? "); 11 | fscanf(stdin, "%s", filename); 12 | fh = fopen(filename, "r"); 13 | 14 | if (fh == NULL) 15 | { 16 | fprintf(stdout, "cannot open"); 17 | return; 18 | } 19 | 20 | c = getc(fh); 21 | while (c != EOF) 22 | ; 23 | { 24 | putc(c, stdout); 25 | c = getc(fh); 26 | } 27 | fclose(fh); 28 | } -------------------------------------------------------------------------------- /week7/other/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | FILE *fh; 6 | char filename[100]; 7 | int c; 8 | printf("file name? "); 9 | fscanf(stdin, "%s", filename); // stdin - stanadard input for reading a file 10 | 11 | fh = fopen(filename, "r"); // r - read mode 12 | 13 | if (fh==NULL){ 14 | fprintf(stdout, "cannot open %s\n", filename); 15 | return; 16 | } 17 | 18 | c = getc(fh); 19 | while ( c!=EOF) 20 | { 21 | putc(c,stdout); 22 | c = getc(fh); 23 | } 24 | fclose(fh); 25 | } -------------------------------------------------------------------------------- /week8/practice-exam.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAXWORDS 10 6 | #define MAXLENGTH 30 7 | 8 | int nrWords(char *dict[]) { 9 | int counter = 0; 10 | // num of strings stored in array 11 | for (int i = 0; icurrentLength) { 158 | currentLength = strlen(dict[i]); 159 | longIndex = i; 160 | } 161 | } 162 | else { 163 | break; 164 | } 165 | } 166 | return longIndex; 167 | } 168 | 169 | void sortDict(char *dict[], int sortedUntil) { 170 | int longestIndex = sortedUntil; 171 | // orders by decreasing length 172 | // recursive 173 | if (sortedUntil+1 == MAXWORDS) { 174 | return; 175 | } 176 | 177 | // find longest index 178 | for (int i = sortedUntil+1; i