调用函数后无输出:C++随机数排序打印问题排查
C++排序函数修改后打印无输出问题排查与修复
我正在开发一个C++程序,功能是生成随机数并打印,第二部分需要打印升序排序后的随机数。调整数组升序排序函数后,调用print函数完全无输出。之前第二部分会输出但全是0,现在改完彻底没输出,没法验证修复效果。
原始代码
#include <iomanip> #include <iostream> #include <cstdlib> using namespace std; #define lowerbound 10.0 #define upperbound 950.0 #define min_values 75 #define max_values 150 #define max_val_file 40 #define max_output 12 double randDouble(); int buildRandom(double array[]); void print(string, double array[], int); void sort(double array[], int); int main() { srand(19); int num_vals; double random_array[max_values]; double sorted_array[max_values]; string title1 = "Random Values"; string title2 = "Sorted Random Values"; num_vals = buildRandom(random_array); cout << "There are " << num_vals << " values in the first array" << '\n'; print(title1, random_array, num_vals); sort(random_array, num_vals); cout << '\n'; print(title2, sorted_array, num_vals); } double randDouble() { int random_integer = rand(); double random_value; random_value = lowerbound + (random_integer / (RAND_MAX / (upperbound - lowerbound))); return random_value; } int buildRandom(double random_array[]) { int num_vals = min_values + rand() % (max_values - min_values + 1); int i; double random_num; for (i = 0; i < num_vals; ++i) { random_num = randDouble(); random_array[i] = random_num; } return num_vals; } void print(string title, double random_array[], int num_vals) { int i; cout << '\n' << title << '\n' << '\n'<< setw(8); for (i = 0; i < num_vals; ++i) { cout << setprecision(3) << fixed << random_array[i] << setw(9); if ((i + 1) % max_output == 0) { cout << '\n' << setw(8); } } } void sort (double sorted_array[], int numberOfValues) { int top = 0; int x, SSF, PTR; double swap; double last = numberOfValues - 1; for (top = 0; top < last; ++top) { PTR = top; SSF = top; for (x = 0; PTR < last; ++x) { if (sorted_array[PTR] < sorted_array[SSF]) { SSF = PTR; } } swap = sorted_array[SSF]; sorted_array[SSF] = sorted_array[top]; sorted_array[top] = swap; } }
问题根源
- 死循环导致程序卡住:
sort函数内层循环for (x = 0; PTR < last; ++x)中,PTR的值始终等于top,没有递增,导致循环条件永远成立,程序陷入死循环,根本无法执行到后续的print语句。 - 未初始化数组被打印:
main函数中打印的sorted_array从未被赋值,之前输出0是因为栈数组的默认初始化值,现在死循环直接阻断了打印流程。 - 整数除法丢失精度:
randDouble函数中random_integer / (RAND_MAX / (upperbound - lowerbound))是整数除法,会导致生成的随机数精度严重丢失。 - 排序逻辑遍历不完整:内层循环没有遍历
top之后的所有元素,无法正确找到最小值的位置。
修复方案
1. 修复排序函数的死循环与遍历逻辑
将sort函数的内层循环改为遍历top之后的所有元素,去掉无用的x变量:
void sort (double array[], int numberOfValues) { int top; int SSF, PTR; double swap; int last = numberOfValues - 1; // 数组索引用int类型更合理 for (top = 0; top < last; ++top) { SSF = top; // 遍历top之后的所有元素,寻找最小值的位置 for (PTR = top + 1; PTR <= last; ++PTR) { if (array[PTR] < array[SSF]) { SSF = PTR; } } // 交换当前top位置与最小值位置的元素 swap = array[SSF]; array[SSF] = array[top]; array[top] = swap; } }
2. 正确关联排序后的数组
在main函数中,排序前将random_array的内容复制到sorted_array,确保排序的是目标数组,且打印的是已排序的内容:
// main函数中排序前添加数组复制逻辑 for (int i = 0; i < num_vals; ++i) { sorted_array[i] = random_array[i]; } sort(sorted_array, num_vals);
3. 修正随机数生成的精度问题
将randDouble中的整数除法改为浮点除法,避免精度丢失:
double randDouble() { int random_integer = rand(); double random_value; random_value = lowerbound + static_cast<double>(random_integer) / RAND_MAX * (upperbound - lowerbound); return random_value; }
4. 优化打印格式
在print函数末尾添加换行,避免输出内容粘在一起:
void print(string title, double random_array[], int num_vals) { int i; cout << '\n' << title << '\n' << '\n'<< setw(8); for (i = 0; i < num_vals; ++i) { cout << setprecision(3) << fixed << random_array[i] << setw(9); if ((i + 1) % max_output == 0) { cout << '\n' << setw(8); } } cout << '\n'; // 添加末尾换行 }
修复后效果
程序会正常生成随机数,打印原始随机值,然后打印升序排序后的随机值,无死循环,随机数精度正常。
内容的提问来源于stack exchange,提问作者1tylers
相关产品推荐
相关产品推荐

