使用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
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下