#include <stdio.h>
#define MAX 5
void main() {
int array[MAX] = {1, 2, 4, 5};
int N = 4; // number of elements in array
int i = 0; // loop variable
int index = 2; // index location to insert new value
int value = 3; // new data element to be inserted
// print array before insertion
printf(Printing array before insertion −\n);
for(i = 0; i < N; i++) {
printf(array[%d] = %d \n, i, array[i]);
}
// now shift rest of the elements downwards
for(i = N; i >= index; i--) {
array[i+1] = array[i];
}
// add new element at first position
array[index] = value;
// increase N to reflect number of elements
N++;
// print to confirm
printf(Printing array after insertion −\n);
for(i = 0; i < N; i++) {
printf(array[%d] = %d\n, i, array[i]);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。