Answer to Question #47757 in C++ for arundhati

Question #47757
c++ program to reverse an array.
1
Expert's answer
2014-10-14T00:56:44-0400
#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;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS