-1

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;
}
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • These code snippet for(j=0;j – Vlad from Moscow Sep 10 '20 at 13:00
  • 2
    why so complicated? After reading you need not modify the array at all. Only when printing the output every second line needs the loop backwards. Using `std::vector` would help a lot here – 463035818_is_not_an_ai Sep 10 '20 at 13:06
  • 3
    also read [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Your code uses a non-standard feature that comes with some compilers as extension. – 463035818_is_not_an_ai Sep 10 '20 at 13:06

3 Answers3

1

For starters variable length arrays (VLA) is not a standard C++ feature

cin>>n;
int a[n][n],i,j,l,m,temp;

Instead use the standard container std::vector<std::vector<int>>.

Secondly to output a row in the reverse order there is no need to swap elements of the row.

Thirdly this loop

for(j=0;j<n;j++)

is redundant. Leave only its inner loop that swaps elements in a row if you want to swap them.

Without swapping elements in rows the program can look for example the following way.

#include <iostream>
#include <iomanip>
#include <vector>

int main() 
{
    std::cout << "Enter the size of 2d array: ";
    
    size_t n = 0;
    
    if ( std::cin >> n )
    {
        std::vector<std::vector<int>> v( n, std::vector<int>( n ) );
        
        std::cout<< "enter elements into the array: ";
        
        for ( auto &row : v )
        {
            for ( auto &item : row )
            {
                std::cin >> item;
            }
        }
        
        std::cout << '\n';
        
        std::cout << "the 2d array is\n";
        
        for ( const auto &row : v )
        {
            for ( const auto &item : row )
            {
                std::cout << std::setw( 2 ) << item << ' ';
            }
            std::cout << '\n';
        }
        
        std::cout << "\nthe snake movement 2d array is\n";
        
        for ( size_t i = 0; i < n; i++ )
        {
            for ( size_t j = 0; j < n; j++ )
            {
                std::cout << std::setw( 2 ) 
                          << ( i % 2 == 0 ? v[i][j] : v[i][n-j-1] )
                          << ' ';
            }
            std::cout << '\n';
        }
    }
    
    return 0;
}

Its output might look like

Enter the size of 2d array: 4
enter elements into the array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
the 2d array is
 1  2  3  4 
 5  6  7  8 
 9 10 11 12 
13 14 15 16 

the snake movement 2d array is
 1  2  3  4 
 8  7  6  5 
 9 10 11 12 
16 15 14 13 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You have a loop for(j=0;j<n;j++) in your snake modification. You only want to loop once. You have a nested loop, so you're actually swapping the same elements an even number of times, undoing your snake modification. Simply remove your inner loop, and you should be good.

#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++)
    {
        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;
}
Chris Clayton
  • 241
  • 3
  • 9
0

I have modified your code, you number changing order logic is faulty, try this one out..

#include <iostream>

using namespace std;

int main() {
    int n = 0;
    cout<<"Enter size of 2d array: ";
    cin>>n;
    
    int a[n][n] = {0};
    int 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 inputed 2d array is: \n";
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            cout<<a[i][j];
            cout<<" ";
        }
        cout<<"\n";
    }
    
    //for changing the order logic
    for(i=0;i<n;i++) {
        
        //if it is the odd row
        if(i % 2 != 0) { 
            
            //for the row to invert
            for(j=n-1, l=0; j>n/2; l++, j--) {
                temp=a[i][j];
                a[i][j]=a[i][l];
                a[i][l]=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;
}