-7
當用戶輸入的數組大小爲8時,爲什麼此代碼等待用戶輸入10個整數?它提供了分段故障時10個整數是used.`簡單插入排序中的分段錯誤?
#include <iostream>
using namespace std;
int main()
{
int x, a[x];
cout << "enter the size of array" << endl;
cin >> x;
cout << "enter the elements" << endl;
for (int j = 0; j < x; j++)
cin >> a[j];
for (int i = 1; i < x; i++) {
for (int k = 0; k < i; k++) {
if (a[i] < a[k])
swap(a[i], a[k]);
else
continue;
}
}
for (int m = 0; m < x; m++)
cout << a[m];
}
因爲您聲明'a [x]'_before_,所以您知道'x'的值是... – Alnitak
我總是說使用最高的編譯器警告開關.... –