हम एलिमेंट्स को जहां चाहें इन्सर्ट कर सकते हैं, जिसका अर्थ है कि हम या तो शुरुआती स्थिति में या मध्य में या अंत में या एरे में कहीं भी इन्सर्ट कर सकते हैं।
एरे में एलिमेंट इन्सर्ट करने के बाद, पोजीशन या इंडेक्स स्थान बढ़ जाता है लेकिन इसका मतलब यह नहीं है कि एरे का आकार बढ़ रहा है।
एलिमेंट इन्सर्ट करने के लिए उपयोग किया गया लॉजिक निम्नलिखित है:
एरे का आकार दर्ज करें
वह पोजीशन दर्ज करें जहां आप एलिमेंट इन्सर्ट करना चाहते हैं
अगला वह नंबर दर्ज करें जिसे आप उस पोजीशन में इन्सर्ट करना चाहते हैं
for(i = size - 1; i >= pos - 1; i--)
student[i + 1] = student[i];
student[pos - 1] = value;
फाइनल एरे को फॉर लूप का उपयोग करके प्रिंट किया जाना चाहिए।
प्रोग्राम
#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 position:");
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 position: 48 final array after inserting the value is 12 23 48 34 45 56 67