dynamically allocate an array and print content in reverse

I need to Write a program that dynamically allocates an array, enters its content from the keyboard and print its content in reverse order i am able to print it regular but I'm having trouble printing it in revere


heres a code i made that prints it regular

#include <iostream>

using namespace std;

int main(){
int length ;
int *ArrayPtr;

cout << " Please enter the length of the array " << endl;
cin >> length;

ArrayPtr = new int[length];

cout << "Please enter the elements of the array " << endl;

for (int index = 0; index <= length -1; index++)
cin >> ArrayPtr[index];

for (int index = 0; index <= length -1; index++)
cout << ArrayPtr[index]<<endl;
return 0;
}


So what have you done to print the array contents in reverse order?

By the way this for (int index = 0; index <= length -1; index++) really should be done as: for (int index = 0; index < length; index++)

It is too easy to forget the subtraction and access your array out of bounds, so just don't do the subtraction by using the '<' operator instead of the "<=" operator.

And don't forget to delete[] what you new[].

And please use code tags when posting code.
Last edited on
Topic archived. No new replies allowed.