Question #47757

c++ program to reverse an array.

Expert's answer

#include <iostream>

using namespace std;

int main()

{

int a[5] = { 1, 2, 3, 4, 5 }; // init array

int i; // counter

int n = 5; // count values in array

// dispaly start values

for ( int i = 0; i < n; i++ ) {

cout << a[i]

<< " ";

}

// reverse array

for ( int i = 0; i < n / 2; i++ ) {

int tmp = a[i];

a[i] = a[ n - i - 1 ];

a[ n - i - 1 ] = tmp;

}

cout << endl; // new string

// dispaly finish values

for ( int i = 0; i < n; i++ ) {

cout << a[i]

<< " ";

}

return 0;

}

using namespace std;

int main()

{

int a[5] = { 1, 2, 3, 4, 5 }; // init array

int i; // counter

int n = 5; // count values in array

// dispaly start values

for ( int i = 0; i < n; i++ ) {

cout << a[i]

<< " ";

}

// reverse array

for ( int i = 0; i < n / 2; i++ ) {

int tmp = a[i];

a[i] = a[ n - i - 1 ];

a[ n - i - 1 ] = tmp;

}

cout << endl; // new string

// dispaly finish values

for ( int i = 0; i < n; i++ ) {

cout << a[i]

<< " ";

}

return 0;

}

``

## Comments

## Leave a comment