SHARE
TWEET

Untitled

razvanth21 Jan 15th, 2017 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     int *v, *aux, k = 1, x, i, j, c;
  7.     FILE *f;
  8.  
  9.     v = (int *) malloc(sizeof(int));
  10.     if (!v)
  11.     {
  12.         printf("Nu pot aloca memorie!");
  13.         exit(EXIT_FAILURE);
  14.     }
  15.  
  16.     f = fopen("test.txt", "r");
  17.  
  18.     if (f != NULL)
  19.     {
  20.         while (fscanf(f, "%d", &x) == 1)
  21.         {
  22.             aux = (int *) realloc(v, (k + 1) * sizeof(int));
  23.  
  24.             if (!aux)
  25.             {
  26.                 free(v);
  27.                 printf("Nu pot redimensiona blocul!");
  28.                 exit(EXIT_FAILURE);
  29.             }
  30.             else
  31.                 v = aux;
  32.  
  33.             v[k - 1] = x;
  34.             k ++;
  35.         }
  36.     }
  37.  
  38.     x = -1;
  39.     for (i = 0; i < k-1; i++)
  40.     {
  41.         c = 1;
  42.  
  43.         for (j = i+1; j < k; j++)
  44.         {
  45.             if (v[i] == v[j])
  46.                 c ++;
  47.         }
  48.  
  49.         if(x != v[i])
  50.             printf("%d %d\n", v[i], c);
  51.  
  52.         x = v[i];
  53.     }
  54.  
  55.     free(v);
  56.     return 0x0;
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top