From 164e9d961be07fa7b9f2026e1acd622a46c4b20c Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 21 May 2026 20:57:07 +0200 Subject: [PATCH] cm --- med/C-programming-a-modern-standard/digitseen.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/med/C-programming-a-modern-standard/digitseen.c b/med/C-programming-a-modern-standard/digitseen.c index ad321d2..a7a76e4 100644 --- a/med/C-programming-a-modern-standard/digitseen.c +++ b/med/C-programming-a-modern-standard/digitseen.c @@ -10,12 +10,16 @@ void numberprinter(int inarr[], int arrlength) { int *numbertodigitarray(int input, int *arr_size) { + // TODO: This function loops twice. Consider a fixed-size array (max 11 digits) + // or reading input as a string to avoid this. int op = 0; //counter which we use to calc length of array // if we had arr length I could just iterate with this while loop // but we don't so we're using the same modulo method to calculate the amount // of digits and put it in op // tempinput so we don't overwrite our actual input through this calculation + // we should probably be using a struct for this. instead + // TODO: Define a struct { int *digits; int length; } to return both values cleanly. int tempinput = input; while (tempinput) { tempinput /= 10; @@ -28,8 +32,11 @@ int *numbertodigitarray(int input, int *arr_size) { //initialize array now that we know what our arr length is int *digitarray = malloc(op * sizeof(int)); - //TODO we're not handling malloc + if (digitarray == NULL) { + printf("allocation failed"); + exit(0); // TODO: Use EXIT_FAILURE instead of 0 for allocation errors. + } //modulo the number to get the digits and put it into the created array, however, now that we know the length of the number, we could iterate in reverse to match the fact that modulo always gives us the last number (the leftovers) for (int i = op -1 ; i >= 0; i--) { @@ -44,6 +51,7 @@ int *numbertodigitarray(int input, int *arr_size) { } void checkifduplicatedigit(int input[], int inputlength) { + // TODO: Initialize this array to 0 (e.g., {0}) to avoid garbage values from the stack. int digit_seen[10]; // not yet relevant for (int i = 0; i < inputlength; i++) { // check if digit_seen[input[i]] is true @@ -52,6 +60,7 @@ void checkifduplicatedigit(int input[], int inputlength) { if (digit_seen[input[i]] == 1) { printf("duplicate number: %d\n", input[i]); } else { + // TODO: Bug! You are marking the index 'i' as seen, not the digit value 'input[i]'. digit_seen[i] = 1; } } @@ -62,6 +71,7 @@ int main(void) { int n; printf("enter a number: "); + // TODO: scanf is fragile. Try using fgets() and strtol() for robust input sanitization. scanf("%d", &n); int digitslength = 0; int *digits = numbertodigitarray(n, &digitslength); @@ -69,6 +79,7 @@ int main(void) { numberprinter(digits, digitslength); checkifduplicatedigit(digits,digitslength); + // TODO: Memory leak! Call free(digits) here before the program exits. return 0; } /*