1
#include <bits/stdc++.h>
using namespace std;
int M;
int N;
int K;
int temp=0;
void leftrotate(int A[M][N])
{

    for(int i=0;i<M;i++)
    {
        temp=A[i][0];
        int j;
        for(j=0;j<N-1;j++)
        {
            A[i][j]=A[i][j+1];
        }
        A[i][j]=temp;
    }

    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            cout<<A[i][j];
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>M;
        cin>>N;
        cin>>K;
        int A[M][N];
        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {

                cin>>A[i][j];
            }
        }
        leftrotate(A);
    }
    return 0;
}

As you can see inside main function the values of m and n are taken dynamically and all the variables are defined globally. Now I need to call the function leftrotate with the 2d array as parameter. What is the way to do that with or without pointers ?

Yash Sharma
  • 811
  • 1
  • 9
  • 27
  • 5
    This is illegal in standard C++ `int A[M][N];` as `M` and `N` must be known at compile time. Instead I'd suggest you look up `std::vector` – Cory Kramer Sep 26 '17 at 15:35
  • 2
    Note that C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. It will incidentally help you with your problem as well. – Some programmer dude Sep 26 '17 at 15:36
  • okay if i need to use arrays only then i will have to define one length right ? – Yash Sharma Sep 26 '17 at 15:36
  • 1
    I also recommend you read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Sep 26 '17 at 15:37
  • @Someprogrammerdude thanks :) – Yash Sharma Sep 26 '17 at 15:38
  • If you don't want to use C++ (`std::vector` "wrapper" for dynamic arrays), then you can use good old plain old memory C-like array consisting of pointer pointing to the first element (and the whole data are stored in continuous memory block, starting there), and size(s) passed as another argument, like `void leftrotate(int *A, const int M, const int N);`. Then your code defines the internal layout of data in memory (rows vs columns, or even "triangle" to save memory in case you know A[x,y] == A[y,x] and M==N are huge). The most common "mapping" function for index is `idx(x, y) = y*size_x + x;` – Ped7g Sep 26 '17 at 17:02
  • Don't use VLAs, they are not an official C++ feature (and get your compiler to warn about them). Use `std::vector`. – Jesper Juhl Sep 26 '17 at 18:20

1 Answers1

-1

For multidimensional dynamic array you can use:

vector<vector<int>> A

or

int **A

Sample:

void leftrotate(int **A)
{

    for(int i=0;i<M;i++)
    {
        temp=A[i][0];
        int j;
        for(j=0;j<N-1;j++)
        {
            A[i][j]=A[i][j+1];
        }
        A[i][j]=temp;
    }

    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            cout<<A[i][j];
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>M;
        cin>>N;
        cin>>K;
        int **A = new int*[M];
            for(int i = 0; i < M; i++)
                A[i] = new int[N];

        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {

                int p;
                cin>>p;
                A[i][j] = p;
            }
        }
        leftrotate(A);

        for(int i = 0; i < M; ++i) {
            delete [] A[i];
        }
        delete [] A;
    }
    return 0;
}
rflobao
  • 562
  • 2
  • 8
  • Please don't use any of these if high performance is required. Follow Ped7g's suggestion in the comments above. – Chiel Sep 26 '17 at 23:09
  • @Chiel why is that not very efficient ? – Yash Sharma Sep 27 '17 at 04:18
  • @YashSharma. Because the elements of the array won't be contiguous in memory. For that you need to make single vector and use the method that Ped7g is suggesting. – Chiel Sep 27 '17 at 10:35