원하는 곳에 요소를 삽입할 수 있습니다. 즉, 시작 위치나 중간, 마지막 또는 배열의 아무 곳에나 삽입할 수 있습니다.
배열에 요소를 삽입한 후 위치 또는 인덱스 위치가 증가하지만 배열의 크기가 증가한다는 의미는 아닙니다.
요소를 삽입하는 데 사용되는 논리는 -
-
배열의 크기를 입력하세요
-
요소를 삽입할 위치를 입력하세요.
-
그런 다음 해당 위치에 삽입할 숫자를 입력하세요.
for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value;
for 루프를 사용하여 최종 배열을 인쇄해야 합니다.
프로그램
#include<stdio.h>
int main(){
int student[40],pos,i,size,value;
printf("enter no of elements in array of students:");
scanf("%d",&size);
printf("enter %d elements are:\n",size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that poition:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is\n");
for(i=0;i<=size;i++)
printf("%d\n",student[i]);
return 0;
} 출력
enter no of elements in array of students:6 enter 6 elements are: 12 23 34 45 56 67 enter the position where you want to insert the element:3 enter the value into that poition:48 final array after inserting the value is 12 23 48 34 45 56 67