Объединение и пересечение двух массивов - 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
Листинг программы
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void BuildGroup(int** group, int* count) {/*func to buld a array*/
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*/
scanf("%d", &size);
while (flag != 1) {/*if we have true, exit*/
if (size == 0) {/*the Empty set*/
printf("Empty Set\n");/*it will be show with 0*/
flag = 1;/*need to exit from the loop*/
*group = (int*)calloc(flag, sizeof(int));/*dinamic memory management*/
if (!*group) {/*check if dinamic memory managment is falce*/
printf("ERROR!Not Enough memory!\n");
exit(1);/*exit from the program*/
}
}
else {
*group = (int*)calloc(size, sizeof(int));/*dinamic memory management*/
if (!*group) {/*check if dinamic memory managment is falce*/
printf("ERROR!Not Enough memory!\n");
exit(1);/*exit from the program*/
}
arr = (int*)calloc(size, sizeof(int));/*temp array*/
if (!arr) {/*check it*/
printf("ERRPR!Not Enough memonty!\n");
exit(1);
}
printf("Enter a group: ");
flag = 1;
for (i = 0; i < size; i++)
{
scanf("%d", &(*group)[i]);/*enter a array*/
arr[i] = (*group)[i];/*for the check a equel parameters*/
for (j = 0; j < i; j++)/*loop to check a equel parameters*/
{
if (i == 0) {/*because we it's first number on the array*/
break;
}/*exit from the loop*/
if ((*group)[i] == arr[j]) {/*if parameters are equel*/
printf("Wrong Input,Try Again\n");/*the user enter a wrong input*/
flag = 0;/*so flag is flase*/
free(arr);/*delete a array*/
free(*group);/*delete a array*/
break;/*exit from the loop*/
}
else
flag = 1;/*if input is corrent, so flag is true*/
}
if (flag == 0) {
getchar(stdin);
break;/*exit*/
}
}
}
}
free(arr);/*delete temp array*/
*count = size;/*the size of the new array*/
}
int* Union(int* group1, int size1, int* group2, int size2, int* sizeUnion) {
int count=size1+size2,j=0,i=0,k=0,flag=0,*UnionArr=NULL,size;
if (size1 == 0) {/*if we have a Empty set*/
*sizeUnion = size2;/*A union 0 = A*/
return group2;
}
else if (size2 == 0) {
*sizeUnion = size2;/*B union 0 = B*/
return group1;
}
else {
size = (size1 >= size2) ? size1 : size2;/*if size1>size2,size = size1,else size=size2*/
for (i = 0; i < size; i++)/*loop to check a equel parameters*/
{
if (size == size1) {/*if the bigest array is group1*/
for (j = i - 1; j < size2; j++)/*check a group2*/
{
if (group1[i] != group2[j])/*if parameters are different, up the j*/
continue;
else
count--;/*esle down a sum of number of parameters of two groups*/
}
}
else if (size == size2) {/*if the bigest array is group2*/
for (j = i - 1; j < size2; j++)
{
if (group2[i] != group1[j])
continue;
else
count--;
}
}
}
UnionArr = (int*)calloc(count, sizeof(int));/*build union array*/
flag = 0;/*true false parameter*/
if (size == size1) {/*if bigest array is group1*/
for (i = 0; i < size1;i++)/*loop to add parameters for union array*/
{
UnionArr[i] = group1[i];/*coppy a bigest group to Union*/
}
for (j = 0; j < size2; j++)/*check a parameters of smallest group*/
{
for (k = j - 1; k < size1; k++)/*check a parameters of Union*/
{
if (group2[j] == UnionArr[k]) {/*if the parameters are equel, flag is false, and exit from the loop*/
flag = 0;
break;
}
else
flag = 1;
}
if (flag == 1) {
UnionArr[i] = group2[j];
i++;
}
}
}
else if (size == size2) {/*if bigest array is group1*/
for (i = 0; i < size2;i++)/*loop to add parameters for union array*/
{
UnionArr[i] = group2[i];/*coppy a bigest group to Union*/
}
for (j = 0; j < size1; j++)/*check a parameters of smallest group*/
{
for (k = j - 1; k < size2; k++)/*check a parameters of Union*/
{
if (group1[j] == UnionArr[k]) {/*if the parameters are equel, flag is false, and exit from the loop*/
flag = 0;
break;
}
else
flag = 1;
}
if (flag == 1) {
UnionArr[i] = group1[j];
i++;
}
}
}
*sizeUnion = count;
return UnionArr;
}
}
int* Intersection(int* group1, int size1, int* group2, int size2, int* sizeInter) {
int size,count=0,*InterArr = NULL, i, j,k=0,flag=0;
if (size1 == 0) {/*if we have a empty set*/
*sizeInter = size1+1;/*to output a 0*/
return group1;/*A inter 0 = 0*/
}
else if (size2 == 0) {
*sizeInter = size2+1;
return group2;/*B inter 0 = 0*/
}
else {
size = (size1 >= size2) ? size1 : size2;/*if size1>size2,size = size1,else size=size2*/
if (size == size1) {/*the bigest size*/
for (i = 0; i < size; i++)
{
for (j = 0; j < size2; j++)
{
if (group1[i] == group2[j])/*if the parameters are equel*/
count++;/*up*/
}
}
}
else if (size == size2) {/*the bigest size*/
for (i = 0; i < size; i++)
{
for (j = 0; j < size1; j++)
{
if (group2[i] == group1[j])
count++;
}
}
}
InterArr = (int*)calloc(count, sizeof(int));/*build new array*/
if (size == size1) {
for (i = 0; i < size2; i++)
{
for (j = 0; j < size1; j++)
{
if (group1[j] == group2[i]) {
InterArr[k] = group1[j];
k++;
}
}
}
}
else if (size == size2) {
for (i = 0; i < size1; i++)
{
for (j = 0; j < size2; j++)
{
if (group2[j] == group1[i]) {
InterArr[k] = group2[j];
k++;
}
}
}
}
*sizeInter = count;
return InterArr;
}
}
int main() {
int *group1 = NULL,*group2=NULL, size1, size2, sizeUnion,*UnionArr=NULL,*InterArr=NULL,sizeInter;
printf("Enter a size of first group: ");
BuildGroup(&group1, &size1);
printf("Enter a size of second group: ");
BuildGroup(&group2, &size2);
UnionArr = Union(group1, size1, group2, size2, &sizeUnion);
printf("The Union of the groups is:{ ");
for (int i = 0; i < sizeUnion; i++)
{
printf("%d ", UnionArr[i]);
}
printf("}\n");
InterArr=Intersection(group1,size1,group2,size2,&sizeInter);
printf("The Inetsection of the groups is:{ ");
for (int i = 0; i < sizeInter; i++)
{
printf("%d ", InterArr[i]);
}
printf("}\n");
return 0;
}