-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathArray_Insert.c
46 lines (40 loc) · 970 Bytes
/
Array_Insert.c
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
#include<stdio.h>
#define MAX 100
void insert(int arr[], int n, int item, int index);
void display(int arr[], int n);
main(){
int arr[MAX], i, n, item, pos;
// Input
printf("Enter size of array: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("Enter element %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEnter number to insert: ");
scanf("%d", &item);
printf("\nEnter position at which to insert: ");
scanf("%d", &pos);
if(pos <= n){
insert(arr, n, item, pos-1);
n++;
printf("\nNew Array: ");
display(arr, n);
}
else{
printf("\nINVALID INPUT! Position more than size of array.");
}
}
void insert(int arr[], int n, int item, int index){
int i;
for(i = n; i > index; i--){
arr[i] = arr[i-1];
}
arr[index] = item;
}
void display(int arr[], int n){
int i;
for(i = 0; i < n; i++){
printf("%d ", arr[i]);
}
}