【发布时间】:2021-03-30 12:31:22
【问题描述】:
请帮助任何人指出代码有什么问题
#include <iostream>
using namespace std;
int main() {
int n;
cout << "enter the size of array ";
cin >> n;
int arr[n];
cout << "enter numbers \n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; i < n; j++) {
if (arr[j] < arr[i]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
cout << "the numbers are\n" << endl;
for (int j = 0; j < n; j++) {
cout << arr[j] << " " << endl;
}
return 0;
}
我的问题是为什么这段代码没有对数组进行排序,也没有在输入后给出输出,它也显示分段错误?
【问题讨论】:
-
你试过用调试器运行它吗?你会看到故障发生在哪里。
标签: c++ segmentation-fault output selection-sort