3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
SalvGi
  • 59
  • 9
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/211256/discussion-on-question-by-salvgi-how-to-reverse-order-of-a-set-array). – Samuel Liew Apr 09 '20 at 03:36

4 Answers4

2

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;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks Vlad I'll go and try that now! – SalvGi Apr 08 '20 at 20:08
  • Is there any way to do it without the std::swap tool? Your way totally works but we haven't learned about that in the class I'm taking and I'm guessing the professor doesn't want me using it for this exercise as it would defeat the purpose. I'm going to go look into std::swap and see if I can figure out how to code that without using it but at the same time basically using it if that makes sense – SalvGi Apr 08 '20 at 20:20
  • 1
    @SalvGi We, beginners, should help each other,:) – Vlad from Moscow Apr 08 '20 at 20:41
  • from Mowcow Also, what I ended up doing instead of std::swap just in case you wanna know is I made a simple swap function of my own. void swap(int&n1, int &n2) { int tmp; tmp=n1; n1=n2; n2=tmp; return; } – SalvGi Apr 08 '20 at 20:46
  • @SalvGi Yes, you can write such a function declared for example like void swap( int &a, int &b ); And within the body of the function you can place the code similar to what I showed in my answer. – Vlad from Moscow Apr 08 '20 at 20:47
2

If you're allowed to use the STL, you could just do

void reverseorder(int number[], int SIZE)
{
    std::reverse(number, number + SIZE);
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • I doubt he wants me using that otherwise this exercise would be much easier! Thanks for showing me that though I'm sure it'll come in handy in the future! – SalvGi Apr 08 '20 at 20:13
  • 1
    Confirm with your professor if that's actually disallowed. They might not stop you, just because it's easy. Easy is good :) – cigien Apr 08 '20 at 20:14
1

In this line:

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

This doesn't reverse it actually replaces incrementally with the number before it. By the time index i reaches the middle of the array, it will start repeating the last half part of the array again.

Khaled Ismail
  • 315
  • 1
  • 2
  • 17
0
void reverse(int *num,int size){
    for(int i = 0;i<(size)/2;i++){
        const int temp = num[i];
        num[i] = num[size-i-1];
        num[size-i-1] = temp;
    }
}

num is a pointer that points at the array, size is the size of the array, temp is a variable that holds a value to swap the first element with the last and the second element with the last-1 .... we need to repeat this (size of array)/2 times

Salmy
  • 47
  • 4