i wrote some code to take an array and run it through a function to reverse the elements of the array and then return a pointer to the new array. For some reason im not getting the results i want. It has to do with my reverseArray function. Specifically in my for loop because when i hard code my new array i get the numbers i want. I would like to avoid hard coding them. any advise??

#include<iostream>
using namespace std;

int* reverseArray(int,int[]);
void showArray(int[], int);

int main()
{
	const int SIZE = 5;
	int* fixedArray;
	int test[SIZE] = {10, 20, 30, 40, 50};
	
	int* arrayPtr;
	
	
	fixedArray = reverseArray(SIZE, test);

	cout << "Here is the original array: " << endl;
	showArray(test,SIZE);
	cout << "Here is the new array: " << endl;
	showArray(fixedArray, SIZE);
}

	int* reverseArray(int size,int score[])
{
	int* revArray;
	int count;
	int index;

	revArray = new int[size];
	
	for(count = 5;count = 0;count--)
	{
		index = 0;

		revArray[index] = score[count];
		index++;

	}

	return revArray;


}
	void showArray(int score[], int size)
	{
		for(int index = 0; index < size; index++)
		{
			cout << score[index] << " " << endl;
		}


	}

Sorry to say, it is the usual problems.

Basically, if you get a problem like this put some std::cout statements in the inner loops that are doing stuff. .e.g in reverseArray.

You would find: for(count=5;count = 0; count--) is wrong since I think you intended to write for(count=5;count != 0; count--) .

Note: That error would have been a warning from your compiler if you turn on the warnings.

You have another error, that is a bit more subtle, that is you have an array of size items, BUT you set count = 5. That means you have a bug waiting to happen. Next, you then use score[count] . Note that score is an array from score[0] to score[4]. Try something like score[count-1] Finally, please remember to delete the memory you allocate in reverseArray.

Thank you i should have seen that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.