• C Program to Delete Given Number from an Array

Deleting a specific number from an array is a common operation in programming. It allows us to manipulate and modify data to meet specific requirements. In this blog post, we will explore the process of deleting a given number from an array using the C programming language. Whether you are new to programming or have some experience, this post will guide you through the steps involved in deleting a number from an array effectively.

Creating an Array

Before we dive into the process of deleting a number from an array, let’s first understand how to create an array in C. An array is a collection of elements of the same data type, referenced by a common name.

To create an array, we need to declare the data type of the elements and specify the size of the array. Here’s an example:

``int numbers[5]; // Creates an array of integers with a size of 5``

In the above example, we create an array of integers named `numbers` with a size of 5. The array’s elements are indexed from 0 to 4, and you access individual elements in the array by using indexing.

Deleting a Number from the Array

Now that we have an understanding of how to create an array, let’s move on to deleting a specific number from it. To delete a number from an array, we need to perform the following steps:

1. Iterate through the array to find the position of the number to be deleted.

2. If the number is found, shift all the elements after the deleted number one position to the left.

3. Update the size of the array to reflect the deletion.

To better understand this process, let’s consider an example. Suppose we have the following array:

``int numbers[] = {1, 2, 3, 4, 5};``

And we want to delete the number 3 from the array. Here’s how we can achieve that using the steps mentioned above:

1. Iterate through the array to find the position of the number 3 (index = 2).

2. Shift all the elements after the number 3 one position to the left.

Before deletion: {1, 2, 3, 4, 5}After deletion: {1, 2, 4, 5}

3. Update the size of the array to reflect the deletion. In this case, the new size of the array will be 4.

Updating the Array Size

In C, the size of the array is fixed at the time of declaration. However, when we delete an element from an array, it is important to update the array size to ensure that we do not have any unused elements.

To update the array size in C, we can make use of dynamic memory allocation. The `realloc()` function allows us to change the size of a previously allocated memory block. Here’s an example:

``int* resized_numbers = (int*)realloc(numbers, new_size * sizeof(int));``

In the above example, we use the `realloc()` function to update the size of the `numbers` array to `new_size` (4 in our previous example). The `sizeof(int)` is used to calculate the size of each integer element in bytes.

Final C Program

Now that we have covered the steps involved in deleting a number from an array and updating its size, let’s put it all together in a complete C program. Here’s an example:

``````#include <stdio.h>
#include <stdlib.h>

int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);

// Deleting the number 3 from the array
int number_to_delete = 3;
int position = -1;

// Find the position of the number to be deleted
for (int i = 0; i < size; i++) {
if (numbers[i] == number_to_delete) {
position = i;
break;
}
}

// Shift elements after the deleted number to the left
if (position != -1) {
for (int i = position; i < size - 1; i++) {
numbers[i] = numbers[i + 1];
}

size--;

// Update the size of the array
int* resized_numbers = (int*)realloc(numbers, size * sizeof(int));

if (resized_numbers != NULL) {
numbers = resized_numbers;
}
}

// Print the updated array
printf("Updated Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}

// Free the memory allocated to the array
free(numbers);

return 0;
}``````

In this program, we first declare and initialize the `numbers` array. We then determine the size of the array using the `sizeof()` operator.

Next, we find the position of the number we want to delete by iterating through the array. If we find the number, we will shift all the elements after it one position to the left. We then update the size of the array using `realloc()` and store the resized array in a new pointer called `resized_numbers`.

Finally, we print the updated array and free the memory allocated to the array using the `free()` function.

Conclusion

In this blog post, we explored the process of deleting a given number from an array using the C programming language. We discussed creating an array, locating the position of the number to delete, shifting the elements, and updating the array size. We also provided a complete C program that demonstrated these steps.

Deleting a number from an array is a fundamental operation in programming, and understanding how to do it efficiently can greatly enhance your programming skills. By following the steps and code provided in this post, you should now have a clear understanding of how to delete a given number from an array using C.

To further solidify your knowledge, try experimenting with different arrays and numbers to delete. This will give you a deeper understanding of how the deletion process works and how it affects the array’s size and elements. Happy coding!