2

I wrote a function creating a dynamic array of random values and another function creating a new array consisting of unique values of the previous array. The algorithm used counts unique values correctly. However, I faced a problem in printing all values. In the example below the program printed 7 2 12714320 4 5 instead of 7 2 4 5 6 .

This is the program which can be tested:

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

int *delduplicate(int *v, int size_old, int *size_new);

main()
{
    int n;
    int *norepeat;
    float *results;
    int dim, size_norepeat, i;
    
    int a[7] = {7,2,2,4,5,6,7};
    
    norepeat = delduplicate(a, 7, &size_norepeat);
    
    for (int i = 0; i < size_norepeat; i++)
        printf("%d ", norepeat[i]);
}



// delduplicate function
int *delduplicate(int *v, int size_old, int *size_new)
{
    int i, j, k = 1, uniques = 1, repeats, *new_v, temp;
    
    // count the number of unique elements
    for (i = 1; i < size_old; i++)
    {
        int is_unique = 1;
        
        for (j = 0; is_unique && j < i; j++)
        {
            if (v[i] == v[j])
                is_unique = 0;          
        }
        
        if (is_unique)
            uniques++;
    }
    
    *size_new = uniques;
    
    // create new array of unique elements
    new_v = (int*) malloc(*size_new * sizeof(int));
    
    // fill new array with unique elements
    new_v[0] = v[0];
    
    for (i = 1; i < size_old; i++)
    {
        int is_unique = 1;
            
        for (j = 0; j < i; j++)
        {
            if (v[i] == v[j])
                is_unique = 0;
        }
    
        if (is_unique)
            new_v[k] = v[i];
            k++;
            
    }
    return new_v;
}

The problem should be happening here:

// fill new array with unique elements
new_v[0] = v[0];

for (i = 1; i < size_old; i++)
{
    int is_unique = 1;
        
    for (j = 0; j < i; j++)
    {
        if (v[i] == v[j])
            is_unique = 0;
    }

    if (is_unique)
        new_v[k] = v[i];
        k++;       
}
8
  • 2
    what is "the problem" ? Commented Jun 20, 2020 at 16:14
  • 1
    when the posted code is run through a compiler, a LONG string of warnings is output. Starting with: untitled1.c:7:1: warning: return type defaults to ‘int’ [-Wimplicit-int] When compiling, always enable the warnings, then fix those warnigs. ( for gcc, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11 ) Note: other compilers use different options to produce the same results. Commented Jun 21, 2020 at 4:31
  • 1
    Note: the last line of the compiler output may be: Compilation finished successfully. but that only means the compiler applied some workaround to the problems, not that the correct code was produced. Commented Jun 21, 2020 at 4:32
  • 1
    OT: for ease of readability and understanding: 1) please follow the axiom: only one statement per line and (at most) one variable declaration per statement. Commented Jun 21, 2020 at 4:35
  • 1
    OT: regarding: new_v = (int*) malloc(*size_new * sizeof(int)); 1) the function: malloc() expects a parameter of type: size_t but size_new is an integer. 2) in C, the returned type is void* which can be assigned to any pointer. Casting just clutters the code and is error prone. 3) always check (!=NULL) the returned value to assure the operation was successful. Commented Jun 21, 2020 at 4:39

1 Answer 1

3

Your problem is probably occurring in the following section -

if (is_unique)
    new_v[k] = v[i];
    k++;

Here you are incrementing k at each iteration. However, you only want to increment it whenever you have found a unique element. if() without brackets only considers the first statement. So change it to this -

if (is_unique){
    new_v[k] = v[i];
    k++;
}

This change should make your program run fine.


Side Note : If you do not want to use brackets for an if() , for() , etc, you can separate the statements by commas and use without having the brackets. Like this -

if (is_unique)
    new_v[k] = v[i],
    k++;
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. You could also do it like new_v[k++] = v[i].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.