Simplest way to print an array in reverse direction
Code is :
using namespace std;
int a[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(a)/sizeof(int);
int *ptr = a + size;
for(int i = 1; i != size + 1; ++i)
{
cout<<ptr[-i]<<endl;
}
or
int a[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(a)/sizeof(int);
int *ptr = a + size -1; // bcz array start with 0th index.
for(int i = 0; i != size ; ++i)
{
cout<<ptr[-i]<<endl;
}
using namespace std;
int a[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(a)/sizeof(int);
int *ptr = a + size;
for(int i = 1; i != size + 1; ++i)
{
cout<<ptr[-i]<<endl;
}
or
int a[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(a)/sizeof(int);
int *ptr = a + size -1; // bcz array start with 0th index.
for(int i = 0; i != size ; ++i)
{
cout<<ptr[-i]<<endl;
}
Thanx.
Comments
Post a Comment