我试图递归地反转数组的元素。我可以在函数中拥有的唯一参数是数组和大小。这是我到目前为止所做的,但在交换时遇到了麻烦。如何修复我的输出?例如,当我输入 1 2 3 时,元素会反转为 2 3 1
//Recursive function for Reversing array
void reverse_arr(int a[],int size){
if(size ==0){
return ;
}
else{
int temp;
int i= 0;
temp = a[i];
a[i]= a[size-1];
a[size -1] = temp;
reverse_arr(a, size-1);
}
}
int main() {
int a[100];
int size ;
cout<<"Enter the size of the array: "<<endl;
cin>>size;
cout<<"Enter the elements of the array: "<<endl;
for(int i = 0; i<size; i++){
cin>>a[i]; }
for(int i = 0; i<size; i++){
cout<<a[i]<<" ";} …Run Code Online (Sandbox Code Playgroud)