Reversing an arrays elements and outputting as a pointer returning

Here is my objective and I am stuck on making the elements go in reverse, "Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array." I believe we cannot use std shortcuts but have an actual algorithm that sorts the elements in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>

using namespace std;

int *reverseArray(/*in*/ const int[] , /*in*/ int); //a value returning function that returns a pointer to an array
void showArray(/*in*/const int[],/*in*/ int);     // a void function to display array elements


int main()
{
    const int ARRAY_SIZE = 10;
    int original[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10};
    int *pointer = original;

    reverseArray(pointer, ARRAY_SIZE);
    showArray(pointer, ARRAY_SIZE);

    return 0;
}

int *reverseArray(const int array[], int size)
{
    int originalArray[size] = {1,2,3,4,5,6,7,8,9,10};
    int *pointer = originalArray;
    int temp;

    for(int track = 0; track < size; track++)
    {
        pointer[track] = originalArray[track];
    }


    return pointer;
}

void showArray(const int array[], int size)
{
    for(int track = 0; track < size; track++)
    {
        cout << array[track] << " ";
    }
}
> have an actual algorithm that sorts the elements in descending order.
you were not asked to sort, you were asked to reverse
if the input is {42, 13, 54} the output would be {54, 13, 42}

> The function should return a pointer to the new array
that's a can of worms, so I would consider a bad exercise
If you can't use std::vector and don't know (or don't want to deal) with dynamic memory allocation then do something like
1
2
3
4
5
int *reverseArray(const int array[], int size){
   static int reversed[100] = {}; //blind trust
   //your reverse algorithm
   return reversed;
}



About your code, you have some glaring conceptual errors
it seems that you don't understand functions parameters, return values, or scope
variables declared inside a function are local to that function, `pointer' in reverseArray() has no relationship with `pointer' in main()

About your algorithm, well you are doing nothing, you are not reversing but just copying
don't know if it was because you didn't understood the question, you have issues coding or you have no idea how to do it
try to write a pseudocode.
Last edited on
i took a step back and rewrote it i'm just stuck on the value returning function i may be overthinking it,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <iomanip>

using namespace std;

int *reverseArray(/*in*/ const int[] , /*in*/ int); //a value returning function that returns a pointer to an array
void showArray(/*in*/const int[],/*in*/ int);     // a void function to display array elements
void copyArray(/* inout */ int [],/* inout */ int[],/* in */ int);


int main()
{
    const int ARRAY_SIZE = 10;
    int original[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10};
    int copied[ARRAY_SIZE];

    copyArray(original,copied,ARRAY_SIZE);
    reverseArray(copied, ARRAY_SIZE);
    showArray(copied, ARRAY_SIZE);


    return 0;
}

void copyArray(/* inout */ int array1[],/* inout */ int array2[],/* in */ int size)
{
    for(int i = 0; i < size; i++)
    {
        array2[i] = array1[i];
    }
}

int *reverseArray(const int array[], int size)
{
    //Descending order code
}

void showArray(const int array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }
}
1
2
3
4
5
6
7
8
int *reverseArray( int const A[], int size )
{
   int *p = new int[size];
  
   // You write some code to set all p[i] in terms of A[j]

   return p;
}



From int main()
1
2
3
   int *R = reverseArray( A, size );
   showArray( R, size );
   delete [] R;
Topic archived. No new replies allowed.