【发布时间】:2021-12-28 10:38:13
【问题描述】:
#include <iostream>
using namespace std;
#define max 10
int main()
{
int items[max];
int i,n,ub,location,data;
cout<<"Enter the number of items you want to enter: "<<endl;
cin>>n;
ub=n-1;
if(ub>=max-1){
cout<<"Array is full!"<<endl;
}
else{
cout<<"Enter "<<n<<" elements:"<<endl;
for(i=0;i<n;i++){
cin>>items[n];
}
cout<<"Enter the location where you want to insert:"<<endl;
cin>>location;
cout<<"Enter the data you want to insert in location "<<location<<endl;
cin>>data;
while(ub>=location){
items[ub]=items[ub-1];
ub--;
}
items[location]=data;
cout<<"The array after insertion is as follows: "<<endl;
for(i=0;i<n;i++){
cout<<items[n]<<endl;
}
}
}
对于 n=5 的输入数组,假设为 {1 ,2 ,3 ,4 ,5 },我想在数组的位置 2 插入 data=19,使得输出为 {1,19,3 , 4, 5} 但我得到的输出是 {5 ,5 ,5 ,5 ,5}。我该如何解决这个问题?
【问题讨论】:
-
当粘贴结果更相关时,请避免发布图片。这里的做法不好。
-
cin>>items[n];和后面对应的输出是错误的。使用[i]。