Monday 7 April 2014

Difficult array problem in C++ /Array operation in C++

C++ program to

Print an array ,reverse an array ,divide it in block of 3 ,then reverse again .

#include <iostream>

using namespace std;
int main()
{
int a[80],i, j, n, k, l;
cout << "enter the n :";
cin >> n;
cout << "enter the series :";
for (i = 0; i <n; i++)
{

cin >> a[i];
cout << "a[" << i << "]=" << a[i];
cout << endl;
}

cout << "now reverse:";
cout << endl;
for (i = n-1; i>=0; i--)
{
cout << a[i]<<" ";

}
cout << endl;
cout << "Now in a block of three:::"<<endl ;
if (n%3==0)
{
for (i = 0; i < n; i++)
{


if (i% 3== 0)
{
cout << endl;

}
cout << a[i]<<",";

}
}cout << endl;


cout << endl;
cout << "Now reversing each  block of three:::" << endl;
if (n % 3 == 0)
{
for (i = n-1; i >= 0; i--)
{

cout << a[i] << ",";
if (i % 3 == 0)
{
cout << endl;

}


}
}cout << endl;

return 0;
}



enter the n :6
enter the series :1 2 3 4 5 6
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
a[5]=6
now reverse:
6 5 4 3 2 1
Now in a block of three:::

1,2,3,
4,5,6,

Now reversing each  block of three:::
6,5,4,
3,2,1,

Press any key to continue . . .

No comments:

Post a Comment