Deleting a Value in an Array in C
This program searches for and deletes a value entered, it has two steps:
- Browse List until the search is complete and memorize the clue in a variable k.
- Delete the value at the k.
#include< stdio.h> #include< stdlib.h> int i,k; struct { int l; int t[50]; }tab; void sup(int t[20],int k) { for(i=k; i< tab.l; i++) t[i]=t[i+1]; tab.l--; } main() { //Change the Color From The interface en blue system("color 9e"); int valley; printf("give the table size "); do scanf("%d",& tab.l); //la Size of Table Do not must Not Exceed 50 while((tab.l>=50)||(tab.l<=0)); //fill in the Table with the values for(i=0; i< tab.l; i++) { printf("T[%d]=",i); scanf("%d",& tab.t[i]); } //display d array printf("Table Display \n"); for(i=0; i< tab.l; i++) printf("\t\tT[%d]=%d\n",i,tab.t[i]); printf("Give the value of The Element to delete "); scanf("%d",& val); //Search the Value in the Table and Delete la for(k=0; k< tab.l; k++) if(tab.t[k]==val) sup(tab.t,k); puts(""); //Display after deletion printf("View New Table \n"); for(i=0; i< tab.l; i++) printf("\t\tT[%d]=%d\n",i,tab.t[i]); system("pause"); return 0; } |