Basic Set Operations using integer arrays

Program Description

Demonstrates basic set operations using integer arrays. The program prompts the user for number of elements in each set(only two sets are considered for this program) and then takes the input. No error checking is done while taking input. Assumes all inputs are valid.The program provides a menu to the user to perform various operations on sets.The arrays are passed to different functions using pointers

Program
/**********************************
*    code.cheraus.com
*
*   Basic Set Operations using integer arrays
*
*********************************/

#include<stdio.h>

//function prototypes
int *unionofset(int set1[],int set2[],int,int);
void intersectionofset(int set1[],int set2[],int,int);
int *Diffofset(int set1[],int set2[],int num1,int num2);
void Symmetric(int set1[],int set2[],int num1,int num2);


int main()

{
    int ch;
	int num1=0;
	int num2=0;
	int choice=0;
	printf("How many elements in set 1?");
	scanf("%d",&num1);

	printf("\nHow many elements in set two?");
	scanf("%d",&num2);

	int set1[num1];
	int set2[num2];
	int i,j;


        //taking user input
	printf("Enter the elements in set 1:");
	for(i=0;i<num1;i++)
	scanf("%d",&set1[i]);

	printf("\nEnter the elements in set 2:");
	for(j=0;j<num2;j++)
	scanf("%d",&set2[j]);

        printf("\nSet1 is: ");
	 for(i=0;i<num1;i++)
	printf("%d,",set1[i]);

	printf("\nSet2 is:");
	for(j=0;j<num2;j++)
	printf(" %d,",set2[j]);
	printf("\n");

	printf("\nMenu\n");

do
{
	printf("1.Union\n2.Intersection\n3.Set1-Set2\n4.Set2-Set1\n5.Symmetric Difference\n6.Exit\n");
	printf("Enter your choice: ");
	scanf("%d",&choice);

switch(choice)

{
  case 1:

         unionofset(set1,set2,num1,num2);
         break;

 case 2:

        intersectionofset(set1,set2,num1,num2);
        break;

 case 3:

      Diffofset(set1,set2,num1,num2);
      break;

 case 4:

     Diffofset(set2,set1,num2,num1);
     break;

 case 5:

    Symmetric(set1,set2,num1,num2);
    break;

 case 6: return 0;


 default :printf("i\nInvalid choice.......try again\n");
     break;
}

printf("Do you wish to continue ?\n Press 1 to continue and any key to exit.\n ");
scanf("%d",&ch);
}while(ch==1);

    return 0;

}


/*    FUNCTION DEFINITION FOR UNION

Function: unionofset() performs union of the two sets given in the arguments
Inputs:  The function takes the following arguments:
         1) set1 (integer array)
         2) set2 (integer array)
         3)number of elements in set1
         4)number of elements in set2
Return: The function returns a pointer to the resultant set(union of the given sets)

*/

int *unionofset(int set1[],int set2[],int num1,int num2)
{
int (*c1);
int set3[num1+num2];
int i,j,k=0;
int flag=0;
 
 for(i=0;i<num1;i++)
    {
      set3[k]=set1[i];
      k++;
     }

 for(i=0;i<num2;i++)
    {
      flag=0;
      for(j=0;j<num1;j++)

            {
                if(set2[i]==set3[j])
                {
                    flag=1;
                    break;
                 }
             }
      if(flag==0)
     {
        set3[k]=set2[i];
        k++;
      }
   }

  //-100 used as a sentinel value to detect end of the resultant set while displaying result
  set3[k+1]=-100;  
  if(k==0)
   {
      printf("\n\n Resultant set is null set!\n");
   }

  else
  {
    printf("\n Elements of resultant set\n");
    for(i=0;i<k;i++)
    printf("%d, ",set3[i]);
    printf("\n");
  }

   c1=set3;
   return c1;
 }


/*    FUNCTION DEFINITION FOR INTERSECTION

Function: intersectionofset() performs intersection of the two sets given in the arguments
Inputs:  The function takes the following arguments:
         1) set1 (integer array)
         2) set2 (integer array)
         3)number of elements in set1
         4)number of elements in set2
Return: The function returns a pointer to the resultant set(intersection of the given sets)

*/

