basic implementation of duplicate digit checker
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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) {
|
||||
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
|
||||
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
|
||||
|
||||
//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) {
|
||||
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 {
|
||||
digit_seen[i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
int n;
|
||||
|
||||
printf("enter a number: ");
|
||||
scanf("%d", &n);
|
||||
int digitslength = 0;
|
||||
int *digits = numbertodigitarray(n, &digitslength);
|
||||
printf("length of number: %d\n", digitslength);
|
||||
numberprinter(digits, digitslength);
|
||||
|
||||
checkifduplicatedigit(digits,digitslength);
|
||||
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
|
||||
*/
|
||||
Reference in New Issue
Block a user