In the below C program you going to learn how you can find Union of Two given sets or arrays. These type math related C programs can challenge anyone to force their mind to think of an algorithm for C program that can find the given solution of the problem.

Interesting part of this program is that it may look complicated but it is a very to understand C program. It just has a bit of twist.

then the union on A and B will be,

This is the main mathematical logic that will be used for creating a C program that will be used to find out union of two sets or arrays.

##

If the condition is true then the value of element of Set A at index i gets printed on screen and i gets incremented.

If the condition is true then, value of element of Set B at index j gets printed and j gets incremented.

Upon execution the value of element of Set B at index j gets printed and both i and j gets incremented.

Interesting part of this program is that it may look complicated but it is a very to understand C program. It just has a bit of twist.

## Logic for Union of Two Sets

Union means including all the elements of both the sets and placing only one element for two same elements. Let's understand this with an example.**A = {1,2,3,4,5}****B = {4,6,8}**then the union on A and B will be,

**A**U**B = {1,2,3,4,5,6,8}**This is the main mathematical logic that will be used for creating a C program that will be used to find out union of two sets or arrays.

## Source Code for C Program

#include<stdio.h> #include<conio.h> void main() { int a[50],b[50],i,j,k,m,n; printf("Enter no.of elements in to the first set: "); scanf("%d",&m); printf("Enter %d numbers : ",m); for(i=0;i<m;i++) scanf("%d",&a[i]); printf("Enter no.of elements in to the second set: "); scanf("%d",&n); printf("Enter %d numbers: ",n); for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0,j=0;i<m&&j<n;) { if(a[i]<b[j]) { printf("%d ",a[i]); i++; } else if(a[i]>b[j]) { printf("%d ",b[j]); j++; } else { printf("%d ",b[j]); i++; j++; } } if(i<m) { for(;i<m;i++) printf("%d ",a[i]); } if(j<n) { for(j<n;j++) printf("%d ",b[j]); } getch(); }

## Explanation of Algorithm used in C Program

### 1. First For Loop

The first for loop of the algorithm runs from i=0 and j=0 until**i=m-1**and**j=n-1**.### 2. The If condition

The if condition inside the first for loop check if element of set A at index**i**is smaller than the element of Set B at index**j**.If the condition is true then the value of element of Set A at index i gets printed on screen and i gets incremented.

### 3. Else If condition

The else if condition in the first for loop checks if element of Set A at index i is greater than element of Set B at index j.If the condition is true then, value of element of Set B at index j gets printed and j gets incremented.

### 4. Else condition

The else condition of the if-else ladder will get executed if both the above condition is false.Upon execution the value of element of Set B at index j gets printed and both i and j gets incremented.

### 5. First If condition outside for loop

The First if condition outside the first for loop check if after the first for loop the value of i is still smaller than m. If it's true that means some elements are still left to be printed in set A. So, the for loop inside the if condition prints all the remaining elements of Set A.### 6. Second If condition after first for loop

The second if condition checks if the value of j after running the first for loop is smaller than n. If it's true it means that some elements of Set B are still left to be printed. So, the for loop inside the second if condition prints the remaining elements of Set B.**Summary :**The above C program checks or compares elements of both the Sets or Arrays and prints the values according to increasing order and increments the value of the i and j or indexes according to next comparison should be. The if conditions after the for loop checks if after a index elements are left un-printed and prints the rest of remaining elements for both the Set or Arrays.
## No comments:

## Post a Comment