#include #include #include void numberprinter(int inarr[], int arrlength) { for (int i = 0; i < arrlength; i++) { printf("number%d: %d\n", i, inarr[i]); } } 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; op++; } *arr_size = op; printf("op: %d\n", op); //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--) { int last_digit = input % 10; printf("last digit: %d\n", last_digit); digitarray[i] = last_digit; // printf("%d\n", digits[op]); input /= 10; } return digitarray; } 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 // if it is, print that it's a duplicate // if it isn't make it true. 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; } } } 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); printf("length of number: %d\n", digitslength); numberprinter(digits, digitslength); checkifduplicatedigit(digits,digitslength); // TODO: Memory leak! Call free(digits) here before the program exits. return 0; } /* somehow I have to find a way to get the number of digits in my number for now, we use modulo. whatever remains is the last digit of the number we then divide by 10 to remove the last number we can keep a counter, and we can put it into an array. however, if we use the iterator (which has to start from 0, because we don't know the size of the number?) then our number will be the wrong way around. malloc, blijkbaar very different */