0

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;
}
3
  • 2
    sizeof(arr) is returning the size of an int pointer, not the number of elements in your array. Commented Feb 24, 2012 at 2:00
  • @SamDufel - In this instance sizeof(arr) (since arr is an array in the context when sizeof is used) would produce 5*sizeof(int). Commented Feb 24, 2012 at 2:05
  • 1
    sizeof(arr) is returning the size of the array in bytes, which is sizeof(int) * 5. It's NOT returning the number of elements in the array. For that you'd need to say sizeof(arr) / sizeof(arr[0]) Commented Feb 24, 2012 at 2:05

6 Answers 6

3

Use sizeof(arr)/sizeof(arr[0]) instead of sizeof(arr).

sizeof(arr) gives the total size of the array. sizeof(arr[0]) is the size of one array element (all elements have the same size). So sizeof(arr)/sizeof(arr[0]) is the number of elements.

Sign up to request clarification or add additional context in comments.

2 Comments

sizeof (arr) / sizeof (arr [0]) is a more general version of that. I tend to like the generality better. But either is good for the purpose of passing an array to a function.
yes, I agree this sizeof (arr) / sizeof (arr [0]) code is better
3

An optimized answer to the question would be using reverse () from STL if you are allowed to use it:

std::reverse 

http://www.sgi.com/tech/stl/reverse.html

int main()
{
    int arr[] = {6,41,12,5,2};
    cout<<"The rev of {6,41,12,5,2}"<<endl;
    reverse(arr, arr + 5);
    copy(arr, arr + 5, ostream_iterator<int>(cout, ", "));
}

2 Comments

I'd guess that this is a homework problem and so STL is out of the question. But yeah, normally std::reverse is the answer.
Well maybe he cares. Not to say reverse isn't the best solution, but the "Who cares?" part could easily have been left out of this answer.
1

sizeof return the size in bytes. In your example, if sizeof(int) = 4, it returns 20.

Comments

0

Because you're using an array, you have to keep the size of the array handy as well. sizeof computes the size of a value in memory, in this case the size of all the memory used to represent arr. You can do sizeof(arr)/sizeof(int) to get the number of elements in an array. This makes sense because it's taking the total size of the array and dividing it by the size of an element in the array. Beware however that this only works for arrays (int arr[4] = {6,41,12,5,2};). If it's a pointer to a heap-allocated array via something like int* i = new int[4]; you'll need to keep the size of the array hanging around.

Also, you're calling your reverse function from within a cout<< call, which will print the function's return value (in this case it's hard-coded to 0).

It also turns out there is a function in the C++ standard library (std::reverse) that can do this.

Comments

0

If I may speak subjectively and in an off-topic manner about your approach, it is very un-C-like. My personal favorite way to reverse an array goes like this:

void reverse(int *a, int n)
{
   int *p = a, *q = a + n - 1;

   while (p < q)
   {
      int swap = *p;
      *p++ = *q;
      *q-- = swap;
   }
}

// Usage:

   int a [] = { /* ... */ };

   reverse(a, sizeof(a)/sizeof(*a));

Of course, since your question is tagged c++, there's always std::reverse().

Comments

0

Sizeof operator return the one extra (arrayLength + 1) here 6 will return when passs 6 it store in a when a-1 you get 5 but array index start from 0 length-1 that from 0 to 4 here i pointing to index 5 that is not last element last+1 that why you got garbage value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.