Home > Software design >  I want to show elements of a matrix that appear only once
I want to show elements of a matrix that appear only once

Time:05-29

I got this but it I can't seem to make it work, tried everything I could think of.

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

void alocare_vector(int *v, int nr, int elem)
{
    int *v1;
    if ((v1 = realloc(v, nr * sizeof(int))) == NULL)
    {
        printf("Memorie insuficienta");
        free(v);
        exit(EXIT_FAILURE);
    }
    v = v1;
    v[nr - 1] = elem;
}
int main()
{
    int a[100][100];
    int n, *v = NULL, i, j, k, l, ok = 0, nr = 0;
    printf("Number de elements n:");
    scanf("%d", &n);
    for (i = 0; i < n; i  )
        for (j = 0; j < n; j  )
            scanf("%d", &a[i][j]);
    for (k = 0; k < n; k  )
    {
        for (l = 0; l < n; l  )
        {
            for (i = 0; i < n; i  )
                for (j = 0; j < n; j  )
                    if (a[k][l] == a[i][j] && (k != i && l != j))
                        ok = 1;
            if (ok == 0)
            {
                nr  ;
                alocare_vector(v, nr, a[k][l]);
            }
            ok = 0;
        }
    }
    if (nr > 0)
    {
        printf("Elements:");
        for (i = 0; i < nr; i  )
        {
            printf("%d ", v[i]);
        }
    }
    free(v);

}

Basically, I need to create a matrix, ans print the elements that appear only once using an array with dynamic memory allocation.

CodePudding user response:

one simple fix is to declare the "int * v" as a global variable and modify the function as

alocare_vector(int nr, int elem)

now using a hash table you can store the numbers in the array, below is the your implementation just a bit modified.

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

int hash[1000]={0};
int * v=NULL;

void alocare_vector(int nr, int elem)
{
    int *v1;
    if ((v1 = (int*)realloc(v, nr * sizeof(int))) == NULL)
    {
        printf("Memorie insuficienta");
        free(v);
        exit(EXIT_FAILURE);
    }
    v = v1;
    v[nr - 1] = elem;
}
int main()
{
int a[100][100];
int n, i, j, nr = 0;
printf("Number de elements n:");
scanf("%d", &n);
for (i = 0; i < n; i  )
    {
    for (j = 0; j < n; j  )
        {
            scanf("%d", &a[i][j]);
              hash[a[i][j]];
        }
    }   

for (i = 0; i < n; i  )
    {
    for (j = 0; j < n; j  )           
        {
                if(hash[a[i][j]]==1) 
                {
                    nr  ;
                    alocare_vector(nr, a[i][j]);
                }
        }
    }
    if (nr > 0)
    {
        printf("Elements:");
        for (i = 0; i < nr; i  )
        {
            printf("%d ", v[i]);
        }
    }
    free(v);
}

CodePudding user response:

When calling a your funtion you pass a copy of the pointer. Therfore any modifications to this local copy of the pointer will not affect the copy in main. Therfore you need a refrence or pointer to the pointer in main.

void alocare_vector(int **v, int nr, int elem)
{
    ...
    *v = v1;
}

CodePudding user response:

Here you have brut force example how to generate array of distinct elements of the 2D array:

typedef struct
{
    size_t length;
    int data[];
}data_t;


data_t *add_element(data_t *arr, int element)
{
    size_t newsize = arr ? arr -> length : 0;
    arr = realloc(arr, newsize * sizeof(arr -> data[0])   sizeof(*arr));
    
    if(arr)
    {
        arr -> length = newsize;
        arr -> data[newsize - 1] = element;
    }
    return arr;
}

int isPresent(size_t rows, size_t cols, size_t vrow, size_t vcol, int (*array)[cols])
{
    int val = array[vrow][vcol];
    for(size_t row = 0; row < rows; row  )
        for(size_t col = 0; col < cols; col  )
        {
            if(vcol == col && vrow == row) continue;
            if(array[row][col] == val)
            {
                return 1;               
            }
        }
    return 0;
}

data_t *distinctElements(size_t rows, size_t cols, int (*array)[cols])
{
    data_t *dist = NULL;
    for(size_t row = 0; row < rows; row  )
    {
        for(size_t col = 0; col < cols; col  )
        {
            if(!isPresent(rows, cols, row, col, array))
            {
                data_t *tmp = add_element(dist, array[row][col]);
                if(tmp) dist = tmp;
                else { /* error handling */}
            }
        }
    }
    return dist;
}
  • Related