digitseen.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. int op = 0; //counter which we use to calc length of array
  11. // if we had arr length I could just iterate with this while loop
  12. // but we don't so we're using the same modulo method to calculate the amount
  13. // of digits and put it in op
  14. // tempinput so we don't overwrite our actual input through this calculation
  15. int tempinput = input;
  16. while (tempinput) {
  17. tempinput /= 10;
  18. op++;
  19. }
  20. *arr_size = op;
  21. printf("op: %d\n", op);
  22. //initialize array now that we know what our arr length is
  23. int *digitarray = malloc(op * sizeof(int));
  24. //TODO we're not handling malloc
  25. //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)
  26. for (int i = op -1 ; i >= 0; i--) {
  27. int last_digit = input % 10;
  28. printf("last digit: %d\n", last_digit);
  29. digitarray[i] = last_digit;
  30. // printf("%d\n", digits[op]);
  31. input /= 10;
  32. }
  33. return digitarray;
  34. }
  35. void checkifduplicatedigit(int input[], int inputlength) {
  36. int digit_seen[10]; // not yet relevant
  37. for (int i = 0; i < inputlength; i++) {
  38. // check if digit_seen[input[i]] is true
  39. // if it is, print that it's a duplicate
  40. // if it isn't make it true.
  41. if (digit_seen[input[i]] == 1) {
  42. printf("duplicate number: %d\n", input[i]);
  43. } else {
  44. digit_seen[i] = 1;
  45. }
  46. }
  47. }
  48. int main(void) {
  49. int n;
  50. printf("enter a number: ");
  51. scanf("%d", &n);
  52. int digitslength = 0;
  53. int *digits = numbertodigitarray(n, &digitslength);
  54. printf("length of number: %d\n", digitslength);
  55. numberprinter(digits, digitslength);
  56. checkifduplicatedigit(digits,digitslength);
  57. return 0;
  58. }
  59. /*
  60. somehow I have to find a way to get the number of digits in my number
  61. for now, we use modulo. whatever remains is the last digit of the number
  62. we then divide by 10 to remove the last number
  63. we can keep a counter, and we can put it into an array. however, if we use the
  64. iterator (which has to start from 0, because we don't know the size of the
  65. number?) then our number will be the wrong way around.
  66. malloc, blijkbaar very different
  67. */