Home > Mobile >  I want to insert in a vector multiple values in a location
I want to insert in a vector multiple values in a location

Time:03-19

When I create a vector , let's say size of 5 , elements are 1,2,3,4,5 and I want to add at the location( for example index 2) the numbers 200 and 300 , the vector should look like 1 ,2 ,200,300,3,4,5.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int v[] ={1,2,3,4,5,6,7};
    int n = sizeof(v)/sizeof(int);
    int location,element;
    printf("Enter location:");
    scanf("%d",&location);
    printf("Enter element:");
    scanf("%d",&element);
    for(int i = n - 1; i >= location;i--){
        v[i   1] = v[i];
    }
    v[location] = element;
    for(int i = 0; i <= n;i  ){
        printf("%d ",v[i]);
    }
}

enter image description here

CodePudding user response:

You declared a fixed size array

int v[] ={1,2,3,4,5,6,7};

You can not enlarge it.

Thus this loop

for(int i = 0; i <= n;i  ){
    printf("%d ",v[i]);
}

invokes undefined behavior due to using the index with the value n that is outside the valid range of indices [0, n) for the source array.

You need to allocate the array dynamically and when you are going to add one more element you will need to reallocate the array.

Here is a demonstration program.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( void )
{
    size_t n = 7;

    int *v = malloc( n * sizeof( int ) );
    memcpy( v, ( int [7] ){ 1, 2, 3, 4, 5, 6, 7 }, n * sizeof( int ) );

    for ( size_t i = 0; i < n; i   )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    size_t location = 0;

    printf( "Enter location: " );
    scanf( "%zu", &location );

    if ( n < location ) location = n;

    int element = 0;

    printf( "Enter element: " );
    scanf( "%d",&element );

    int *tmp = realloc( v, ( n   1 ) * sizeof( int ) );
    
    if ( tmp != NULL )
    {
        v = tmp;

        memmove( v   location   1, v   location, ( n - location ) * sizeof( int ) );
        v[location] = element;
          n;
    }

    for ( size_t i = 0; i < n; i   )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    free( v );
}    

The program output might look like

1 2 3 4 5 6 7
Enter location: 2
Enter element: 200
1 2 200 3 4 5 6 7

CodePudding user response:

You need to loop like this

for(;;){
  int location,element;
  printf("Enter location:");

  scanf("%d",&location);

  if(location == -1)
     break;
  printf("Enter element:"); 
  scanf("%d",&element);
  for(int i = n - 1; i >= location;i--){
    v[i   1] = v[i];
  }
  v[location] = element;
}   

BUT you code is severely broken, you are adding to a vector thats of fixed size. you cannot do that

In this code

    for (int i = n - 1; i >= location; i--) {
        v[i   1] = v[i];
    }

n is the count of number of elements, so on the first loop you do

        v[n] = v[n-1]

ie

       v[7] = v[6]

well v[7] is off the end of the array (array indexes are 0 to 6) Thats very bad

  • Related