使用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 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短