p11

Write a C program that will take the elements of two integer arrays of 5 element each, and insert the common elements of both the array into a third array (Set intersection)

  1. /*
  2.  * C Program to Find Union & Intersection of 2 Arrays
  3.  */
  4. #include <stdio.h>
  5. #define SIZE 5
  6.  
  7. void get_value(int arr[]);
  8. void print_value(int arr[], int n);
  9. void function_sort(int arr[]);
  10. int find_intersection(int array1[], int array2[], int intersection_array[]);
  11. int find_union(int array1[], int array2[], int union_array[]);
  12.  
  13. void main()
  14. {
  15.     int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
  16.     int num_elements;
  17.  
  18.     //input elements of Array1
  19.     printf("\n Enter the elements of Array 1: n");
  20.     get_value(array1);
  21.     printf("\n\n Elements of Array 1: ");
  22.     print_value(array1, SIZE);
  23.  
  24.     //Sort array 1
  25.     function_sort(array1);
  26.     printf("nnSorted elements of Array 1: ");
  27.     print_value(array1, SIZE);
  28.  
  29.     //input elements of Array2
  30.     printf("nnEnter the elements of Array 2: n");
  31.     get_value(array2);
  32.     printf("\n\n Elements of Array 2: ");
  33.     print_value(array2, SIZE);
  34.  
  35.     //Sort array 2
  36.     function_sort(array2);
  37.     printf("\n\nSorted elements of Array 2: ");
  38.     print_value(array2, SIZE);
  39.  
  40.     //Find Intersection
  41.     num_elements = find_intersection(array1, array2, intersection_array);
  42.     printf("\n\n Intersection is: ");
  43.     print_value(intersection_array, num_elements);
  44.  
  45.     //Find Union
  46.     num_elements = find_union(array1, array2, union_array);
  47.     printf("\n\n Union is: ");
  48.     print_value(union_array, num_elements);
  49. }
  50.  
  51. void get_value(int arr[])
  52. {
  53.     int i, j;
  54.     for (i = 0; i < SIZE; i++)
  55.     {
  56.         j = i + 1;
  57.         printf("\n Enter element %d: ", j);
  58.         scanf("%d", &arr[i]);
  59.     }
  60. }
  61.  
  62. void print_value(int arr[], int n)
  63. {
  64.     int i;
  65.     printf("{ ");
  66.     for (i = 0; i < n; i++)
  67.     {
  68.         printf("%d ", arr[i]);
  69.     }
  70.     printf("}");
  71. }
  72.  
  73. void function_sort(int arr[])
  74. {
  75.     int i, j, temp, swapping;
  76.  
  77.     for (i = 1; i < size; i++)
  78.     {
  79.         swapping = 0;
  80.         for (j = 0; j < size-i; j++)
  81.         {
  82.             if (arr[j] > arr[j+1])
  83.             {
  84.                 temp = arr[j];
  85.                 arr[j] = arr[j + 1];
  86.                 arr[j + 1] = temp;
  87.                 swapping = 1;
  88.             }
  89.         }
  90.         if (swapping == 0)
  91.         {
  92.             break;
  93.         }
  94.     }
  95. }
  96.  
  97. int find_intersection(int array1[], int array2[], int intersection_array[])
  98. {
  99.     int i = 0, j = 0, k = 0;
  100.     while ((i < size) && (j < size))
  101.     {
  102.         if (array1[i] < array2[j])
  103.         {
  104.             i++;
  105.         }
  106.         else if (array1[i] > array2[j])
  107.         {
  108.             j++;
  109.         }
  110.         else
  111.         {
  112.             intersection_array[k] = array1[i];
  113.             i++;
  114.             j++;
  115.             k++;
  116.         }
  117.     }
  118.     return(k);
  119. }
  120.  
  121. int find_union(int array1[], int array2[], int union_array[])
  122. {
  123.     int i = 0, j = 0, k = 0;
  124.     while ((i < SIZE) && (j < SIZE))
  125.     {
  126.         if (array1[i] < array2[j])
  127.         {
  128.             union_array[k] = array1[i];
  129.             i++;
  130.             k++;
  131.         }
  132.         else if (array1[i] > array2[j])
  133.         {
  134.             union_array[k] = array2[j];
  135.             j++;
  136.             k++;
  137.         }
  138.         else
  139.         {
  140.             union_array[k] = array1[i];
  141.             i++;
  142.             j++;
  143.             k++;
  144.         }
  145.     }
  146.     if (i == SIZE)
  147.     {
  148.         while (j < SIZE)
  149.         {
  150.             union_array[k] = array2[j];
  151.             j++;
  152.             k++;
  153.         }
  154.     }
  155.     else
  156.     {
  157.         while (i < SIZE)
  158.         {
  159.             union_array[k] = array1[i];
  160.             i++;
  161.             k++;
  162.         }
  163.     }
  164.     return(k);
  165. }
$ cc pgm98.c
$ a.out
 
Enter the elements of Array 1:
 
Enter element 1: 12
 
Enter element 2: 34
 
Enter element 3: 23
 
Enter element 4: 56
 
Enter element 5: 45
 
 
Elements of Array 1: { 12 34 23 56 45 }
 
Sorted elements of Array 1: { 12 23 34 45 56 }
 
Enter the elements of Array 2:
 
Enter element 1: 34
 
Enter element 2: 56
 
Enter element 3: 12
 
Enter element 4: 78
 
Enter element 5: 66
 
 
Elements of Array 2: { 34 56 12 78 66 }
 
Sorted elements of Array 2: { 12 34 56 66 78 }
 
Intersection is: { 12 34 56 }
 
Union is: { 12 23 34 45 56 66 78 }

No comments:

Post a Comment