【发布时间】:2019-05-14 17:04:57
【问题描述】:
当插入数组时,我们知道索引从 0 开始。所以,如果我们想在位置 3 插入一个元素,我们必须在位置 2 输入输入。为了便于阅读,我想给出正确的位置,即位置 3 表示精确到 3,而不是 2。
这里是代码sn-p。
printf("In which position you want to enter the element? ");
scanf("%d",&k);
for (j=n; j>=k; j--)
{
array[j+1]=array[j];
}
printf("Which element do you want to insert? ");
scanf("%d", &item);
array[k]=item;
n++;
样本输出:
How many elements? 5
Enter the values
1
2
4
5
6
In which position you want to enter the element? 2
Which element do you want to insert? 3
After insertion the array is:
1
2
3
4
5
6
我希望位置在 3。
【问题讨论】:
-
for (j=n;如果 n 是数组中的元素数,我看起来是错误的,这意味着n-1是最后一个实际填充的元素,但只要 n+1 在数组边界内应该没问题。 -
如果您将
scanf放入k,并且您希望输入3的人引用索引2,您可以尝试减1。 -
如果数组中有 3 个元素,则在 0...3 范围内选择一个元素可用于将新元素插入 四个 位置之一,这是新元素所在位置的索引。
-
没有看到minimal reproducible example,我们只能猜测。
-
你的问题没有问题。
标签: c