#include<stdio.h>
#include<stdlib.h>
int insert(int *A, int a,int size){
int flag = 0;
int i;
for(i = 0; i <= size ; i++){
if(A[i] == a){
flag = 1;
printf("Dulicate not allowed in Set\n");
}
}
if(flag == 0){
A[size] = a;
return 1;
}
else
return 0;
}
void intersection(int *A,int *B,int sizeA,int sizeB){
int ni,i,j;
int sizei=0;
if(sizeA < sizeB)
ni = sizeA;
else
ni = sizeB;
int itr = 0;
int *intsec = malloc(ni * sizeof(int));
for(i = 0;i < sizeA;i++){
for(j = 0;j < sizeB;j++){
if(A[i] == B[j]){
intsec[sizei] = A[i];
sizei++;
continue;
}
itr++;
}
}
printf("\nIntersection Result is\n");
for(i = 0; i < sizei ; i++)
printf("%d \n",intsec[i]);
}
void setdifferance(int *A,int *B,int sizeA,int sizeB,int ch){
int i ,j,ni;
int flag =0;
int k = 0;
if(sizeA > sizeB)
ni = sizeA;
else
ni = sizeB;
int *setdiff = malloc(ni * sizeof(int));
if(ch == 1) {
for(i = 0 ; i < sizeA ; i++){
for(j = 0 ; j < sizeB; j++){
if(A[i] == B[j]){
flag = 1;
}
}
if(flag == 0){
setdiff[k] = A[i];
k++;
}
flag = 0;
}
}
if(ch == 2){
for(i = 0 ; i < sizeB ; i++){
for(j = 0 ; j < sizeA ; j++){
if(B[i] == A[j]){
flag = 1;
}
}
if(flag == 0){
setdiff[k] = B[i];
k++;
}
flag = 0;
}
}
printf("\nSet Differance Result is\n");
if(k == 0)
printf("NULL\n");
for(i = 0; i < k ; i++)
printf("%d \n",setdiff[i]);
}
void unionOp(int *A,int *B,int sizeA, int sizeB){
int size = sizeA + sizeB;
int *uni = malloc(size * sizeof(int));
int i,j;
int k=0;
int flag =0;
for(i = 0 ; i < sizeA; i++){
uni[i] = A[i];
}
for(j = 0 ; j < sizeB ; j++){
for(i = 0 ; i < sizeA ; i++){
if(B[j] == A[i]){
flag =1;
}
}
if(flag == 0){
k++;
uni[sizeA+k-1] = B[j];
}
flag = 0;
}
printf("\nUnion Result is\n");
for(i = 0; i < sizeA+k ; i++)
printf("%d \n",uni[i]);
}
void main(){
int na,nb,a,b,sizeA,sizeB,flag,diffch;
int ch=1;
//struct set *s, *B;
sizeA = 0;
sizeB = 0;
printf("Enter number of elements of set 1\n");
scanf("%d",&na);
int *A = malloc(na * sizeof(int));
int *B = malloc(nb * sizeof(int));
while(sizeA < na){
printf("\nInsert elements in set 1\n");
scanf("%d",&a);
sizeA += insert(A,a,sizeA);
}
printf("\nEnter number of elements of set 2\n");
scanf("%d",&nb);
while(sizeB < nb){
printf("Insert elements in set 2\n");
scanf("%d",&b);
sizeB += insert(B,b,sizeB);
}
while(ch){
printf("\nPress 1 for intersection\n"
"Press 2 for union\n"
"Press 3 for set differance\n"
"Press 0 for exit \n");
scanf("%d",&ch);
switch(ch){
case 1:
intersection(A,B,sizeA,sizeB);
break;
case 2:
unionOp(A,B,sizeA,sizeB);
break;
case 3:
printf("\nFor (Set 1 - set2) press 1 "
"Else press 2\n");
scanf("%d",&diffch);
setdifferance(A,B,sizeA,sizeB,diffch);
break;
case 0:
printf("Good-Bye\n");
break;
default:
printf("Invalid Input\n");
}
}
}
#include<stdlib.h>
int insert(int *A, int a,int size){
int flag = 0;
int i;
for(i = 0; i <= size ; i++){
if(A[i] == a){
flag = 1;
printf("Dulicate not allowed in Set\n");
}
}
if(flag == 0){
A[size] = a;
return 1;
}
else
return 0;
}
void intersection(int *A,int *B,int sizeA,int sizeB){
int ni,i,j;
int sizei=0;
if(sizeA < sizeB)
ni = sizeA;
else
ni = sizeB;
int itr = 0;
int *intsec = malloc(ni * sizeof(int));
for(i = 0;i < sizeA;i++){
for(j = 0;j < sizeB;j++){
if(A[i] == B[j]){
intsec[sizei] = A[i];
sizei++;
continue;
}
itr++;
}
}
printf("\nIntersection Result is\n");
for(i = 0; i < sizei ; i++)
printf("%d \n",intsec[i]);
}
void setdifferance(int *A,int *B,int sizeA,int sizeB,int ch){
int i ,j,ni;
int flag =0;
int k = 0;
if(sizeA > sizeB)
ni = sizeA;
else
ni = sizeB;
int *setdiff = malloc(ni * sizeof(int));
if(ch == 1) {
for(i = 0 ; i < sizeA ; i++){
for(j = 0 ; j < sizeB; j++){
if(A[i] == B[j]){
flag = 1;
}
}
if(flag == 0){
setdiff[k] = A[i];
k++;
}
flag = 0;
}
}
if(ch == 2){
for(i = 0 ; i < sizeB ; i++){
for(j = 0 ; j < sizeA ; j++){
if(B[i] == A[j]){
flag = 1;
}
}
if(flag == 0){
setdiff[k] = B[i];
k++;
}
flag = 0;
}
}
printf("\nSet Differance Result is\n");
if(k == 0)
printf("NULL\n");
for(i = 0; i < k ; i++)
printf("%d \n",setdiff[i]);
}
void unionOp(int *A,int *B,int sizeA, int sizeB){
int size = sizeA + sizeB;
int *uni = malloc(size * sizeof(int));
int i,j;
int k=0;
int flag =0;
for(i = 0 ; i < sizeA; i++){
uni[i] = A[i];
}
for(j = 0 ; j < sizeB ; j++){
for(i = 0 ; i < sizeA ; i++){
if(B[j] == A[i]){
flag =1;
}
}
if(flag == 0){
k++;
uni[sizeA+k-1] = B[j];
}
flag = 0;
}
printf("\nUnion Result is\n");
for(i = 0; i < sizeA+k ; i++)
printf("%d \n",uni[i]);
}
void main(){
int na,nb,a,b,sizeA,sizeB,flag,diffch;
int ch=1;
//struct set *s, *B;
sizeA = 0;
sizeB = 0;
printf("Enter number of elements of set 1\n");
scanf("%d",&na);
int *A = malloc(na * sizeof(int));
int *B = malloc(nb * sizeof(int));
while(sizeA < na){
printf("\nInsert elements in set 1\n");
scanf("%d",&a);
sizeA += insert(A,a,sizeA);
}
printf("\nEnter number of elements of set 2\n");
scanf("%d",&nb);
while(sizeB < nb){
printf("Insert elements in set 2\n");
scanf("%d",&b);
sizeB += insert(B,b,sizeB);
}
while(ch){
printf("\nPress 1 for intersection\n"
"Press 2 for union\n"
"Press 3 for set differance\n"
"Press 0 for exit \n");
scanf("%d",&ch);
switch(ch){
case 1:
intersection(A,B,sizeA,sizeB);
break;
case 2:
unionOp(A,B,sizeA,sizeB);
break;
case 3:
printf("\nFor (Set 1 - set2) press 1 "
"Else press 2\n");
scanf("%d",&diffch);
setdifferance(A,B,sizeA,sizeB,diffch);
break;
case 0:
printf("Good-Bye\n");
break;
default:
printf("Invalid Input\n");
}
}
}
No comments:
Post a Comment