Latest :

C Program : Check If Arrays are Disjoint or Not | C Programs

Write a C program to find whether the given two arrays are disjoint set or not. We call two arrays disjoint set when they have no common elements between them. For checking this, we’ll first require the number of elements in the two arrays and the data values or elements in the two arrays as input.

Our expected output as mentioned in the problem statement is whether they are disjoint sets or not and, if they are not then, displaying the common elements between the two.

The first step is to gather all the necessary inputs. To read them at runtime in C, we can make use of a well-known predefined function called scanf().

This function reads any primitive datatype like int, float, char, long, etc., at runtime and based on the format specifier given in the code the input datatype is decided. For our given problem statement, all the inputs are of integer type hence, we use the ‘%d’ format specifier to read them.

We first read the sizes of two arrays and create the two arrays of the same size. We then iterate in loop from beginning to end of individual array and read the data values or elements of the two arrays.

printf(“\nEnter the size of array 1 : “);

scanf(“%d”,&n1);

printf(“\nEnter the size of array 2 :” );

scanf(“%d”,&n2);

int arr1[n1];

int arr2[n2],temp[n1];

printf(“\nEnter the %d elements in Array 1:\n”,n1);

for(i=0;i<n1;i++) {

scanf(“%d”,&arr1[i]);

}

printf(“\nEnter the %d elements in Array 2 :\n”,n2);

for(i=0;i<n2;i++) {

scanf(“%d”,&arr2[i]);

}

Output:

Check If Arrays are Disjoint or Not In C – Using Function

In the above method, we have seen the logic to be used for the given problem statement but as we can observe, the entire code is within main method. The disadvantages that arise with this are that, the code is neither reusable and nor is it that readable.

For example, if the same logic is needed to be used somewhere else in the same code then, we will have to rewrite it all over again. Also, if some part of code is to be searched then, we’ll have to go through entire main method to find it which could be time consuming.

To get over these problems, we can make use of functions instead. Here, we can split the code based on its functionality and place it in different function blocks. This way, if some logic is to be reused then, we can directly make a function call and pass necessary parameters to it.

Apart from that, if some part of code is to be found then, we can directly go to its corresponding function block and look for it. Hence, code becomes reusable as well as readability is improved.

To implement this, we can split the code into to parts- the input/output operations and the logic.

The main method first reads all the necessary inputs using scanf() function just as above and then, makes a function call to the user-defined function (disjoint).

To this method, all the inputs are passed and this contains the logic behind determining whether the two arrays are disjoint sets or not. With the same logic as above it iterates through both the arrays and if it finds a matching element then it returns -1 to the main method. If there are no matching elements till the end then, it returns 1.

In main method, if the value returned is -1 then, the two arrays are not disjoint sets and the same is displayed on the console screen. Else, if the value returned is 1 then, the two arrays are disjoint set and the same is displayed on the console screen. This way, the main method handles the i/o operations whereas the user-defined function (disjoint) contains the logic for the problem statement.

Output:

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...