Latest :

C Program : Find Missing Elements of a Range – 2 Ways | C Programs

Write a C program to find the missing elements of a given range. For this, the set of inputs necessary are, size of an array or number of elements in the array along with the elements or data values of the array. We also need the start and end range as input based on which we’ll compare with the array to find the missing elements. Our expected output is a set of all integers that are missing in the given range.

Reading the inputs is the first step to follow. To gather these inputs at runtime, we will use predefined input function called scanf(). This function reads input of any primitive datatype at runtime from the keyboard directly.

The type of input is determined by the format specifier we mention in the code. For our given problem statement all the inputs are of integer type therefore, we’ll use ‘%d’ format specifier for reading all of them. We first read the size of array and then, create an array of the same size.

Then, we iterate through the loop from beginning to the end and read each element or data value. Later, we will read the start and end mentioning the range.

printf(“\nEnter number of elements in an array:”);

scanf(“%d”,&n);

int a[n];

printf(“\nEnter %d elements in array:\n”,n);

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

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

}

printf(“\nEnter Start and End values:\n”);

scanf(“%d\n%d”,&start,&end);

After gathering all the necessary inputs, we’ll iterate from the given range in loop from start to end. For each value of start, we’ll iterate through the given array and check whether it matches nay of thelement in the array.

If it matches then, we update the c value (initially zero for every iteration from start to end) to 1 and break from the loop of iterating through the array. We then check whether the value of c equals zero.

If it equals zero then it means that, the start value is missing in the array so, we display it using printf() and update the noMossing variable (initially 0) to 1. If the c value is not equal to zero it means that, the element is not missing so, we do not do anything. We then, increment the start value by 1 for the next iteration. This continues until the start value becomes greater than the end value.

printf(“\nMissing elements in a range of %d to %d :\n”,start,end);

while(start<=end) {

c=0;

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

if(start==a[i]) {

c=1;

break;

}

}

if(c==0) {

printf(“%d “,start);

noMissing=1;

}

start++;

}

Even after completion of all the iterations, if the noMissing variable value still remains zero it means that, there is no missing value in the given range and therefore, we display the same using the predefined output function printf().

if(noMissing==0)

printf(“\nThere are no missing elements in a given range”);

This way, we can display all the missing values in the specified range that are not present in the array and also display that there are no missing values if there really aren’t any. This is our desired resultant output for the given problem statement.

Output:

C Program Missing elements of a range – Using Function

Above we have seen the logic to display the missing numbers in the given range which are not present in the array. This is a lengthy code and we can observe that, the entire code is written within main method.

The major disadvantages with it is that, if the same logic is needed elsewhere then, we’ll have to rewrite the same set of statements again. Also, if some part of the code is to be searched we’ll have to go through the entire main method to find it and it could be a hideous task. So, the code is neither reusable nor very readable.

To overcome this, we can make use of functions instead. The code can be split up into parts based on its logic and functionality and be placed within separate function blocks. If we do so then, if same logic is required elsewhere we can just make a function call and pass the necessary parameters instead of rewriting it thereby, making it reusable.

Also, the code becomes a lot cleaner and if some part of code is to be searched then, we can directly go to its corresponding function instead of searching all over main method hence, making it more readable.

To implement the same, we first read the set of inputs from the main method using scanf() and then make a function call to the use-defined function (missingElements).

We pass all the inputs as parameters and it returns an integer equivalent telling whether there are missing numbers or not. This function contains the same logic discussed above to find the missing elements are returns 1 if there are missing elements and 0 it there aren’t any.

In main method, if the value returned is 0 then, we display that there are no missing elements in the given range in the array. This way, the main method handles the input/output operations whereas, the user-defined functino (missingElements) takes care of the logic behind it.

Output:

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...