Q. Write a C program to insert or add new element in array, give with output of program.
Ans.
/*c program for insert new element in array*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
int arr[SIZE];
int i,index,tmp,num;
printf("Enter total number of elements in array : ");
scanf("%d", &num);
for(i=0; i<num; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d", &arr[i]);
}
printf("\nEnter element number before to insert new element : ");
scanf("%d", &index);
printf("Enter new element : ");
scanf("%d", &tmp);
for(i=num-1; i>=index-1; i--)
arr[i+1] = arr[i];
arr[index-1] = tmp;
printf("\n-- After insertion an element, new array list --\n\n");
for(i=0; i<=num; i++)
printf("\t%d\n",arr[i]);
getch();
return 0;
}
/**************** Output ******************/
Related programs:
Ans.
/*c program for insert new element in array*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
int arr[SIZE];
int i,index,tmp,num;
printf("Enter total number of elements in array : ");
scanf("%d", &num);
for(i=0; i<num; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d", &arr[i]);
}
printf("\nEnter element number before to insert new element : ");
scanf("%d", &index);
printf("Enter new element : ");
scanf("%d", &tmp);
for(i=num-1; i>=index-1; i--)
arr[i+1] = arr[i];
arr[index-1] = tmp;
printf("\n-- After insertion an element, new array list --\n\n");
for(i=0; i<=num; i++)
printf("\t%d\n",arr[i]);
getch();
return 0;
}
/**************** Output ******************/
Screen shot of insert element in array |
Related programs: