1

I am working on an old exam and the problems states that a given array (int zahlen[]={1,4,5,1,5,7,9,2,3,4}) has values that are the same. The task is to replace the values that are the same with '-1'. After each replacement, a given variable, count, has to be increased by one.

My problem is that the variable count is two-times higher than normal (In this case there are only 3 of the same numbers and the variable shows 6.)

The function is called array_unique. I am would be grateful for a brief explanation of my mistake.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    system("chcp 1252");
    int zahlen[]={1,4,5,1,5,7,9,2,3,4};
    int len = sizeof(zahlen)/sizeof(int);
    int erg = array_unique(zahlen,len);
    printf("Es wurden %d doppelte Zahlen gelöscht: \n",erg);
    printf("Das Array hat nun folgende Werte: ");
    printArrayUnique(zahlen,len);
    return 0;
}

void printArrayUnique(int *array, int len){
    for(int i=0; i<len; i++){
        if(array[i]!=-1){
            printf("%d ",array[i]);

        }
    }
}

int array_unique(int *array, int len){
    int count=0;
    for(int i=0; i<len;i++){
        for(int j=i+1; j<len;j++){
            if(array[i]==array[j]){
                array[j] = -1;
                count++;


            }
        }

    }
    return count;
}

I have not figured out any other solution to fix the faulty value of count.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    In C you need to *declare* all the functions you call, before you call them. Your compiler should be complaining about the undeclared functions. – Some programmer dude Jan 09 '23 at 10:40
  • Other than that, now seems like a very good time to learn how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). With a debugger you can step through the code line by line while monitoring variables and their values. That will make it easy to see if and where things go wrong. – Some programmer dude Jan 09 '23 at 10:42
  • Welcome to SO. What have you tried to debug your code? Did you run it in a debugger and inspect variables? Did you try to print the array index values that are replaced and counted as duplicates in your loop? What was the result? – Gerhardh Jan 09 '23 at 10:43
  • The compiler is not complaining, as I declared them in the main function. – Christian Jiga Jan 09 '23 at 10:43
  • 1
    What do you suppose `if(array[i]==array[j])` determines when you run across two of your already-negated slots (e.g two slots have been "de-duplicated" by setting -1 in them, but in doing so you just created a new set of duplicates (duplicate -1's)). If `-1` is going to be used as a don't-count-me tombstone, both `j` and `i` loops should skip those outright. – WhozCraig Jan 09 '23 at 10:44
  • You don't explicitely declare those functions in `main`. You use implicit function declaration. That should cause a warning for any recent compiler. – Gerhardh Jan 09 '23 at 10:47

1 Answers1

1

The issue is due to the fact that your are counting duplicates more than once; so, when you have found a duplicate entry, you correctly replace that with -1 but then, later in the loops, you will be (potentially, at least) comparing two or more of those -1 values.

Just add a check that either value is not -1 (along with the test for equality) before incrementing the count variable:

int array_unique(int* array, int len)
{
    int count = 0;
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            if (array[i] == array[j] && array[j] != -1) {
                array[j] = -1;
                count++;
            }
        }
    }
    return count;
}

Note also that, as mentioned in the comments, you really do need declarations of your functions before you use them. Add the following two lines before the main function:

void printArrayUnique(int* array, int len);
int array_unique(int* array, int len);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83