Advertisement
boyan16-z

Removing zeros + positive and negative arrays

Mar 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 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, *b;
  10. int i, n, j = 0, s = 0;
  11.  
  12. printf("Enter the size : ");
  13. scanf_s("%d", &n);
  14. a = (int*)malloc(n * sizeof(int));
  15.  
  16. for (i = 0; i < n; i++)
  17. {
  18. printf("a[%d] = ", i);
  19. scanf_s("%d", &a[i]);
  20. }
  21.  
  22. for (i = 0; i < n; i++)
  23. {
  24. if (a[i] != 0) {
  25. a[j] = a[i];
  26. j++;
  27. }
  28. }
  29. a = (int*)realloc(a, j * sizeof(int));
  30.  
  31. b = (int*)malloc(j * sizeof(int));
  32. for (i = 0; i < j; i++) {
  33. if (a[i] < 0) {
  34. b[s] = a[i];
  35. s++;
  36. }
  37. }
  38. b = (int*)realloc(b, s * sizeof(int));
  39.  
  40. n = 0;
  41. for (i = 0; i < j; i++){
  42. if (a[i] > 0){
  43. a[n] = a[i];
  44. n++;
  45. }
  46. }
  47. a = (int*)realloc(a, n * sizeof(int));
  48.  
  49. printf("Положительные\n");
  50. for (i = 0; i < n; i++){
  51. printf("a[%d] = %d ", i, a[i]);
  52. }
  53. printf("\nОтрицательные\n");
  54. for (i = 0; i < s; i++){
  55. printf("b[%d] = %d ", i, b[i]);
  56. }
  57. free(a);
  58. free(b);
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement