
我们可以在任意位置插入元素,这意味着我们可以在数组的起始位置、中间、最后或任意位置插入。
在数组中插入元素后,位置或索引位置增加,但并不意味着数组的大小增加。
插入元素的逻辑是−
输入数组的大小
立即学习“C语言免费学习笔记(深入)”;
输入要插入元素的位置
接下来输入您要在该位置插入的数字
for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value;
应使用 for 循环打印最终数组。
程序
现场演示
#includeint 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:",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
"); for(i=0;i<=size;i++) printf("%d
",student[i]); return 0;}
输出
enter no of elements in array of students:6enter 6 elements are:122334455667enter the position where you want to insert the element:3enter the value into that poition:48final array after inserting the value is12234834455667
以上就是使用C语言在数组中插入元素的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1443898.html
微信扫一扫
支付宝扫一扫