0% found this document useful (0 votes)
15 views1 page

C Program for Array Insertion

This C code defines functions to display the contents of an integer array, insert an element into the array at a given index if space is available, and test the functions by displaying an initial array, inserting an element, and displaying the modified array. The display function traverses the array and prints each element, while indInsertion shifts elements over and inserts the new element if the array is not full.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

C Program for Array Insertion

This C code defines functions to display the contents of an integer array, insert an element into the array at a given index if space is available, and test the functions by displaying an initial array, inserting an element, and displaying the modified array. The display function traverses the array and prints each element, while indInsertion shifts elements over and inserts the new element if the array is not full.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<stdio.

h>

void display(int arr[], int n){


// Code for Traversal
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int indInsertion(int arr[], int size, int element, int capacity, int index){
// code for Insertion
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] = {7, 8, 12, 27, 88};
int size = 5, element = 45, index=1;
display(arr, size);
indInsertion(arr, size, element, 100, index);
size +=1;
display(arr, size);
return 0;
}

You might also like