Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int searchElement(int arr[], int size, int value){
- int i;
- for(i=0; i<size; i++){
- if(arr[i]==value){
- return i;
- }
- }
- return -1;
- }
- int removeElement(int arr[], int size, int value){
- int pos, i;
- pos = searchElement(arr, size, value);
- if(pos==-1){
- return -1;
- }
- for(i = pos; i<size-1; i++){
- arr[i] = arr[i+1];
- }
- return size-1;
- }
- int main()
- {
- int a[50], i;
- int n, value, location;
- printf("Enter how many numbers: ");
- scanf("%d", &n);
- for(i=0; i<n; i++){
- scanf("%d", &a[i]);
- }
- printf("Enter the element to be deleted: ");
- scanf("%d", &value);
- //Searching
- // location = searchElement(a, n, value);
- //
- // if(location==-1){
- // printf("Item not found!!\n");
- // }
- // else{
- // printf("Item found at index %d\n", location+1);
- // }
- //Deletion
- int size;
- size = removeElement(a, n, value);
- if(size==-1){
- printf("Deletion is not possible, element not found!!");
- }
- else{
- printf("Element %d is deleted!!", value);
- printf("Updated array is: \n");
- for(i = 0; i<size; i++){
- printf("%d ", a[i]);
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement