Tuesday, 3 July 2012

Delete an element from an array in C


Code - 


//____Program to delete an element from an array at a particular position____

//______Including Header Files___

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int main()
{
int elements[9], position = 0, element = 0, counter = 0;

printf("\n\t______ Delete an element from an array at a particular position ______");

printf("\n\n   Emter the elements :- ");

for(counter = 0 ; counter <= 8; counter ++)
{
printf("\n\n\t%d element - ", counter + 1);

scanf("%d", &elements[counter]);
}

system("cls");

printf("\n\t______ Delete an element from an array at a particular position ______");

printf("\n\n  Elements entered in array :-  \n\n");

for(counter = 0; counter <= 8; counter ++)
{
if (counter % 3 == 0)
{
printf("\n\n");
}
printf("  '%d' Position - %d        ", counter + 1, elements[counter]);
}

printf("\n\n\t Enter the position to delete an element :- ");

scanf("%d", & position);

element = elements[position - 1];

for(counter = position - 1; counter <= 7; counter ++)
{


elements[counter] = elements[counter + 1];
}

printf("\n\n  Elements entered in new array :-  \n\n");

for(counter = 0; counter <= 7; counter ++)
{
if (counter % 3 == 0)
{
printf("\n\n");
}
printf("  '%d' Position - %d        ", counter + 1, elements[counter]);
}

printf("\n\n  Element deleted - %d at position %d ", element, position);

getch();


}

Follow Us on Facebook - Assignment Hub

No comments:

Post a Comment