Ghislain_Mike

Equivalent Arrays

Mar 8th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int equivalentArray(int a[], int b[], int size_a, int size_b) {
  4.     int result = 0, i, j, count_matches, count_missmatch = 0;
  5.  
  6.     int matching_numbers[size_a];
  7.  
  8.     for (i = 0, count_matches = 0; i < size_a; i++) { // check all values in the array 1
  9.  
  10.         for (j = 0; j < size_b; j++) { // compare the current value with all values inside array 2
  11.  
  12.             if (a[i] == b[j]) {
  13.                 count_matches++;
  14.                 matching_numbers[count_matches] = a[i];
  15.             } else {
  16.                 count_missmatch++;
  17.             }
  18.  
  19.         }
  20.  
  21.     }
  22.  
  23.     if (count_missmatch > 0 && (count_matches == size_a || count_matches == size_b)) {
  24.         result = 1;
  25.     }
  26.  
  27.     return result;
  28. }
  29.  
  30. int main() {
  31.     int i, size_1, size_2, comparison;
  32.  
  33.     printf("Enter the size of your 1st array: ");
  34.     scanf("%d", &size_1);
  35.  
  36.     int array_1[size_1];
  37.  
  38.     printf("\nEnter array values\n");
  39.     for (i = 0; i < size_1; i++) {
  40.         printf("%d. array element: ", i+1);
  41.         scanf("%d", &array_1[i]);
  42.     }
  43.  
  44.     // enter the second array
  45.  
  46.     printf("\n\nEnter the size of your 2nd array: ");
  47.     scanf("%d", &size_2);
  48.  
  49.     int array_2[size_2];
  50.  
  51.     printf("\nEnter array values\n");
  52.     for (i = 0; i < size_2; i++) {
  53.         printf("%d. array element: ", i+1);
  54.         scanf("%d", &array_2[i]);
  55.     }
  56.  
  57.     // compare the two arrays
  58.     comparison = equivalentArray(array_1, array_2, size_1, size_2);
  59.     printf("\nThe returned value = %d", comparison);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment