Insert into an array of integers

1

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;
}
    
asked by Cecilia Carolina 15.09.2018 в 19:42
source

2 answers

0

We are going to clean the code a bit before responding.

First, do not use fflush with stdin . fflush is intended to be used only with output devices. That your system works is not a guarantee of success, as reflected in the documentation :

  

In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).

On the other hand, the variable ce does not make sense, since it will always be worth TAM :

int ce=0;
for(i=0;i<TAM;i++) // ce == 0 && i == 0
{
  puts("Ingrese el elemento nuevo");
  fflush(stdin);
  scanf("%d",&vector[i]);
  ce++; // ce deja de incrementarse cuando 'i==TAM'
}

So delete the variable ce and replace its occurrences with TAM .

And now, look at this part of your code:

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

There you are asking for the position where to insert the new element ... you already have pos , you do not need to iterate through the vector to see where to add it, that is, the following:

for(i=0;i<=ce;i++)
{   int aux;
    if(i==pos)
        vector[i]=dato_agregar;
}

And instead you should leave this:

vector[pos] = dato_agregar;

Now, with this you are not inserting the new value but replacing the old one with a new one. Inserting a value implies that the values that are after must be moved to make room for the new value:

for( int i=TAM-1; i>pos; i-- )
{
  vector[i] = vector[i-1];
}

vector[pos] = dato_agregar;

However, since the vector is full, the last value of the vector will be lost, since there is no room for it.

    
answered by 21.09.2018 / 11:56
source
0

It is not necessary to do all that, you read the position where you want to insert with this: scanf("%d",&pos).

The position where it is inserted is already stored in the pos variable.

Here you read the number to insert:

puts("Ingrese el elemento nuevo");
        fflush(stdin);
        scanf("%d",&dato_agregar);

The data to be added is saved in data_add.

The only thing you have to do to save the data in the position given is the following.

vector[pos]=datos_agregar.
    
answered by 20.09.2018 в 23:32