Insert an element in array
Write a C program to insert an element in an array at the specified position. C program to insert an element in the array at a given position. The program should also print an error message if the insert position is invalid. Logic to insert an element in the array at a given position in the C program. If MAX_SIZE < new size then overflow condition!
for(i=size-1; i>=position-1; i--) {
array[i+1] = array[i];
}
array[position-1] = value;
size++;
Insertions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<stdio.h> #define MAX_SIZE 10 int main() {
int array[MAX_SIZE],size,i,position,value,Length =0;
/* Input size of the array */ printf("Enter the size of the array:\n"); scanf("%d",&size);
/* if MAX_SIZE < new size then overflow condition!*/ Length = sizeof(array)/sizeof(array[0]);
if(Length < size) { printf("overflow condition! Please enter size between 1 to %d", Length); } else {
/* Input elements in array */ printf("Enter %d elements:\n",size);
for(i=0; i<size; i++) { scanf("%d",&array[i]); }
/* Input position to insert */ printf("Enter the element position:\n"); scanf("%d",&position);
/* If position of element is not valid */ if(position>size ||position <=0) { printf("Please enter position between 1 to %d", size); return; } else {
/* Input value to insert */ printf("Enter the value:\n"); scanf("%d",&value);
for(i=size-1; i>=position-1; i--) { array[i+1] = array[i]; }
/* Insert new element at given position and increment size */ array[position-1] = value; size++;
/* Print array after insert */ printf("Array elements after insertion :\n"); for(i=0; i<size; i++) { printf("%d\n",array[i]); } }
}
return 0; }
|
Output
Enter the size of the array:
5
Enter 5 elements:
10 20 30 40 50
Enter the element position:
3
Enter the value:
100
Array elements after insertion :
10
20
100
30
40
50
Enter the size of the array:
15
overflow condition! Please enter size between 1 to 10
Insert a new element at the beginning:
for(i=size-1; i>=0; i--) {
array[i+1] = array[i];
}
array[0] = value;
size++;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<stdio.h> #define MAX_SIZE 10 int main() {
int array[MAX_SIZE],size,i,value;
/* Input size of the array */ printf("Enter the size of the array:\n"); scanf("%d",&size);
/* Input elements in array */ printf("Enter %d elements:\n",size);
for(i=0; i<size; i++) { scanf("%d",&array[i]); }
/* Input value to insert at the beginning*/ printf("Enter the value that you want to insert at the beginning:\n"); scanf("%d",&value);
for(i=size-1; i>=0; i--) { array[i+1] = array[i]; }
/* Insert new element at the beginning */ array[0] = value; size++;
/* Print array after insert at the beginning */ printf("\nArray elements after insertion at the beginning:\n"); for(i=0; i<size; i++) { printf("%d ",array[i]); }
return 0; }
|
Output
Enter the size of the array:
8
Enter 8 elements:
20 30 40 50 60 70 80 90
Enter the value that you want to insert at the beginning:
10
Array elements after insertion at the beginning:
10 20 30 40 50 60 70 80 90
Insert a new element at the ending:
array[size] = value;
size++;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include<stdio.h> #define MAX_SIZE 10 int main() {
int array[MAX_SIZE],size,i,value;
/* Input size of the array */ printf("Enter the size of the array:\n"); scanf("%d",&size);
/* Input elements in array */ printf("Enter %d elements:\n",size);
for(i=0; i<size; i++) { scanf("%d",&array[i]); }
/* Input value to insert at the ending*/ printf("Enter the value that you want to insert at the ending:\n"); scanf("%d",&value);
/* Insert new element at the ending*/ array[size] = value; size++;
/* Print array after insert at the ending */ printf("\nArray elements after insertion at the ending:\n"); for(i=0; i<size; i++) { printf("%d ",array[i]); }
return 0; }
|
Output
Enter the size of the array:
8
Enter 8 elements:
10 20 30 40 50 60 70 80
Enter the value that you want to insert at the ending:
90
Array elements after insertion at the ending:
10 20 30 40 50 60 70 80 90