I am learning to program in C, and I have this exercise that I can not finish solving:
"Insert an element in an array of integers, given the insertion position"
Does anyone think I'm failing? Would I have to use a helper to move items when I try to insert?
Of course, thank you!
#include <stdio.h>
#include <stdlib.h>
#define TAM 5
int main()
{
int ce=0;
int vector[TAM];
int pos;
int dato_agregar;
int i;
for(i=0;i<TAM;i++)
{
puts("Ingrese el elemento nuevo");
fflush(stdin);
scanf("%d",&vector[i]);
ce++;
}
puts("Ingrese posicion donde insertar el elemento nuevo");
fflush(stdin);
scanf("%d",&pos);
while(pos>ce) //validar posicion
{
puts("Ingrese posicion donde insertar el elemento nuevo");
fflush(stdin);
scanf("%d",&pos);
}
puts("Ingrese el elemento nuevo");
fflush(stdin);
scanf("%d",&dato_agregar);
for(i=0;i<=ce;i++)
{ int aux;
if(i==pos)
vector[i]=dato_agregar;
}
for(i=0;i<ce;i++)
{
printf("Elemento %d = %d\n",i,vector[i]);
}
return 0;
}