c++arraysprintingreverse

Printing an array in C++?


Is there a way of printing arrays in C++?

I'm trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and it seemed like C++ can't print arrays. That can't be true can it?


Solution

  • Just iterate over the elements. Like this:

    for (int i = numElements - 1; i >= 0; i--) 
        cout << array[i];
    

    Note: As Maxim Egorushkin pointed out, this could overflow. See his comment below for a better solution.