my aim is to reverse an array 3,12,2,1 to 1,2,12,3. when i run this code i get garbage before my actually result. i can't seem to see where the problem is please assit
#include<iostream>
using namespace std;
int rev (int arr[], int a){
//int r;
for(int i =a-1; i>=0; i--){
cout<<arr[i]<<" ";
}
return 0;
}
int main(){
int arr[] = {6,41,12,5,2};
cout<<"The rev of {6,41,12,5,2}"<<endl;
cout<<rev(arr, sizeof(arr))<<endl;
system("pause");
return 0;
}
sizeof(arr)is returning the size of an int pointer, not the number of elements in your array.sizeof(arr)(sincearris an array in the context whensizeofis used) would produce5*sizeof(int).sizeof(arr)is returning the size of the array in bytes, which issizeof(int) * 5. It's NOT returning the number of elements in the array. For that you'd need to saysizeof(arr) / sizeof(arr[0])