|
1 | 1 | /* file: clib.h |
2 | 2 | author: David De Potter |
3 | 3 | 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. |
4 | 12 | */ |
5 | 13 |
|
6 | 14 | #ifndef CLIB_H_INCLUDED |
|
13 | 21 |
|
14 | 22 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
15 | 23 |
|
16 | | -#define ABS(a) ((a) < 0 ? -(a) : (a)) |
17 | | - |
18 | 24 | #define SIGN(a) ((a) > 0 ? 1 : ((a) < 0 ? -1 : 0)) |
19 | 25 |
|
20 | 26 | // macro definition for swapping two variables |
|
24 | 30 | // Examples: PRINT_ARRAY(myInts, "%d", 10); |
25 | 31 | // PRINT_ARRAY(myDbls, "%.2lf", 20); |
26 | 32 | #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" : ", "); \ |
30 | 36 | } |
31 | 37 |
|
32 | 38 | // macro for printing a matrix of a given type and dimensions |
33 | 39 | // Examples: PRINT_MATRIX(myInts, "%d", rows, cols); |
34 | 40 | // PRINT_MATRIX(myDbls, "%.2lf", rows, cols); |
35 | 41 | // PRINT_MATRIX(myChrs, "%c", rows, cols); |
36 | 42 | #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" : " "); \ |
41 | 47 | } \ |
42 | 48 | } |
43 | 49 |
|
|
54 | 60 | // CREATE_MATRIX(myChrs, char, 15, 10); |
55 | 61 | #define CREATE_MATRIX(type, matrix, rows, cols) \ |
56 | 62 | 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)); |
59 | 65 |
|
60 | 66 | // macro for freeing the memory of a matrix |
61 | 67 | #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]); \ |
64 | 70 | free(matrix); |
65 | 71 |
|
66 | 72 | // macro for reading input into an array of known length |
67 | 73 | // Examples: READ_ARRAY(myInts, "%d", 20); |
68 | 74 | // READ_ARRAY(myDbls, "%lf", 15); |
69 | 75 | // READ_ARRAY(myString, "%c", 10); |
70 | 76 | #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]); |
73 | 79 |
|
74 | 80 | // macro for reading input into a matrix of given dimensions |
75 | 81 | // Examples: READ_MATRIX(myInts, "%d", 10, 5); |
76 | 82 | // READ_MATRIX(myDbls, "%lf", 8, 8); |
77 | 83 | // READ_MATRIX(myChrs, "%c", 5, 10); |
78 | 84 | #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]); |
82 | 88 |
|
83 | 89 | // macro for reading input from stdin as long as it lasts |
84 | 90 | // creates a new array of the given type and format, |
|
92 | 98 | int arr##Len = 0; type arr##var; \ |
93 | 99 | while (scanf(format, &arr##var) == 1) { \ |
94 | 100 | arr[arr##Len++] = arr##var; \ |
95 | | - if (arr##Len % 100 == 0) \ |
| 101 | + if (arr##Len % 100 == 0) {\ |
96 | 102 | arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \ |
| 103 | + memset(arr + arr##Len, 0, 100 * sizeof(type)); \ |
| 104 | + } \ |
97 | 105 | } \ |
98 | 106 | size = arr##Len;\ |
99 | 107 | arr[arr##Len] = '\0'; |
|
109 | 117 | size_t arr##Len = 0; type arr##var; \ |
110 | 118 | while (scanf(format, &arr##var) == 1 && arr##var != delim) { \ |
111 | 119 | arr[arr##Len++] = arr##var; \ |
112 | | - if (arr##Len % 100 == 0) \ |
| 120 | + if (arr##Len % 100 == 0) { \ |
113 | 121 | arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \ |
| 122 | + memset(arr + arr##Len, 0, 100 * sizeof(type)); \ |
| 123 | + } \ |
114 | 124 | } \ |
115 | 125 | size = arr##Len;\ |
116 | 126 | arr[arr##Len] = '\0'; |
|
0 commit comments