C Programming code for Inserting no. in 1D Array – KLE Tech

Write a Modular C Programming Code for Inserting a value to 1D Array?

CODE:

#include<stdio.h>
void readArray(int[],int);
void insert(int A[],int n);
int main()
{
int n,occ,key;
int arr[100];
printf("Enter the size of Array: ");
scanf("%d",&n);
printf("Enter the elements of array: \n");
readArray(arr,n);
insert(arr,n);
}
void readArray(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void displayArray(int arr[],int n)
{
int i;
for(i=0;i<n+1;i++)
{
printf("%d ",arr[i]);
}
}
void insert(int arr[],int n)
{
int i,j,pos,value;
printf("Enter the position index : ");
scanf("%d",&pos);
printf("Enter the value into that position: ");
scanf("%d",&value);
for(i=n-1;i>=pos-1;i--)
arr[i+1]=arr[i];
arr[pos-1]= value;
printf("Final array after inserting the value is\n");
displayArray(arr,n);}

OUTPUT

Enter the size of Array: 5
Enter the elements of array:
2
9
5
6
4
Enter the position index : 2
Enter the value into that position: 1
Final array after inserting the value is
2 1 9 5 6 4
Process returned 0 (0x0) execution time : 12.027 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!