The c program given below is able to insert a new data inside of an array. First of all you will be asked to enter the limit. That means how much data you want to store in the array. For example: you want to store 5 data inside the array and your data are 1, 2, 3, 4 & 5.
So, when you will provide the limit 5, then you will be asked to provide the data. After providing the data 1, 2, 3, 4, & 5; now obviously a[0]=1, a[1]=2, a[2]=3, a[3]=4 and a[4]=5.
Now our goal is to insert a new data inside the array. For example: we want to insert 7 in the second position. Then the value of a[1] will be 7.
#include<stdio.h>
void main(){
//declaring integer type array and variables
int a[10], n, i, j, k, item;
//displaying message to enter the limit
printf("Enter a limit: ");
//storing the limit into the variable n
scanf("%d",&n);
//message to input the data
printf("\nEnter the data: ");
//for-loop to input data into the array
for(i=1; i<=n; i++){
scanf("%d",&a[i]);
}
//message before displaying the data
printf("\nData before inserting: ");
//for-loop to display the data
for(i=1; i<=n; i++){
printf("%d, ",a[i]);
}
//transfering data to j that stored into n
j=n;
//message to enter the position
printf("\n\nEnter position: ");
//storing data into k
scanf("%d",&k);
//while-loop to shift the data
while(j>=k){
a[j+1] = a[j];
j=j-1;
}
//message to enter the item
printf("\nEnter the item: ");
//storing item into the variable item
scanf("%d",&item);
//transferring the data to a[k] from item
a[k] = item;
//increasing the value of n
n = n+1;
//message before displaying the data
printf("\n\nThe data now: ");
//for-loop to display the data
for(i=1; i<=n; i++){
printf("%d, ",a[i]);
}
}

A complex c program, thanks for making it easier. Really good work yaar.
ReplyDelete