Reverse of the array element!! Why does one code(1st code) run n other(2nd code) does not give the desired result | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
1 Answer
+ 1
Vliz's ya sure gimme ur code too
4th Apr 2022, 5:05 PM
Bone(Geremsa)
Bone(Geremsa) - avatar