Delete an element in a vector in C

Question:

I have developed this algorithm that loads, displays, and removes elements in a vector. But when I give to delete an item, it only deletes the last one and not the one I choose. For example, I enter the items: 4, 5, 6; and then I say that I want to eliminate the 4 or the 5 and the 6 is eliminated, that is to say always the last one.

I use CodeBlocks on Windows. I really can't find the fault. What is happening and how can I fix it?

Here I leave the code:

#include <stdio.h>

#define TAM 30

void CargarVector(int arr[TAM], int cantidad);
void MostrarVector(int arr[TAM], int cantidad);
void EliminarElemento(int arr[TAM], int *cantidad, int elem);

int main(){
    int option;
    int arr[TAM];
    int cantidad;
    int elem;

    do{
        printf("Menu\n");
        printf("-----\n");
        printf("0: Salir\n");
        printf("1: Cargar vector\n");
        printf("2: Mostar vector\n");
        printf("3: Eliminar vector\n");
        scanf("%d", &option);
        switch(option){
            case 1: printf("Cuantos elementos desea ingresar?"); scanf("%d", &cantidad);
                    CargarVector(arr, cantidad); break;
            case 2: MostrarVector(arr, cantidad); break;
            case 3: printf("Ingrese elemento a eliminar: "); scanf("%d", &elem);
                    EliminarElemento(arr, &cantidad, elem); break;
        }
    } while(option!=0);

    return 0;
}

void CargarVector(int arr[TAM], int cantidad){
    int i;

    for(i= 0; i<cantidad; i+=1){
        printf("Ingrese elemento: "); scanf("%d", &arr[i]);
    }
}

void MostrarVector(int arr[TAM], int cantidad){
    int i;
    for(i= 0; i<cantidad; i+=1){
        printf("Elemento[%d]= %d\n", i, arr[i]);
    }
}

void EliminarElemento(int arr[TAM], int *cantidad, int elem){
    int i;

    for(i= elem; i<*cantidad-1; i+=1){
        arr[i]= arr[i+1];
    }
    *cantidad-=1;
}

Answer:

The problem is that the elem parameter is the element and not the position the element is in. The algorithm would work well if instead of asking "Enter element to delete:" what you would ask was: "Enter the position of the element to delete:".

You are not really deleting any item itself. Let's see the code EliminarElemento :

void EliminarElemento(int arr[TAM], int *cantidad, int elem){
    int i;

    for(i= elem; i<*cantidad-1; i+=1){
        arr[i]= arr[i+1];
    }
    *cantidad-=1;
}

What this function does:

  • Starting from the position specified by element.
  • Move all elements one position.
  • Reduces the size of the vector by 1.

For example, in your case of [4,5,6] in particular, the problem is that element 4 is not in position 4 of the vector but in position zero. With which what your algorithm does is: nothing. Because the for loop is from position 4 and until position is less than 3. It never enters. But the variable quantity (the number of elements in the vector) is reduced by one, so the new vector is [4,5]. Even when you indicated to delete item 4, 6 is "deleted".

Scroll to Top