How to write a C Program To Find Union & Intersection Of Two Array in C Programming Language ?
Solution For C Program :
/*C Program To Find Union & Intersection Of Two Array.*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char arr1[10],arr2[10],arr3[20],arr4[20],arr5[10];
/*If u want to use already initialized array then u can use code given below as comment & change according to this in code given ahead*/
//char arr1[10]={'b','a','l','j','i'},arr2[10]={'g','o','s','w','a','m','i'},arr3[20],arr4[20],arr5[10];
int i=0,m,n,j,temp,k=0,t;
clrscr();
printf("\tNOTE: Union and Intersection are defined for sets and in \n\t set repeation of element is not done.\n\n");
printf("enter no of elements in first array:");
scanf("%d",&m);
fflush(stdin);
printf("\nenter elements of first array:\n");
for(i=0;i<m;i++)
{
scanf("%c",&arr1[i]);
fflush(stdin);
}arr1[i]='\0';
printf("enter no of elements in second array:");
scanf("%d",&n);
fflush(stdin);
printf("\nenter the elements of second array:\n");
for(i=0;i<n;i++)
{
scanf("%c",&arr2[i]);
fflush(stdin);
}arr2[i]='\0';
printf("\nEntered array is:\n\tarr1={");
for(i=0;i<m;i++)
{
printf("%c,",arr1[i]);
arr3[k]=arr1[i];k++; //merging in arr3
}
printf("}\n\tarr2={");
for(i=0;i<n;i++)
{
printf("%c,",arr2[i]);
arr3[k]=arr2[i];k++; //merging in arr3
}arr3[k]='\0';
printf("}\n");
printf("\nMerged array: ");
puts(arr3);
/***************sorting starts here*/
printf("\nsorted array is:\n");
for(i=1;arr3[i]!='\0';i++)
{
for(j=1;arr3[j]!='\0';j++)
{
if(arr3[j-1]<arr3[j])
{
temp=arr3[j-1];
arr3[j-1]=arr3[j];
arr3[j]=temp;
}
}
}
/***************sorting ends here*******/
printf("\n\tarr3={");
for(i=0;arr3[i]!='\0';i++)
{
printf("%c,",arr3[i]);
}
printf("}\n");
/*********Union starts here********/
k=1;
arr4[0]=arr3[0];
for(j=0;arr3[j]!='\0';j++)
{
if(arr3[j]!=arr3[j+1])
{
arr4[k]=arr3[j+1];
k++;
}
}
printf("\nUnion of two array: \n \narr4={");
for(i=0;arr4[i]!='\0';i++)
{
printf("%c,",arr4[i]);
}
/*********Intersection starts here******/
printf("}\n\nIntersection of two array: \n\narr5={");
t=0;
for(j=0;arr3[j]!='\0';j++)
{
if(arr3[j]==arr3[j+1])
{
arr5[t]=arr3[j];
t++;
}
}arr5[t]='\0';
for(i=0;arr5[i]!='\0';i++)
{
printf("%c,",arr5[i]);
}printf("}");
getch();
}
You may also learn these C Program/Code :
C Program To Swap Two Numbers Without Using Third Variable
Learn More :
Find
- Find out the perfect number using c program
- Find g.c.d of two number using c program.
- Write a c program to find out NCR factor of given number
- How to Write a C program to find maximum between two numbers ?
- Write a C program to find maximum between three numbers ?
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program turnLEDOn to Drives Forward Until It Finds The Line
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find Product Of Two No Using MACRO
- C Program To Find Reverse Of Any Digit Number
- C Program To Find Sum and Difference Of Two Matrices
- C Program To Find The Frequency Of A Number
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Transpose Of A Matrix
- C Program Finding the sum of Squares using Recursion
- C Program to Find NCR Of A Given Number Program
- C Program To Find Maximum Of Given Numbers
Array
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- C Program Array Index Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Array Example
- C Program to Array
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Search A Number Inside The Array
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program To Copy One Character Array Into Another
- Returns: array of decoded values. [0] - count of values
- C Program to Check if a number is in an array[1000]
- C Program to Find max and min in array using pointer concept
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to find smallest in an array using pointer
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- C Program to Array Deserialization
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- Input reads in the array positions and range for random generation.