• Register
100 points
5

Index insertion of an element in an array means at which index of array we want to insert an element  in given array.Here in this code i inserted element 15 at index 3.

Code logic

First we check that size is not greater than capacity of given array which 100 here.We are taking size here because after inserting an element in array then array size will increase so we can insert an element until it equals or less than capacity of array.Then it will take index and decrease by 1 because we need to put that element before the index so that it will become the index which passes in our function. When an element get inserts then put the element which is present on index 3 into next nearby index i.e 4.That’s it for this program.

Code 

#include<stdio.h>
void display(int arr[],int n){
    for(int i=0;i<n;i++){
        printf("%d",arr[i]);
    }
    printf("\n");
}
int indInsertion(int arr[],int index,int element,int capacity,int size){
    if(size>=capacity){
        return -1;
    }
    for(int i=size-1;i>=index;i--){
        arr[i+1]=arr[i];
    }
    arr[index]=element;
    return 1;
}
int main(){
    
    int arr[100]={3,7,5,18,22};
    int size=5;
    int index=3;
    int element=15;
    indInsertion( arr,index,element,100,size);
    size +=1;
    display(arr,size);
    return 0;
}

Output

3  7   5   15   18   22

 

100 points
5