Advertisement
davide1409

17-06-2020.c

Jan 19th, 2021
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. int check_elem(int elem, int *v, int dim){
  4.     // return 1 iff elem in v; 0 altrimenti
  5.     for(int i = 0; i!=dim; i++){
  6.         if(v[i] == elem){
  7.             return 1;
  8.         }
  9.     }
  10.    
  11.     return 0;
  12. }
  13.  
  14.  
  15. int *noRipetizioni(int **A, int nr, int nc){
  16.     int dim = nr*nc;
  17.     int *V = malloc(sizeof(*v) * dim);
  18.     size_t num_elem = 0;
  19.     if(!V){
  20.         return NULL;
  21.     }
  22.  
  23.  
  24.     // inizializzo il vettore
  25.     for(int i = 0; i!=dim; i++){
  26.         V[i] = 0;
  27.     }
  28.  
  29.     for(int j = 0; j!=nc; j++){
  30.         for(int i = 0; i!=nr; i++){
  31.             if( num_elem && !check_elem(A[i][j], V, dim) ){
  32.                 V[i] = A[i][j];
  33.                 num_elem++;
  34.             }
  35.         }
  36.     }
  37.  
  38.     if(dim != num_elem){
  39.         V = realloc(V, sizeof(*V) * num_elem);
  40.     }
  41.    
  42.     if(!V){
  43.         return NULL;
  44.     }  
  45.  
  46.     return V;
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement