Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 4. Insert some random numbers into an array so that each number appears in the array at most once.
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- int main()
- {
- int n, j = 0, i;
- // array size
- scanf("%d", &n);
- int ar[n], cn_ar[1001] = {0};
- srand(time(NULL));
- // INSERT RANDOM NUMBER and count distinct element
- for (i = 0; i < n; ++i)
- {
- int k;
- k = rand()%10;
- printf("%d ", k);
- if (cn_ar[k] == 0)
- {
- ar[j++] = k;
- cn_ar[k]++;
- }
- }
- printf("\n");
- // print array
- for (i = 0; i < j; ++i)
- printf("%d ", ar[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment