EDIT: I figured it out. I do not know how to delete threads. Sorry.

I need to return a pointer to a new array. The new array must be the same as the old one, just in reverse. I have it all done except for one little part. I can't seem to return the the pointer to show all the elements. I get 50, after that is all a bunch of random numbers being printed out. Any quick fixes in order for it to print out 50 40 30 20 10. Thanks

#include <iostream>
using namespace std;



int *reverseArray(int array1[], int const size);
int main() {//start main

	const int size = 5;
	int array1 [size] = {10,20,30,40,50};
	int *newArray;
	
	int count;
	int x;



	newArray = reverseArray(array1, size);
	for (count = 0; count < size; count ++){//start for

	cout << newArray[count];

	}//end for
   cin >> x;
}//end main



int *reverseArray(int array1[], int const size)  {//start function
	int count;
	int i=0;
	int newArray[5];
	int *numptr = newArray;
	cout << "This is the orginal array: " ;
	 
	for(count = 0; count < size; count++){//start for
		
		cout << array1[count] << " ";
		
	}//end for
	while (count > 0 ) {//start while
	numptr[i] = array1[count -1];
	
	cout << numptr[i];
	
	count--;
	i++;
	
	}//end while
	 
		return numptr; 
}// end function

Edited 6 Years Ago by Rez11: n/a

I posted a solution in your original thread and what you have there does not do what you think it does.

You need to use the new operator since NewArray is being declared locally in your function and therefore is deleted at the end of the function. If you use the new operator it allocated memory that lasts until you use the delete[] operator (or until the program is closed).

This code shows the original array. Then it reverses that array into a new one. We needed to return a pointer. I have compiled the code and it works.

I don't need any of the information after the programs closes/the memory is erased. So I should be fine. Thanks for helping me though.

off the record...Johnny Bravo is the bomb!!!

This question has already been answered. Start a new discussion instead.