Delete an element in a vector in C

9

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

Use CodeBlocks in Windows. I really do not find the flaw. What is happening, and how can I solve 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;
}
    
asked by Fesa 22.01.2016 в 18:42
source

2 answers

8

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

There is really no element being deleted. Let's see the code of 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:

  • From the position specified by element.
  • Move all items one position.
  • Reduce 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. So what your algorithm does is: nothing. Because the for loop is from position 4 and until the position is less than 3. It never enters. But the quantity variable (the number of elements in the vector) is reduced by one, so the new vector is [4,5]. Even when you indicated that you deleted item 4, you "erase" the 6.

    
answered by 22.01.2016 в 19:18
2

Equal should do a rebuild like this is using a previous compilation, if I understand well what you want to achieve this is what I get:

Menu
-----
0: Salir
1: Cargar vector
2: Mostar vector
3: Eliminar vector
1
Cuantos elementos desea ingresar?3
Ingrese elemento: 4 
Ingrese elemento: 5
Ingrese elemento: 6
Menu
-----
0: Salir
1: Cargar vector
2: Mostar vector
3: Eliminar vector
2
Elemento[0]= 4
Elemento[1]= 5
Elemento[2]= 6
Menu
-----
0: Salir
1: Cargar vector
2: Mostar vector
3: Eliminar vector
3
Ingrese elemento a eliminar: 1
Menu
-----
0: Salir
1: Cargar vector
2: Mostar vector
3: Eliminar vector
2
Elemento[0]= 4
Elemento[1]= 6
Menu
-----
0: Salir
1: Cargar vector
2: Mostar vector
3: Eliminar vector
    
answered by 22.01.2016 в 19:15