Saturday, August 18, 2018

ARRAY INSERTION USING FUNCTIONS

  • Problem Description

    Write a program to insert an element in the array

    Input 1: Size of the Array
    Input 2: The number of elements
    Input 3: The Place index where the elements needs to be inserted
    Inout 4: The Element to be inserted
  • CODING ARENA::
  • #include <stdio.h>
    int main()
    {
      int pos,i,n,value;
      scanf("%d",&n);
      int arr[n];
      for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
      scanf("%d",&pos);
      scanf("%d",&value);
      for(i=n-1;i>=pos-1;--i)
        arr[i+1]=arr[i];
     
      arr[pos-1]=value;
      if(pos>n)
        printf("Sorry Invalid Location");
      else
      {
        for(i=0;i<=n;i++)
        printf("\n%d",arr[i]);
      }
      return 0;
    }
  • Test Case 1

    Input (stdin)
    5
    
    1 2 3 4 5
    
    4
    
    10
    
    
    Expected Output
    1
    
    2
    
    3
    
    10
    
    4
    
    5
  • Test Case 2

    Input (stdin)
    3
    
    1 2 3
    
    4
    
    1
    
    
    Expected Output
    Sorry Invalid Location

No comments:

Post a Comment