ibrahim_065

Insert some random numbers into an array so that each number appears in the array at most once.

Oct 10th, 2020 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. /*
  2. 4. Insert some random numbers into an array so that each number appears in the array at most once.
  3. */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<time.h>
  7. int main()
  8. {
  9.     int n, j = 0, i;
  10.     // array size
  11.     scanf("%d", &n);
  12.     int ar[n], cn_ar[1001] = {0};
  13.     srand(time(NULL));
  14.     // INSERT RANDOM NUMBER and count distinct element
  15.     for (i = 0; i < n; ++i)
  16.     {
  17.         int k;
  18.         k = rand()%10;
  19.         printf("%d ", k);
  20.         if (cn_ar[k] == 0)
  21.         {
  22.             ar[j++] = k;
  23.             cn_ar[k]++;
  24.         }
  25.     }
  26.     printf("\n");
  27.     // print  array
  28.     for (i = 0; i < j; ++i)
  29.         printf("%d ", ar[i]);
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment