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!