cpp_study_five 2019-12-06 15:43 采纳率: 100%
浏览 186
已采纳

c++数组插入代码错误啊,我想知道原数组被插入数字哪去了(大佬帮看看)

#include
#include
#include
#include
#include
#include
using namespace std;
int main () // 数组插入
{
const int N = 99;
double power[N];//没有赋初值
int powerCount = 0;// 定义数组长度
double insertpower; //定义插入数
int insert_index = 0; //定义插入数下标
power[powerCount++] = 45; // 数组变化
power[powerCount++] = 32;
power[powerCount++] = 56;
power[powerCount++] = 90;
power[powerCount++] = 21;
double temp;
for (int i=0; i {
for (int j=0; j {
if (power[j] temp = power[j];
power[j] = power[j+1];
power[j+1] = temp;
}
}
}
for (int i=0; i {
cout cout }
cout cin >> insertpower;
//insert_index = powerCount;
for (int i=0; i {
if (insertpower > power[i]){ // 插入数代替数组原来数,并更换下标(直接替换掉数组原来数字)
power[i] = insertpower;
insert_index = i;
break; //一旦找到数,跳出循环
}
}
//cout << powerCount << endl;
for (int i=powerCount-1; i>=insert_index; i--){ //
power[i+1] = power[i];//数字复制到后一个元素
}
//cout << power[2] << endl;
power[insert_index+1] = insertpower;
powerCount++;
cout << "插入后:" << endl;
for (int i=0; i<powerCount; i++) // 循环输出数组
{
cout << power[i] << "\t" << endl;
}

return 0;

}

  • 写回答

1条回答

  • JonathanYan 2019-12-06 17:10
    关注

    这样写编译能通过吗
    for (int i=0; i {
    第一个循环里执行了这句,那执行前的power[i]去哪了,直接舍弃了么
    power[i] = insertpower;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置