Reverse array not working?

I am trying to get my reverse array to print numbers backwards but instead I get this once I compile and run:


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36
36 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 83

Only the first and last numbers switch! Help would be appreciated, thank you!


#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
// Declare array of integers
const int DATA_SIZE = 20;
int data[DATA_SIZE] = {0};

// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;

// Initialize array
for (int index = 0; index < DATA_SIZE; index++)
data[index] = random() % 100;

// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;



// Reverse array

char T;
for (int index = 0; index < DATA_SIZE; index++)
{
T = data[index];
data[index] = data[DATA_SIZE-1-index];
data[DATA_SIZE-1-index] = T;

if (index < DATA_SIZE/2)
{
break;
}

}
{
// Print array
for (int index = 0; index < DATA_SIZE; index++)
cout << data[index] << " ";
cout << endl;


}

return 0 ;
}
would that work if i just need a program to output an array to count to 9
You wrote:
if (index < DATA_SIZE/2)

You meant:
if (index > DATA_SIZE/2)

Formatting code and line numbers and indentation are all good ideas
Also, since you want to stop the loop after index hits DATA_SIZE/2, just put that as the condition in the for loop:
1
2
3
4
5
6
for (int index = 0; index < DATA_SIZE/2; index++)
{
    T = data[index];
    data[index] = data[DATA_SIZE-1-index];
    data[DATA_SIZE-1-index] = T;
}
Topic archived. No new replies allowed.