digitseen.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. void numberprinter(int inarr[], int arrlength) {
  5. for (int i = 0; i < arrlength; i++) {
  6. printf("number%d: %d\n", i, inarr[i]);
  7. }
  8. }
  9. int *numbertodigitarray(int input, int *arr_size) {
  10. // TODO: This function loops twice. Consider a fixed-size array (max 11 digits)
  11. // or reading input as a string to avoid this.
  12. int op = 0; //counter which we use to calc length of array
  13. // if we had arr length I could just iterate with this while loop
  14. // but we don't so we're using the same modulo method to calculate the amount
  15. // of digits and put it in op
  16. // tempinput so we don't overwrite our actual input through this calculation
  17. // we should probably be using a struct for this. instead
  18. // TODO: Define a struct { int *digits; int length; } to return both values cleanly.
  19. int tempinput = input;
  20. while (tempinput) {
  21. tempinput /= 10;
  22. op++;
  23. }
  24. *arr_size = op;
  25. printf("op: %d\n", op);
  26. //initialize array now that we know what our arr length is
  27. int *digitarray = malloc(op * sizeof(int));
  28. //TODO we're not handling malloc
  29. if (digitarray == NULL) {
  30. printf("allocation failed");
  31. exit(0); // TODO: Use EXIT_FAILURE instead of 0 for allocation errors.
  32. }
  33. //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)
  34. for (int i = op -1 ; i >= 0; i--) {
  35. int last_digit = input % 10;
  36. printf("last digit: %d\n", last_digit);
  37. digitarray[i] = last_digit;
  38. // printf("%d\n", digits[op]);
  39. input /= 10;
  40. }
  41. return digitarray;
  42. }
  43. void checkifduplicatedigit(int input[], int inputlength) {
  44. // TODO: Initialize this array to 0 (e.g., {0}) to avoid garbage values from the stack.
  45. int digit_seen[10]; // not yet relevant
  46. for (int i = 0; i < inputlength; i++) {
  47. // check if digit_seen[input[i]] is true
  48. // if it is, print that it's a duplicate
  49. // if it isn't make it true.
  50. if (digit_seen[input[i]] == 1) {
  51. printf("duplicate number: %d\n", input[i]);
  52. } else {
  53. // TODO: Bug! You are marking the index 'i' as seen, not the digit value 'input[i]'.
  54. digit_seen[i] = 1;
  55. }
  56. }
  57. }
  58. int main(void) {
  59. int n;
  60. printf("enter a number: ");
  61. // TODO: scanf is fragile. Try using fgets() and strtol() for robust input sanitization.
  62. scanf("%d", &n);
  63. int digitslength = 0;
  64. int *digits = numbertodigitarray(n, &digitslength);
  65. printf("length of number: %d\n", digitslength);
  66. numberprinter(digits, digitslength);
  67. checkifduplicatedigit(digits,digitslength);
  68. // TODO: Memory leak! Call free(digits) here before the program exits.
  69. return 0;
  70. }
  71. /*
  72. somehow I have to find a way to get the number of digits in my number
  73. for now, we use modulo. whatever remains is the last digit of the number
  74. we then divide by 10 to remove the last number
  75. we can keep a counter, and we can put it into an array. however, if we use the
  76. iterator (which has to start from 0, because we don't know the size of the
  77. number?) then our number will be the wrong way around.
  78. malloc, blijkbaar very different
  79. */