如何可视化这些未出现的数字.(C++)
How to visualize these numbers that are not appearing. (C++)
我有一个练习,用于我的编程入门课,我对此有疑问(代码将在这里(。任务非常简单,只需合并两个数组并按升序对它们进行排序。现在,我把所有东西都放下了,但是当我运行代码时,其中两个数字不可见(最后两个(。完整程序的代码如下。
#include <iostream>
using namespace std;
void mergeSortedArrays(const int array1[], int size1, const int array2[], int size2, int result[])
{
int tmp;
bool swap;
int sizefull = size1 + size2;
int loops = 0;
while (loops < size1)
{
result[loops] = array1[loops];
loops++;
}
while (loops < sizefull)
{
result[loops] = array2[loops];
loops++;
}
do {
swap = false;
for (int i = 0; i < sizefull - 1; i++)
{
if (result[i] > result[i + 1])
{
tmp = result[i + 1];
result[i + 1] = result[i];
result[i] = tmp;
swap = true;
}
}
} while (swap);
}
int main()
{
const int SIZE1 = 3, SIZE2 = 4;
const int sizefull = SIZE1 + SIZE2;
int array1[SIZE1] = { 3, 8, 9 };
int array2[SIZE2] = { -7, -2, 0, 7 };
int result[sizefull];
mergeSortedArrays(array1, SIZE1, array2, SIZE2, result);
for (int i = 0; i < sizefull; i++)
{
cout << result[i] << " ";
}
cout << endl;
return 0;
}
单击此处查看程序显示的内容。我只是想知道顶部的"mergeSortedArrays"函数是否有任何问题,可能是在我将值插入"结果"数组的部分。感谢大家的帮助或伸出援手。:)
代码在"mergeSortedArrays"函数中存在问题。
您的第二个循环(mergeSortedArrays
while
(:
while (loops < sizefull)
{
result[loops] = array2[loops];
loops++;
}
使用错误的索引进行array2
,即 array2
只有size2
个项目,但您使用最多可以sizefull - 1
的计数器oops
。
我的建议是再添加一个计数器,例如:
int cnt = 0;
while (loops < sizefull)
{
result[loops] = array2[cnt++];
loops++;
}
或者改用for
:
for (int cnt = 0; loops < sizefull; cnt++)
{
result[loops] = array2[cnt];
loops++;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 在将数字随机生成为数组期间从内存输出随机数的数组