🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
One of the fundamental operations while working with arrays is the deletion of an element at a specific position. In this post, we will be demonstrating a C program to delete an element from a specified position in an array. We will also provide a step-by-step explanation of the program.
2. Program Overview
The following C program will:
1. Obtain the size of the array and its elements from the user.
2. Request the position of the element the user wants to delete.
3. Delete the element from the specified position.
4. Showcase the modified array to the user.
3. Code Program
#include <stdio.h>
int main() {
int n, i, pos;
int arr[100];
// Collecting the number of elements in the array
printf("Enter the number of elements in array: ");
scanf("%d", &n);
// Obtaining the elements
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Getting the position of the element to delete
printf("Enter the position of element to delete (0 to %d): ", n-1);
scanf("%d", &pos);
// Deleting the element
for(i = pos; i < n-1; i++) {
arr[i] = arr[i+1];
}
// Displaying the result
printf("Resultant array is:\n");
for(i = 0; i < n-1; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements in array: 5 Enter 5 integers: 10 20 30 40 50 Enter the position of element to delete (0 to 4): 4 Resultant array is: 10 20 30 40
4. Step By Step Explanation
1. Variable Declaration:
- n is used to store the number of elements in the array.
- arr[100] represents the array holding our elements.
- pos is the position from which we will delete an element.
2. Input Gathering:
- We start by getting the number of elements and then each individual element from the user.
- Next, we ask the user for the position of the element they wish to delete.
3. Element Deletion:
- To delete the desired element, we shift all the elements from its right, one position to the left. This overwrites the element at the position, essentially deleting it.
- It's important to note that this doesn't free up memory; it only changes the value. If we were using dynamic memory allocation, we would need to free the memory too.
4. Displaying the Modified Array: We then display the new array after deletion.
Through this method, one can delete an element from any position in the array. Nonetheless, it's crucial to be cautious and not try to delete a position outside the range of the array to avoid unexpected results.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment