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;
}