now uses struct for digits array
This commit is contained in:
@@ -8,60 +8,58 @@ void numberprinter(int inarr[], int arrlength) {
|
||||
}
|
||||
}
|
||||
|
||||
struct Darray {
|
||||
int *digits;
|
||||
int length;
|
||||
};
|
||||
|
||||
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
|
||||
struct Darray *numbertodigitDarr(int input) {
|
||||
|
||||
// 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.
|
||||
// same calculation for arrsize
|
||||
int op = 0;
|
||||
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) {
|
||||
// allocate memory for struct and array, apparently more ideomatic to have a single malloc
|
||||
struct Darray *DigDarr =
|
||||
malloc(op * sizeof(int) + sizeof(*DigDarr) + sizeof(int));
|
||||
if (DigDarr == NULL) {
|
||||
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--) {
|
||||
int last_digit = input % 10;
|
||||
printf("last digit: %d\n", last_digit);
|
||||
digitarray[i] = last_digit;
|
||||
// printf("%d\n", digits[op]);
|
||||
DigDarr->digits[i] = last_digit;
|
||||
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.
|
||||
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]);
|
||||
//
|
||||
void checkarrdupes(struct Darray input) {
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
printf("digitstr: %d\n", input.digits[i]);
|
||||
}
|
||||
|
||||
int digit_seen[10] = {};
|
||||
|
||||
// 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 {
|
||||
// TODO: Bug! You are marking the index 'i' as seen, not the digit value 'input[i]'.
|
||||
digit_seen[i] = 1;
|
||||
digit_seen[input.digits[i]] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,15 +69,15 @@ int main(void) {
|
||||
int n;
|
||||
|
||||
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);
|
||||
int digitslength = 0;
|
||||
int *digits = numbertodigitarray(n, &digitslength);
|
||||
printf("length of number: %d\n", digitslength);
|
||||
numberprinter(digits, digitslength);
|
||||
struct Darray *mydigitsDarr = numbertodigitDarr(n);
|
||||
|
||||
checkifduplicatedigit(digits,digitslength);
|
||||
// TODO: Memory leak! Call free(digits) here before the program exits.
|
||||
checkarrdupes(*mydigitsDarr);
|
||||
|
||||
free(mydigitsDarr);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user