Объединение и пересечение двух массивов - C (СИ)

Узнай цену своей работы

Формулировка задачи:

доброго времени суток, нужна помощь со следующим кодом. void BuildGroup(int** group, int* count) функция получающая от пользователя размер группы, создающая динамическую группу, и получающая данные( если даные повторяються то она попросит занести их снова). и в конце возвращает группу. int* Union(int* group1, int size1, int* group2, int size2, int* sizeUnion) получает две группы, и возвращает ихнее обьеденение. int* Intersection(int* group1, int size1,int* group2,int size2, int* sizeInter) получает вде группы и возвращает из пересечение. заранее спасибо.

Решение задачи: «Объединение и пересечение двух массивов»

textual
Листинг программы
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. void BuildGroup(int** group, int* count) {/*func to buld a array*/
  6.     int size, flag = 0, *arr = NULL,i,j;/*a size of array,the array to check a equel parameters, flag is for true or false*/
  7.     scanf("%d", &size);
  8.     while (flag != 1) {/*if we have true, exit*/
  9.         if (size == 0) {/*the Empty set*/
  10.             printf("Empty Set\n");/*it will be show with 0*/
  11.             flag = 1;/*need to exit from the loop*/
  12.             *group = (int*)calloc(flag, sizeof(int));/*dinamic memory management*/
  13.             if (!*group) {/*check if dinamic memory managment is falce*/
  14.                 printf("ERROR!Not Enough memory!\n");
  15.                 exit(1);/*exit from the program*/
  16.             }
  17.         }
  18.         else {
  19.             *group = (int*)calloc(size, sizeof(int));/*dinamic memory management*/
  20.             if (!*group) {/*check if dinamic memory managment is falce*/
  21.                 printf("ERROR!Not Enough memory!\n");
  22.                 exit(1);/*exit from the program*/
  23.             }
  24.             arr = (int*)calloc(size, sizeof(int));/*temp array*/
  25.             if (!arr) {/*check it*/
  26.                 printf("ERRPR!Not Enough memonty!\n");
  27.                 exit(1);
  28.             }
  29.             printf("Enter a group: ");
  30.             flag = 1;
  31.             for (i = 0; i < size; i++)
  32.             {
  33.                 scanf("%d", &(*group)[i]);/*enter a array*/
  34.                 arr[i] = (*group)[i];/*for the check a equel parameters*/
  35.                 for (j = 0; j < i; j++)/*loop to check a equel parameters*/
  36.                 {
  37.                     if (i == 0) {/*because we it's first number on the array*/
  38.                         break;
  39.                     }/*exit from the loop*/
  40.                     if ((*group)[i] == arr[j]) {/*if parameters are equel*/
  41.                         printf("Wrong Input,Try Again\n");/*the user enter a wrong input*/
  42.                         flag = 0;/*so flag is flase*/
  43.                         free(arr);/*delete a array*/
  44.                         free(*group);/*delete a array*/
  45.                         break;/*exit from the loop*/
  46.                     }
  47.                     else
  48.                         flag = 1;/*if input is corrent, so flag is true*/
  49.                 }
  50.                 if (flag == 0) {
  51.                     getchar(stdin);
  52.                     break;/*exit*/
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     free(arr);/*delete temp array*/
  58.     *count = size;/*the size of the new array*/
  59. }
  60.  
  61. int* Union(int* group1, int size1, int* group2, int size2, int* sizeUnion) {
  62.     int count=size1+size2,j=0,i=0,k=0,flag=0,*UnionArr=NULL,size;
  63.     if (size1 == 0) {/*if we have a Empty set*/
  64.         *sizeUnion = size2;/*A union 0 = A*/
  65.         return group2;
  66.     }
  67.     else if (size2 == 0) {
  68.         *sizeUnion = size2;/*B union 0 = B*/
  69.         return group1;
  70.         }
  71.     else {
  72.         size = (size1 >= size2) ? size1 : size2;/*if size1>size2,size = size1,else size=size2*/
  73.         for (i = 0; i < size; i++)/*loop to check a equel parameters*/
  74.         {
  75.             if (size == size1) {/*if the bigest array is group1*/
  76.                 for (j = i - 1; j < size2; j++)/*check a group2*/
  77.                 {
  78.                     if (group1[i] != group2[j])/*if parameters are different, up the j*/
  79.                         continue;
  80.                     else
  81.                         count--;/*esle down a sum of number of parameters of two groups*/
  82.                 }
  83.             }
  84.             else if (size == size2) {/*if the bigest array is group2*/
  85.                 for (j = i - 1; j < size2; j++)
  86.                 {
  87.                     if (group2[i] != group1[j])
  88.                         continue;
  89.                     else
  90.                         count--;
  91.                 }
  92.             }
  93.         }
  94.         UnionArr = (int*)calloc(count, sizeof(int));/*build union array*/
  95.         flag = 0;/*true false parameter*/
  96.         if (size == size1) {/*if bigest array is group1*/
  97.             for (i = 0; i < size1;i++)/*loop to add parameters for union array*/
  98.             {
  99.                 UnionArr[i] = group1[i];/*coppy a bigest group to Union*/
  100.             }
  101.             for (j = 0; j < size2; j++)/*check a parameters of smallest group*/
  102.             {
  103.                 for (k = j - 1; k < size1; k++)/*check a parameters of Union*/
  104.                 {
  105.                     if (group2[j] == UnionArr[k]) {/*if the parameters are equel, flag is false, and exit from the loop*/
  106.                         flag = 0;
  107.                         break;
  108.                     }
  109.                     else
  110.                         flag = 1;
  111.                 }
  112.                 if (flag == 1) {
  113.                     UnionArr[i] = group2[j];
  114.                     i++;
  115.                 }
  116.             }
  117.         }
  118.         else if (size == size2) {/*if bigest array is group1*/
  119.             for (i = 0; i < size2;i++)/*loop to add parameters for union array*/
  120.             {
  121.                 UnionArr[i] = group2[i];/*coppy a bigest group to Union*/
  122.             }
  123.             for (j = 0; j < size1; j++)/*check a parameters of smallest group*/
  124.             {
  125.                 for (k = j - 1; k < size2; k++)/*check a parameters of Union*/
  126.                 {
  127.                     if (group1[j] == UnionArr[k]) {/*if the parameters are equel, flag is false, and exit from the loop*/
  128.                         flag = 0;
  129.                         break;
  130.                     }
  131.                     else
  132.                         flag = 1;
  133.                 }
  134.                 if (flag == 1) {
  135.                     UnionArr[i] = group1[j];
  136.                     i++;
  137.                 }
  138.             }
  139.         }
  140.         *sizeUnion = count;
  141.         return UnionArr;
  142.     }
  143. }
  144.  
  145. int* Intersection(int* group1, int size1, int* group2, int size2, int* sizeInter) {
  146.     int size,count=0,*InterArr = NULL, i, j,k=0,flag=0;
  147.     if (size1 == 0) {/*if we have a empty set*/
  148.         *sizeInter = size1+1;/*to output a 0*/
  149.         return group1;/*A inter 0 = 0*/
  150.     }
  151.     else if (size2 == 0) {
  152.         *sizeInter = size2+1;
  153.         return group2;/*B inter 0 = 0*/
  154.     }
  155.     else {
  156.         size = (size1 >= size2) ? size1 : size2;/*if size1>size2,size = size1,else size=size2*/
  157.         if (size == size1) {/*the bigest size*/
  158.             for (i = 0; i < size; i++)
  159.             {
  160.                 for (j = 0; j < size2; j++)
  161.                 {
  162.                     if (group1[i] == group2[j])/*if the parameters are equel*/
  163.                         count++;/*up*/
  164.                 }
  165.             }
  166.         }
  167.         else if (size == size2) {/*the bigest size*/
  168.             for (i = 0; i < size; i++)
  169.             {
  170.                 for (j = 0; j < size1; j++)
  171.                 {
  172.                     if (group2[i] == group1[j])
  173.                         count++;
  174.                 }
  175.             }
  176.         }
  177.         InterArr = (int*)calloc(count, sizeof(int));/*build new array*/
  178.         if (size == size1) {
  179.             for (i = 0; i < size2; i++)
  180.             {
  181.                 for (j = 0; j < size1; j++)
  182.                 {
  183.                     if (group1[j] == group2[i]) {
  184.                         InterArr[k] = group1[j];
  185.                         k++;
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.         else if (size == size2) {
  191.             for (i = 0; i < size1; i++)
  192.             {
  193.                 for (j = 0; j < size2; j++)
  194.                 {
  195.                     if (group2[j] == group1[i]) {
  196.                         InterArr[k] = group2[j];
  197.                         k++;
  198.                     }
  199.                 }
  200.             }
  201.         }
  202.         *sizeInter = count;
  203.         return InterArr;
  204.     }
  205. }
  206.  
  207. int main() {
  208.     int *group1 = NULL,*group2=NULL, size1, size2, sizeUnion,*UnionArr=NULL,*InterArr=NULL,sizeInter;
  209.     printf("Enter a size of first group: ");
  210.     BuildGroup(&group1, &size1);
  211.     printf("Enter a size of second group: ");
  212.     BuildGroup(&group2, &size2);
  213.     UnionArr = Union(group1, size1, group2, size2, &sizeUnion);
  214.     printf("The Union of the groups is:{ ");
  215.     for (int i = 0; i < sizeUnion; i++)
  216.     {
  217.         printf("%d ", UnionArr[i]);
  218.     }
  219.     printf("}\n");
  220.     InterArr=Intersection(group1,size1,group2,size2,&sizeInter);
  221.     printf("The Inetsection of the groups is:{ ");
  222.     for (int i = 0; i < sizeInter; i++)
  223.     {
  224.         printf("%d ", InterArr[i]);
  225.     }
  226.     printf("}\n");
  227.     return 0;
  228. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.364 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы