+ 1

Reverse of the array element!! Why does one code(1st code) run n other(2nd code) does not give the desired result

#include <iostream> using namespace std; int main() { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } int i=0; int j=n-1; while (i<j){ int temp; temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; } for(int i=0;i<n;i++){ cout<<arr[i]<<" "; } return 0; } *****// Another code***** #include <iostream> using namespace std; int main() { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; } int i=0; while (i<j){ int temp; temp=arr[i]; arr[i]=arr[n-i-1]; arr[n-i-1]=temp; i++; } for(int i=0;i<n;i++){ cout<<arr[i]<<" "; } return 0; }

4th Apr 2022, 4:20 PM
Bone(Geremsa)
Bone(Geremsa) - avatar
3 odpowiedzi
+ 1
Vliz's ya sure gimme ur code too
4th Apr 2022, 5:05 PM
Bone(Geremsa)
Bone(Geremsa) - avatar