fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. int n;
  8. int m;
  9. int i;
  10. int k;
  11. printf("Enter the size of array A: ");
  12. scanf("%d",&n);
  13. int arrA[n];
  14. printf("Enter the element(s) of array A: ");
  15. for(i=0;i<n;i++)
  16. {
  17. scanf("%d",&arrA[i]);
  18. }
  19. for(i=0; i<n; i++)
  20. {
  21. printf("%d",arrA[i]);
  22. }
  23. printf("\n");
  24.  
  25. printf("Enter the size of array B: ");
  26. scanf("%d",&m);
  27. int arrB[m];
  28. printf("Enter the element(s) of array B: ");
  29. for(i=0;i<m;i++)
  30. {
  31. scanf("%d",&arrB[i]);
  32. }
  33. for(i=0; i<m; i++)
  34. {
  35. printf("%d",arrB[i]);
  36. }
  37. printf("\n");
  38. k = arr_union(arrA, arrB, n, m);
  39. printf("%d\n",k);
  40. return 0;
  41. }
  42.  
  43.  
  44. int arr_union(int arrA[], int arrB[], int m, int n)
  45. {
  46. int i = 0;
  47. int j = 0;
  48. int k = 0;
  49. int l = 0;
  50. if(n > m)
  51. {
  52. n = l;
  53. }
  54. else
  55. {
  56. m = l;
  57. }
  58. int arrC[l];
  59. while ((i < n) && (j < m))
  60. {
  61. if (arrA[i] < arrB[j])
  62. {
  63. arrC[k] = arrA[i];
  64. i++;
  65. k++;
  66. }
  67. else if (arrA[i] > arrB[j])
  68. {
  69. arrC[k] = arrB[j];
  70. j++;
  71. k++;
  72. }
  73. else
  74. {
  75. arrC[k] = arrA[i];
  76. i++;
  77. j++;
  78. k++;
  79. }
  80. }
  81. if (i == n)
  82. {
  83. while (j < m)
  84. {
  85. arrC[k] = arrB[j];
  86. j++;
  87. k++;
  88. }
  89. }
  90. else
  91. {
  92. while (i < n)
  93. {
  94. arrC[k] = arrA[i];
  95. i++;
  96. k++;
  97. }
  98. }
  99. return(k);
  100. }
  101.  
Success #stdin #stdout 0s 2056KB
stdin
4
1
2
3
4
4
6
7
8
9
stdout
Enter the size of array A: Enter the element(s) of array A: 1234
Enter the size of array B: Enter the element(s) of array B: 6789
0