pass array w pointer & "new operator" help plz!

K, here's my assignment:

"Write a function named reverse 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. Use the
new operator to create the new array. The function should return a pointer to the new array. Demonstrate the
function in a complete program. Use the following for the original array data: 5, 12, -2, 8, 47, 12, 81, 0"

here's my code:

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
#include <iostream>
#include <algorithm>//not certain that I need this?
using namespace std;//taboo? I see many criticisms of this over std::cout etc.
void reverse();//function prototype
int main()
{
        int array1[8] = { 5, 12, -2, 8, 47, 12, 81, 0 };//original array
        int reverseArray1[8];
	
	int *numPtr = nullptr;//not certain on this part, initialized pointer?
	numPtr = array1;//point it at original array

	reverse();//this is where i'll call the function 
	for (int index = 0; index < 8; index++)	//display reversed array
	{
		cout << reverseArray1[index] << endl;
	}

	system("pause");//taboo? I see many complaints about system pause
	return 0;
}

void reverse()//reverse function
{	
	for (int index = 0; index < 8; index++) //loop copy array reversed
	{	
		reverseArray1[index] = array1[7 - index];
	}
	return;//return an array of values? use a pointer? 
}


1. I'm having a hard time understanding how to pass the array from main to function and returning the new array back to main, I'm guessing this is where the pointers come in?

2. I'd like to figure out #1 first before I get into the "new operator," I assume it has something to do with the pointer, memory & return, I'm not certain if it will actually replace my first array without having to initialize a second one? it might change the whole code but I'm just worried about understanding, this code took me 5 minutes to write, it's one of many versions that I've started and scrapped bc I keep using the wrong tactics here. I feel like if I understood these two issues just a little better I'd be ok (for intro programming class)

I'm honestly not trying to get anyone to do my work for me either, but once I see how something works, it usually clicks for me. I actually enjoy doing my homework in this class. Any help would be greatly appreciated!
#include <algorithm>//not certain that I need this?

You would need this if you were to use the reverse function provided as part of C++. If you were to do that, the program would become very simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iterator>
#include <algorithm> // now you need this

int main()
{
    int array1[8] = { 5, 12, -2, 8, 47, 12, 81, 0 };//original array
    int reverseArray1[8];
    
    std::reverse_copy(std::begin(array1), std::end(array1), std::begin(reverseArray1));
    for(int n: reverseArray1)    
        std::cout << n << '\n';
}


online demo: http://coliru.stacked-crooked.com/a/68fe30400eb6e076

(and if you were to write your own reverse_copy, it would be educational to follow the interface of the one from <algorithm>)
Last edited on
1. I'm having a hard time understanding how to pass the array from main to function and returning the new array back to main, I'm guessing this is where the pointers come in?

Yes, pointers are helpful here.

Here's an example that creates a new array with the same size and equivalent content:
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
// http://ideone.com/Bnk7w5
#include <iostream>

int* copy_array(const int* arr, std::size_t size)  // const because we don't intend to modify the contents of arr
{
    int* copy = new int[size];
    for (std::size_t i=0; i<size; ++i)
        copy[i] = arr[i];

    return copy;
}

void print(const int* arr, std::size_t size)
{
    for (std::size_t i=0; i<size; ++i)
        std::cout << arr[i] << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    const std::size_t size = 8 ;
    int array1[size] = { 5, 12, -2, 8, 47, 12, 81, 0 };

    int * array1_copy = copy_array(array1, size) ;

    print(array1, size);
    print(array1_copy, size);

    delete [] array1_copy;
}
Topic archived. No new replies allowed.