Search code examples
c++arraysreversefunction-definition

How to reverse order of a set array


This is an exercise for my class, and I'm not sure how to go about the function that needs to be made to reverse order.

#include <iostream>
#include <iomanip>

void reverseorder(int[], int);
void printout(int[], int);

const int SIZE = 10;

int main()
{
int number[SIZE] = {10, 15, 20, 25, 30, 35, 40, 45, 50, 55};

reverseorder(number, SIZE);
printout(number, SIZE);
}

void reverseorder(int number[], int SIZE)
{
    for (int i = 0; i < SIZE; i++)
    {

        number[i] = number[SIZE - i];
        return;
    }
}

void printout(int number[], int SIZE)
{
    for (int i = 0; i < SIZE; i++)
    {
        std::cout << number[i] << std::setw(5);
    }
    std::cout << std::endl;
}

I know for a fact that fnc void reverseorder(int number[], int SIZE) is not correct, because that's the only thing left that needs to be done. If you know the answer but don't want to give it to me straight up then any hints would be very much appreciated too! Thanks guys

EDIT: Currently the output is: SalvGis-MBP:c++ programming$ ./a.out 503709838 15 20 25 30 35 40 45 50 55

But I want it to be 55 50 45 40 35 30 25 20 15 10

Not sure why the 503709838 is being produced. Somebody mentioned the first iteration being broken, that probably explains it.


Solution

  • To reverse an array what you need is either to use the standard function std::swap or to write such a function yourself.

    This function

    void reverseorder(int number[], int SIZE)
    {
        for (int i = 0; i < SIZE; i++)
        {
    
            number[i] = number[SIZE - i];
            return;
        }
    }
    

    does not swap elements of the array. So for example in the first iteration due to this statement

        number[i] = number[SIZE - i];
    

    the value of the element number[0] will be simply lost. And moreover there is used non-existent element of the array number[SIZE - i] when i is equal to 0.

    You need to swap two halves of the array.

    Also the call of std::setw in this statement

    std::cout << number[i] << std::setw(5);
    

    does not make sense. This call should be placed before the outputted expression number[i].

    Pay attention to that the both functions should have the second parameter of the type size_t and the first parameter of the function that outputs array shall have the qualifier const because within the function the used array is not changed.

    Here you are.

    #include <iostream>
    #include <utility>
    
    void reverseorder( int a[], size_t n )
    {
        for ( size_t i = 0; i < n / 2; i++ )
        {
            std::swap( a[i], a[n-i-1] );
        }
    }
    
    std::ostream & printout( const int a[], size_t n, std::ostream &os = std::cout )
    {
        for ( size_t i = 0; i < n; i++ )
        {
            os << a[i] << ' ';
        }
    
        return os;
    }
    
    
    int main() 
    {
        int number[] = {10, 15, 20, 25, 30, 35, 40, 45, 50, 55};
        const size_t N = sizeof( number ) / sizeof( *number );
    
        printout( number, N ) << '\n';
    
        reverseorder( number, N );
    
        printout( number, N ) << '\n';
    
        return 0;
    }
    

    The program output is

    10 15 20 25 30 35 40 45 50 55 
    55 50 45 40 35 30 25 20 15 10 
    

    If you may not use std::swap then in the loop write for example

        for ( size_t i = 0; i < n / 2; i++ )
        {
            int tmp = a[i];
            a[i] = a[n-i-1];
            a[n-i-1] = tmp;
        }