Column wise wave print

giving wrong answer,reasons ?
#include<bits/stdc++.h>
using namespace std;
int main()
{

    int r,c;
    cin>>r>>c;
    int arr[r][c];
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            cin>>arr[i][j];
        }
    }
    int i=0;//pointer for row
    int j=0;//pointer for column
    while(j<c)
    {
        while(i<r)
        {
            cout<<arr[i][j]<<","<<" ";
            i++;

        }
        i=r-1;
        j++;
        while(i>=0)
        {
            cout<<arr[i][j]<<","<<" ";
            i--;
        }
        i=0;
        j++;
    }
return 0;

}

Hey Shubham, your code will not work for the cases where no. of rows and columns are not equal. for eg.
input:
4 5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35

To resolve this error update your code’s inner while loops conditions as while(i<r && j<c) and while(i>=0 && j<c)

making these changes still my code is not successfully submitted
#include<bits/stdc++.h>
using namespace std;
int main()
{
int r,c;
cin>>r>>c;
int arr[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>arr[i][j];
}
}
int i=0;//pointer for row
int j=0;//pointer for column
while(j<c)
{
while(i<r && j<c)
{
cout<<arr[i][j]<<" ";
i++;

        }
        i=r-1;
        j++;
         while(i>=0 && j<c)
        {
            cout<<arr[i][j]<<" ";
            i--;
        }
        i=0;
        j++;
    }
return 0;

}

Hey, the code is fine but your output should exactly match the output pattern given in the problem for successful submission of the code. for eg.
input:
4 4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

your code’s output:
11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44

but the expected output is:
11, 21, 31, 41, 42, 32, 22, 12, 13, 23, 33, 43, 44, 34, 24, 14, END

so do print the comma ’,' after each number and "END" at the end of the output.

thanks mam :):grinning: