now uses struct for digits array

This commit is contained in:
2026-05-25 01:24:18 +02:00
parent 164e9d961b
commit 7ecaece37f
+41 -43
View File
@@ -8,61 +8,59 @@ void numberprinter(int inarr[], int arrlength) {
} }
} }
struct Darray {
int *digits;
int length;
};
int *numbertodigitarray(int input, int *arr_size) { struct Darray *numbertodigitDarr(int input) {
// 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 // same calculation for arrsize
// but we don't so we're using the same modulo method to calculate the amount int op = 0;
// 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; int tempinput = input;
while (tempinput) { while (tempinput) {
tempinput /= 10; tempinput /= 10;
op++; op++;
} }
*arr_size = op; // allocate memory for struct and array, apparently more ideomatic to have a single malloc
struct Darray *DigDarr =
printf("op: %d\n", op); malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
if (DigDarr == NULL) {
//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"); printf("allocation failed");
exit(0); // TODO: Use EXIT_FAILURE instead of 0 for allocation errors. exit(0);
} }
// forgot to assign a value to the pointer variable in the struct.
DigDarr->digits = (int *)(DigDarr + 1);
DigDarr->length = op;
//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) // insert numbers into array in reverse order
for (int i = op -1 ; i >= 0; i--) { for (int i = op - 1; i >= 0; i--) {
int last_digit = input % 10; int last_digit = input % 10;
printf("last digit: %d\n", last_digit); printf("last digit: %d\n", last_digit);
digitarray[i] = last_digit; DigDarr->digits[i] = last_digit;
// printf("%d\n", digits[op]);
input /= 10; input /= 10;
} }
return digitarray; return DigDarr;
} }
void checkifduplicatedigit(int input[], int inputlength) { //
// TODO: Initialize this array to 0 (e.g., {0}) to avoid garbage values from the stack. void checkarrdupes(struct Darray input) {
int digit_seen[10]; // not yet relevant for (int i = 0; i < input.length; i++) {
for (int i = 0; i < inputlength; i++) { printf("digitstr: %d\n", input.digits[i]);
// check if digit_seen[input[i]] is true }
// if it is, print that it's a duplicate
// if it isn't make it true. int digit_seen[10] = {};
if (digit_seen[input[i]] == 1) {
printf("duplicate number: %d\n", input[i]); // kijk door elke arr element
for (int i = 0; i < input.length; i++) {
// kijk of input[i] al in digit_seen zit
if (digit_seen[input.digits[i]]) {
printf("duplicate number: %d\n", input.digits[i]);
} else { } else {
// TODO: Bug! You are marking the index 'i' as seen, not the digit value 'input[i]'. digit_seen[input.digits[i]] = 1;
digit_seen[i] = 1; }
}
} }
} }
@@ -71,15 +69,15 @@ int main(void) {
int n; int n;
printf("enter a number: "); printf("enter a number: ");
// TODO: scanf is fragile. Try using fgets() and strtol() for robust input sanitization. // TODO: scanf is fragile. Try using fgets() and strtol() for robust input
// sanitization.
scanf("%d", &n); scanf("%d", &n);
int digitslength = 0; int digitslength = 0;
int *digits = numbertodigitarray(n, &digitslength); struct Darray *mydigitsDarr = numbertodigitDarr(n);
printf("length of number: %d\n", digitslength);
numberprinter(digits, digitslength);
checkifduplicatedigit(digits,digitslength); checkarrdupes(*mydigitsDarr);
// TODO: Memory leak! Call free(digits) here before the program exits.
free(mydigitsDarr);
return 0; return 0;
} }
/* /*