Advertisement
boyan16-z

Deletion of zeros

Mar 21st, 2018
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. int main()
  8. {
  9. int *a;
  10. int i, n, j = 0;
  11.  
  12. printf("Enter the size : ");
  13. scanf_s("%d",&n);
  14.  
  15. a = (int*) malloc (n * sizeof(int));
  16.  
  17. for (i = 0; i < n; i++)
  18. {
  19. printf("a[%d] = ", i);
  20. scanf_s("%d", &a[i]);
  21. }
  22.  
  23. for (i = 0; i < n; i++)
  24. {
  25. if (a[i] != 0) {
  26. a[j] = a[i];
  27. j++;
  28. }
  29. }
  30.  
  31. a = (int*) realloc(a, j * sizeof(int));
  32.  
  33. for (i = 0; i < j; i++) {
  34. printf("a[%d] = %d\n", i, a[i]);
  35. }
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement