Skip to content

Commit 9129708

Browse files
committed
🎉
1 parent 93f8bcf commit 9129708

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

Extra-C/prob03/prob3-2.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
version: 3.2, using clib library
66
also changed the implementation to use only two
77
rows of size n instead of a 2D array of size n²
8-
Time complexity is still O(n²)
9-
Space complexity is now O(n) instead of O(n²) of
10-
the previous implementation
8+
Time complexity is still O(n²), but the space
9+
complexity is now O(n) instead of O(n²)
1110
*/
1211

1312
#include <stdio.h>
@@ -16,15 +15,15 @@
1615

1716
int maxPath (int *row1, int *row2, int n) {
1817
/* computes the maximum path cost */
18+
1919
// read the first row
2020
READ_ARRAY(row1, "%d ", 1);
2121

22-
// calculate the maximum path cost for each cell
2322
for (int i = 2; i <= n; ++i) {
2423
// read the next row
2524
READ_ARRAY(row2, "%d ", i);
2625

27-
// calculate the maximum path cost for each cell
26+
// compute the maximum path cost for each cell
2827
for (int j = 0; j < i; ++j) {
2928
if (j == 0)
3029
row2[j] += row1[j];

Functions/clib/clib.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/* file: clib.h
22
author: David De Potter
33
description: header file for the clib library
4+
NOTES:
5+
- I mostly use ints just to be in line with the course material.
6+
- I have not wrapped any macros in do { ... } while(0) loops,
7+
because I don't see the need for it.
8+
- Always use a unique name for the array or matrix when using
9+
the create and read macros, as the macro creates a variable
10+
with that name. No need to declare the variable before using
11+
the macro.
412
*/
513

614
#ifndef CLIB_H_INCLUDED
@@ -13,8 +21,6 @@
1321

1422
#define MIN(a, b) ((a) < (b) ? (a) : (b))
1523

16-
#define ABS(a) ((a) < 0 ? -(a) : (a))
17-
1824
#define SIGN(a) ((a) > 0 ? 1 : ((a) < 0 ? -1 : 0))
1925

2026
// macro definition for swapping two variables
@@ -24,20 +30,20 @@
2430
// Examples: PRINT_ARRAY(myInts, "%d", 10);
2531
// PRINT_ARRAY(myDbls, "%.2lf", 20);
2632
#define PRINT_ARRAY(arr, format, len) \
27-
for (int arr##i = 0; arr##i < len; ++arr##i){ \
28-
printf(format, arr[arr##i]);\
29-
printf(arr##i == len-1 ? "\n" : ", "); \
33+
for (int i = 0; i < len; ++i){ \
34+
printf(format, arr[i]);\
35+
printf(i == len-1 ? "\n" : ", "); \
3036
}
3137

3238
// macro for printing a matrix of a given type and dimensions
3339
// Examples: PRINT_MATRIX(myInts, "%d", rows, cols);
3440
// PRINT_MATRIX(myDbls, "%.2lf", rows, cols);
3541
// PRINT_MATRIX(myChrs, "%c", rows, cols);
3642
#define PRINT_MATRIX(matrix, format, rows, cols) \
37-
for (int matrix##i = 0; matrix##i < rows; ++matrix##i) { \
38-
for (int matrix##j = 0; matrix##j < cols; ++matrix##j) {\
39-
printf(format, matrix[matrix##i][matrix##j]); \
40-
printf(matrix##j == cols-1 ? "\n" : " "); \
43+
for (int i = 0; i < rows; ++i) { \
44+
for (int j = 0; j < cols; ++j) {\
45+
printf(format, matrix[i][j]); \
46+
printf(j == cols-1 ? "\n" : " "); \
4147
} \
4248
}
4349

@@ -54,31 +60,31 @@
5460
// CREATE_MATRIX(myChrs, char, 15, 10);
5561
#define CREATE_MATRIX(type, matrix, rows, cols) \
5662
type **matrix = safeCalloc(rows, sizeof(type *)); \
57-
for (int matrix##i = 0; matrix##i < rows; ++matrix##i) \
58-
matrix[matrix##i] = safeCalloc(cols, sizeof(type));
63+
for (int i = 0; i < rows; ++i) \
64+
matrix[i] = safeCalloc(cols, sizeof(type));
5965

6066
// macro for freeing the memory of a matrix
6167
#define FREE_MATRIX(matrix, rows) \
62-
for (int matrix##i = 0; matrix##i < rows; ++matrix##i) \
63-
free(matrix[matrix##i]); \
68+
for (int i = 0; i < rows; ++i) \
69+
free(matrix[i]); \
6470
free(matrix);
6571

6672
// macro for reading input into an array of known length
6773
// Examples: READ_ARRAY(myInts, "%d", 20);
6874
// READ_ARRAY(myDbls, "%lf", 15);
6975
// READ_ARRAY(myString, "%c", 10);
7076
#define READ_ARRAY(arr, format, len) \
71-
for (int arr##i = 0; arr##i < len; ++arr##i) \
72-
(void)! scanf(format, &arr[arr##i]);
77+
for (int i = 0; i < len; ++i) \
78+
(void)! scanf(format, &arr[i]);
7379

7480
// macro for reading input into a matrix of given dimensions
7581
// Examples: READ_MATRIX(myInts, "%d", 10, 5);
7682
// READ_MATRIX(myDbls, "%lf", 8, 8);
7783
// READ_MATRIX(myChrs, "%c", 5, 10);
7884
#define READ_MATRIX(matrix, format, rows, cols) \
79-
for (int arr##i = 0; arr##i < rows; ++arr##i) \
80-
for (int arr##j = 0; arr##j < cols; ++arr##j) \
81-
(void)! scanf(format, &matrix[arr##i][arr##j]);
85+
for (int i = 0; i < rows; ++i) \
86+
for (int j = 0; j < cols; ++j) \
87+
(void)! scanf(format, &matrix[i][j]);
8288

8389
// macro for reading input from stdin as long as it lasts
8490
// creates a new array of the given type and format,
@@ -92,8 +98,10 @@
9298
int arr##Len = 0; type arr##var; \
9399
while (scanf(format, &arr##var) == 1) { \
94100
arr[arr##Len++] = arr##var; \
95-
if (arr##Len % 100 == 0) \
101+
if (arr##Len % 100 == 0) {\
96102
arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \
103+
memset(arr + arr##Len, 0, 100 * sizeof(type)); \
104+
} \
97105
} \
98106
size = arr##Len;\
99107
arr[arr##Len] = '\0';
@@ -109,8 +117,10 @@
109117
size_t arr##Len = 0; type arr##var; \
110118
while (scanf(format, &arr##var) == 1 && arr##var != delim) { \
111119
arr[arr##Len++] = arr##var; \
112-
if (arr##Len % 100 == 0) \
120+
if (arr##Len % 100 == 0) { \
113121
arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \
122+
memset(arr + arr##Len, 0, 100 * sizeof(type)); \
123+
} \
114124
} \
115125
size = arr##Len;\
116126
arr[arr##Len] = '\0';

IP-Midterms/mid2022/problem1/prob1.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <stdio.h>
77
#include <stdlib.h>
88

9-
#define ABS(x) ((x) < 0 ? -(x) : (x))
10-
119
void readTime(int time[]){
1210
for (int i = 0; i < 3; i++){
1311
time[i] = (getchar() - '0') * 10 + (getchar() - '0');
@@ -24,8 +22,8 @@ int main(int argc, char *argv[]) {
2422
int diff = (time2[0] - time1[0]) * 3600 +
2523
(time2[1] - time1[1]) * 60 + (time2[2] - time1[2]);
2624

27-
printf("%02d:%02d:%02d\n", ABS(diff / 3600),
28-
ABS((diff % 3600) / 60), ABS(diff % 60));
25+
printf("%02d:%02d:%02d\n", abs(diff / 3600),
26+
abs((diff % 3600) / 60), abs(diff % 60));
2927

3028
return 0;
3129
}

0 commit comments

Comments
 (0)