- /* program to remove the specific elements from an array in C. */
- #include <stdio.h>
- #include <conio.h>
- int main ()
- {
- // declaration of the int type variable
- int arr[50];
- int pos, i, num; // declare int type variable
- printf (" \n Enter the number of elements in an array: \n ");
- scanf (" %d", &num);
- printf (" \n Enter %d elements in array: \n ", num);
- // use for loop to insert elements one by one in array
- for (i = 0; i < num; i++ )
- { printf (" arr[%d] = ", i);
- scanf (" %d", &arr[i]);
- }
- // enter the position of the element to be deleted
- printf( " Define the position of the array element where you want to delete: \n ");
- scanf (" %d", &pos);
- // check whether the deletion is possible or not
- if (pos >= num+1)
- {
- printf (" \n Deletion is not possible in the array.");
- }
- else
- {
- // use for loop to delete the element and update the index
- for (i = pos - 1; i < num -1; i++)
- {
- arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
- }
- printf (" \n The resultant array is: \n");
- // display the final array
- for (i = 0; i< num - 1; i++)
- {
- printf (" arr[%d] = ", i);
- printf (" %d \n", arr[i]);
- }
- }
- return 0;
- }
Pages
- Home
- Data Structure book & DBMS notes
- SQL example
- Lab Manual for B.C.A & B.Tech
- Question Bank
- Data Structure Program
- Question Paper format for Sessional Exam
- Books
- ePaper & Times ascent epaper
- B. Tech(CS 2nd Year)
- Question Bank for CSE 2nd Year
- B.C.A Notes
- B.Tech CSE 2nd Year Notes
- C Programming Video Lecture
- Compiler Design Lab
- Logic in Computer Science
- Data Structure Algorithm Video Lecture
- Notes Unit 1
- Notes Unit 2
- Notes Unit 3
- Notes Unit 5
- Notes Unit 4
- SQL Notes
- Linked List
Sunday, August 28, 2022
Deleting an Element at a given Location
C program to Insert an element in an Array
// C Program to Insert an element
// at a specific position in an Array
#include <stdio.h>
int main()
{
int arr[100];
int i, x, pos, n = 10;
// initial array of size 10
for (i = 0; i < 10; i++)
scanf("%d",&arr[i]);
// print the original array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
// element to be inserted
x = 50;
// position at which element
// is to be inserted
pos = 5;
// increase the size by 1
n++;
// shift elements forward
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Tuesday, August 23, 2022
Subscribe to:
Posts (Atom)