Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
I want to print the output as below Output:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
My code is working fine for 3 x 3 matrix but it's not working for 4 x 4 matrix Here is my code please anyone help me out:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter size of 2d array";
cin>>n;
int a[n][n],i,j,l,m,temp;
cout<<"enter elements into array";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"the 2d array is\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<" ";
}
cout<<"\n";
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i%2!=0)
{
for(l=0,m=n-1;l<n/2;l++,m--)
{
temp=a[i][l];
a[i][l]=a[i][m];
a[i][m]=temp;
}
}
}
}
cout<<"the snake movement 2d array is\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
cout<<" ";
}
cout<<"\n";
}
return 0;
}