void intersectionofset(int set1[],int set2[],int num1,int num2)
{
  int set4[num1+num2]; 
  int i,j,k=0;
  int flag=0;

 for(i=0;i<num1;i++)
    {
      flag=1;
      for(j=0;j<num2;j++)

            {
                if(set2[j]==set1[i])
                { 
                    flag=0;
                    break;
                 }
             }
      if(flag==0)
     {
        set4[k]=set1[i];
        k++;
      }
   }


if(k==0)
   {
      printf("\n\n Resultant set is null set!\n");
   }

  else
  {
    printf("\n Elements of resultant set\n");
    for(i=0;i<k;i++)
    printf("%d, ",set4[i]);
    printf("\n");
  }

}
/*    FUNCTION DEFINITION FOR DIFFERENCE

Function: Diffofset() performs difference of the two sets given in the arguments
Inputs:  The function takes the following arguments:
         1) set1 (integer array)
         2) set2 (integer array)
         3)number of elements in set1
         4)number of elements in set2
Return: The function returns a pointer to the resultant set(difference of the given sets)

*/
int *Diffofset(int set1[],int set2[],int num1,int num2)
{
 int (*c2);
 int c[num1+num2];
 int i,j,k=0,flag=0;
  for(i=0;i<num1;i++)
  { 
       flag=0;
       for(j=0;j<num2;j++) 
         { 
           if(set1[i]==set2[j])  
            {
               flag=1;
               break;
            }
         }
   if(flag==0)
   {
    c[k]=set1[i];
    k++;
   }

  }

//-100 used as a sentinel value to detect end of result set while displaying it.
c[k]=-100;


if(k==0)
printf("\nThe difference is a null set. ");

else

{   printf("\n The difference is: ");
   for(i=0;i<k;i++)
      {
        printf("%d,",c[i]);
       }
}
 printf("\n");

  c2=c;
  return c2;
}
/*    FUNCTION DEFINITION FOR SYMMETRIC DIFFERENCE 

Function: Symmetric() performs symmetric difference of the two sets given in the arguments
Inputs:  The function takes the following arguments:
         1) set1 (integer array)
         2) set2 (integer array)
         3)number of elements in set1
         4)number of elements in set2
Return: The function does not return anything

*/
void Symmetric(int set1[],int set2[],int num1,int num2)

{
 int (*p1);     //pointer to diff of set1-set2
 int (*p2);     //pointer to diff of set2-set1
 int (*p3);     //pointer to union of diff of 2 sets
 int i,j,flag,nd=0,ne=0;
 int k=num1+num2;
 int d[k];     //array for storing temporarily set1-set2
 int e[k];     //array for storing temporarily set2-set1


//calculate set1-set2 by calling the Diffofset() function
 p1=Diffofset(set1,set2,num1,num2);

 for(i=0;i<k;i++)
 {
  d[i]=*(p1);
  p1++;
  flag=d[i];
  nd++;
  if(flag==-100)
  break; 
 }


 //calculate set2-set1 by calling Diffofset() function
 p2=Diffofset(set2,set1,num2,num1);
 
 for(j=0;j<k;j++)
 {
  e[j]=*(p2);
  p2++;
  flag=e[j];
  ne++;
  if(flag==-100)
  break;
 }

j=nd+ne-2;


//array for storing the symmetric difference of sets
int ans[j];


//calculating union by using the unionofset() function
p3=unionofset(d,e,nd-1,ne-1);


//storing the elemets pointed by pointer p3  in the "ans" array
for(i=0;i<j;i++)
{
ans[i]=(*p3);
p3++;
}



//Displaying the result
if(j==0)
 printf("The symmetric difference is a null set! ");

else
{
   printf("\nThe symmetric difference is: ");

   //prints the final set of symmetric difference
   for(i=0;i<j;i++)
     {
        printf("%d,",ans[i]);
     }
}
printf("\n");

}

Output

How many elements in set 1?5
How many elements in set two?5
Enter the elements in set 1:1
2
3
4
5
Enter the elements in set 2:2
3
6
7
8
Set1 is: 1,2,3,4,5,
Set2 is: 2, 3, 6, 7, 8,
Menu
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 1
Elements of resultant set
1, 2, 3, 4, 5, 6, 7, 8,
Do you wish to continue ?
Press 1 to continue and any key to exit.
1
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 2
Elements of resultant set
2, 3,
Do you wish to continue ?
Press 1 to continue and any key to exit.
1
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 3
The difference is: 1,4,5,
Do you wish to continue ?
Press 1 to continue and any key to exit.
1
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 4
The difference is: 6,7,8,
Do you wish to continue ?
Press 1 to continue and any key to exit.
1
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 5

The difference is: 1,4,5,
The difference is: 6,7,8,
Elements of resultant set
1, 4, 5, 6, 7, 8,

The symmetric difference is: 1,4,5,6,7,8,
Do you wish to continue ?
Press 1 to continue and any key to exit.
1
1.Union
2.Intersection
3.Set1-Set2
4.Set2-Set1
5.Symmetric Difference
6.Exit
Enter your choice: 6

Comments
comments powered by Disqus