有人能指出代码有什么问题吗
enter code here
在此处包含使用命名空间标准代码
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;
}
我的问题是为什么这段代码不对数组进行排序,也不给出输入后的输出,它还显示分段错误?
仔细看看这个for循环
for(int j=i+1;i<n;j++)
这是一个简单的错别字。
我建议在您的代码中添加一些空格,这样更容易阅读和发现类似的错误
for (int j = i + 1; i < n; j++)
好些了?