Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. bool exists(int *array, size_t array_length, int value);
  5. size_t count_common(int *array_a, size_t size_a, int *array_b, size_t size_b);
  6. int* differences(int *array_a, size_t size_a, int *array_b, size_t size_b);
  7.  
  8. bool multiple(int *array, size_t array_length, int value);
  9. size_t count_multiples(int *array_a, size_t size_a, int *array_b, size_t size_b);
  10. int* multiples(int *array_a, size_t size_a, int *array_b, size_t size_b);
  11.  
  12. int main()
  13. {
  14.     size_t size_a, size_b;
  15.     printf("Insert the length of the two arrays: ");
  16.     scanf("%d%d", &size_a, &size_b);
  17.  
  18.     int *array_a = malloc(sizeof(int) * size_a);
  19.     int *array_b = malloc(sizeof(int) * size_b);
  20.  
  21.     // Filling A
  22.     printf("Filling A: \n");
  23.     for(size_t i = 0; i < size_a; i++) {
  24.         scanf("%d", &array_a[i]);
  25.     }
  26.  
  27.     // Filling B
  28.     printf("Filling B: \n");
  29.     for(size_t i = 0; i < size_b; i++) {
  30.         scanf("%d", &array_b[i]);
  31.     }
  32.  
  33.     int elements_found = count_common(array_a, size_a, array_b, size_b);
  34.     int *array_found = differences(array_a, size_a, array_b, size_b);
  35.  
  36.     // Prints the result
  37.     printf("Printing b in B but not in A: \n");
  38.     for(size_t i = 0; i < elements_found; i++) {
  39.         printf("%d ", array_found[i]);
  40.     }
  41.  
  42.     int multiples_found = count_multiples(array_a, size_a, array_b, size_b);
  43.     int *array_multiples = multiples(array_a, size_a, array_b, size_b);
  44.  
  45.     // Prints the result
  46.     printf("\nPrinting b MOD a for each element of B and A: \n");
  47.     for(size_t i = 0; i < multiples_found; i++) {
  48.         printf("%d ", array_multiples[i]);
  49.     }
  50.     return 0;
  51. }
  52.  
  53. bool exists(int *array, size_t array_length, int value) {
  54.     for(size_t i = 0; i < array_length; i++) {
  55.         if(array[i] == value)
  56.             return true;
  57.     }
  58.     return false;
  59. }
  60.  
  61. size_t count_common(int *array_a, size_t size_a, int *array_b, size_t size_b) {
  62.     size_t elements_found = 0;
  63.     for(size_t i = 0; i < size_b; i++) {
  64.         if(!exists(array_a, size_a, array_b[i]))
  65.             elements_found++;
  66.     }
  67.  
  68.     return elements_found;
  69. }
  70.  
  71. int* differences(int *array_a, size_t size_a, int *array_b, size_t size_b) {
  72.     // Count the amount of differente values between A and B
  73.     int elements_found = count_common(array_a, size_a, array_b, size_b);
  74.  
  75.     // array_found of the size of the elements found
  76.     int *array_found = malloc(sizeof(int) * elements_found);
  77.     elements_found = 0;
  78.     for(size_t i = 0; i < size_b; i++) {
  79.         if(!exists(array_a, size_a, array_b[i]))
  80.             array_found[elements_found++] = array_b[i];
  81.     }
  82.  
  83.     return array_found;
  84. }
  85.  
  86.  
  87. bool multiple(int *array, size_t array_length, int value) {
  88.     for(size_t i = 0; i < array_length; i++) {
  89.         if(value % array[i] == 0)
  90.             return true;
  91.     }
  92.     return false;
  93. }
  94.  
  95. size_t count_multiples(int *array_a, size_t size_a, int *array_b, size_t size_b) {
  96.     size_t elements_found = 0;
  97.     for(size_t i = 0; i < size_b; i++) {
  98.         if(multiple(array_a, size_a, array_b[i]))
  99.             elements_found++;
  100.     }
  101.  
  102.     return elements_found;
  103. }
  104.  
  105. int* multiples(int *array_a, size_t size_a, int *array_b, size_t size_b) {
  106.     // Count the amount of differente values between A and B
  107.     int elements_found = count_multiples(array_a, size_a, array_b, size_b);
  108.  
  109.     // array_found of the size of the elements found
  110.     int *array_found = malloc(sizeof(int) * elements_found);
  111.     elements_found = 0;
  112.     for(size_t i = 0; i < size_b; i++) {
  113.         if(multiple(array_a, size_a, array_b[i]))
  114.             array_found[elements_found++] = array_b[i];
  115.     }
  116.  
  117.     return array_found;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement