Arrays-Wave print Column Wise

My code in this problem is passing only two test cases. How do I optimize my code here?

Hi @shubhamhalder0
Please share your code ide so that i can have a look.

#include using namespace std; # define R 10 # define C 10 void wavePrint(int a[R][C],int m,int n){ int i=0,j=0,wave_direction=1; while(j=0;i–){ cout<>m>>n; for(int i=0;i>a[i][j]; } } wavePrint(a,m,n); return 0; }

1 Like

@shubhamhalder0
Kindly copy paste your code in coding blocks ide and save it and share the link with me because the code you have shared is missing a lot of things(especially int main()).

Hello @shubhamhalder0,

Try to understand your mistake from the following test case:
4 3
11 12 13
21 22 23
31 32 33
41 42 43
Expected Output:
11, 21, 31, 41, 42, 32, 22, 12, 13, 23, 33, 43, END
Your Output:
11, 21, 31, 41, 42, 32, 22, 12, 13, 23, 33, 43, 0, -1816919515, 0, 0, END

Let me know if you still face any issue.

Please Explain elaborately

*elaborately…

Hey @Aayush,

Your code will always print even numbers of columns even if number of columns in the matrix are odd as shown in the example specified in my previous reply.

Reason:
After printing the third column, j will increment to 3 which is equal to n and the wave_direction will become 0.
This will cause the execution of second for loop.
Hence, the fourth row with garbage values will be printed as show above.

Solution:
Include a condition check for k<n over the code segment of printing the even column.

I have modified your code:

Hope, this would help.
Give a like if you are satisfied.

1 Like