使用C语言在数组中插入元素
我们可以在任何我们想要的地方插入元素,这意味着我们可以在开始位置或中间或最后或数组中的任何位置插入元素。
在数组中插入元素后,位置或索引位置增加,但并不意味着数组的大小增加。
用于插入元素的逻辑是 -
输入数组的大小
输入要插入元素的位置
接下来输入要在该位置插入的数字
for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value;
最终数组应使用for循环打印。
程序
#include输出结果int main(){ int student[40],pos,i,size,value; printf("在学生数组中输入元素数:"); scanf("%d",&size); printf("enter %d elements are:\n",size); for(i=0;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; }
在学生数组中输入元素数:6 enter 6 elements are: 12 23 34 45 56 67 输入要插入元素的位置:3 将值输入该点:48 final array after inserting the value is 12 23 48 34 45 56 67
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短