Computer Science‎ > ‎

C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product



 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart

          
C How to Program (Deitel and Deitel)

 


C Program source code to demonstrate common operations on Sets 


#include<stdio.h>
int *setA,*setB;
int ctr,octr,ictr,sizeA,sizeB;
void unionsets()
{//The function to calculate union of two sets
    int *unions=(int *)malloc((sizeA+sizeB)*sizeof(int));
    ctr=0;//array unions declared to hold size of max possible union set
    for(octr=0;octr<sizeA;octr++)
    unions[ctr++]=setA[octr];//copy Set A
    for(octr=0;octr<sizeB;octr++)
    {
        int flag=0;//Copy those elements of set B which are not in set A
        for(ictr=0;ictr<sizeA;ictr++)
        {
            if(setA[ictr]==setB[octr])
            flag=1;//flag keeps track whether element is found or not
        }
        if(flag==0)
        unions[ctr++]=setB[octr];
    }
    printf("\nA Union B is \n");
    for(octr=0;octr<ctr;octr++)//display set
    printf("%d\t",unions[octr]);
    free(unions);
    
}
void intersection()
{
    int size;
    if(sizeA>=sizeB)
    size=sizeB;
    else//the sizxe of intersection set will be of the size of the smaller of two sets
    size=sizeA;
    ctr=0;
    int *intersection=(int *)malloc(size*sizeof(int));
    for(octr=0;octr<sizeA;octr++)
    {
        for(ictr=0;ictr<sizeB;ictr++)
        {
            if(setA[octr]==setB[ictr])//copy those elemts which are common to both
            intersection[ctr++]=setB[ictr];
        }
    }
    printf("\nA Intersection B is \n");
    if(ctr==0)
    {//if no elemnts are there show null and return
    printf("NULL");
    return;
    }//display
    for(octr=0;octr<ctr;octr++)
    printf("%d\t",intersection[octr]);
    free(intersection);
    
}
void difference()
{
    int *BdiffA=(int *)malloc(sizeB*sizeof(int));
    ctr=0;//the difeernce array can have size at max Of B if we find B-A
    for(octr=0;octr<sizeB;octr++)
    {
        int flag=0;
        for(ictr=0;ictr<sizeA;ictr++)
        {
            if(setA[ictr]==setB[octr])
            flag=1;//if flag=1 the elemnt is in A and in B
        }
        if(flag==0)//copy only those elemnts which are in B not A
        BdiffA[ctr++]=setB[octr];
    }
    printf("\nB - A is \n");
    if(ctr==0)
    {    printf("NULL");
    return;//if ctr=0 then NULL
    }
    for(octr=0;octr<ctr;octr++)
    printf("%d\t",BdiffA[octr]);//display
    free(BdiffA);
    
}
void CardinalProduct()
{
    int **cardinalProduct=(int **)malloc(sizeA*sizeB*sizeof(int));//There will be sizeA*sizeB rows in the cardinal product matrix
    for(ctr=0;ctr<sizeA*sizeB;ctr++)
    cardinalProduct[ctr]=(int *)malloc(2*sizeof(int));//1 cardinal product has 2 elements (a,b)
    ictr=0;
    for(ctr=0;ctr<sizeA*sizeB;ctr++)
    {
        cardinalProduct[ctr][0]=setA[ctr/sizeB];//way to fill the first element with appropriate set A element
        cardinalProduct[ctr][1]=setB[ctr%sizeB];//way to fill the second element with appropriate set B element
    }
    printf("\nA*B is \n");
    for(ctr=0;ctr<sizeA*sizeB;ctr++)
    printf("(%d,%d)\t",cardinalProduct[ctr][0],cardinalProduct[ctr][1]);//displaying in cartesian form
    for(ctr=0;ctr<sizeA*sizeB;ctr++)
    free(cardinalProduct[ctr]);
    free(cardinalProduct);
}
int main()
{
printf("Enter the size for set A\n");
scanf("%d",&sizeA);
printf("Enter the size for set B\n");
scanf("%d",&sizeB);
setA=(int *)malloc(sizeA*sizeof(int));
setB=(int *)malloc(sizeB*sizeof(int));
printf("Enter the elements of set A\n");
for(ctr=0;ctr<sizeA;ctr++)
scanf("%d",&setA[ctr]);
printf("Enter the elements of set B\n");
for(ctr=0;ctr<sizeB;ctr++)
scanf("%d",&setB[ctr]);
unionsets();
intersection();
difference();
CardinalProduct();
free(setA);
free(setB);
getch();
}
/*A sample run of the program runs as follows:-
Enter the size for set A
3
Enter the size for set B
4
Enter the elements of set A
1 2 3
Enter the elements of set B
4 5 6 7
A Union B is
1 2 3 4 5 6 7
A Intersection B is
NULL
B - A is
4 5 6 7
A*B is
(1,4) (1,5) (1,6) (1,7) (2,4) (2,5) (2,6) (2,7) (3,4) (3,5) (3,6) (3,7)
*/





 





C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers