Hi
i need to write c program to print the matrix elements in diagonal order,
for example,
int mat[3][4] = [ 0 1 2 3
4 5 6 7
8 9 10 11 ]
output print should be : 0 4 1 8 5 2 9 6 3 10 7 11
Pl check the attachment for more clarity.
This is a discussion on Print the matrix in diagonal order - C program within the C Programming forums, part of the General Programming Boards category; Hi i need to write c program to print the matrix elements in diagonal order, for example, int mat[3][4] = ...
Hi
i need to write c program to print the matrix elements in diagonal order,
for example,
int mat[3][4] = [ 0 1 2 3
4 5 6 7
8 9 10 11 ]
output print should be : 0 4 1 8 5 2 9 6 3 10 7 11
Pl check the attachment for more clarity.
The way to solve a lot of problems like this, is to do it by hand, away from the computer, first. Note the row and column indices as you do it. Repeat it a few times, and you'll start to see the pattern. That pattern is your logic, and serves as the backbone of your pseudo-code, once you put it down into small steps.
All the diagonal lines follow the same logic. The only difference is where the starting point is. Find the logic to solve that, and the rest is all a "matter of technique" as they say.
Give that a try. We don't provide answers to questions like this - it's up to you to make that initial effort, and ask specific questions about what has you stuck. Just a general "it doesn't work", doesn't work here very well. (just a heads up).
Hi Adak,
Thank you very much for the quick reply. i was not expecting a direct c program or pseudo-code solution for the problem. i was strucked to similar logic by all my hand trials, so i was expecting a new approch/view from others. That works, your quoate "All the diagonal lines follow the same logic. The only difference is where the starting point is" is what the core solution. i wrote a function which does the work expected ( just a paper work, i need to check it with m/c ).
Code:void prtMatrixDiag(){ int i,j,a,b,reach = 0; for(i = 0; i < colomn; i++ ) { for(j = 0; j < row; j++) { a = i; if(reach) b = row+1; else b = j; while(a >= 0 && b >=0) { printf("%d ", mat[b][a]); b--; a++; if ((j+1) == row) reach = 1; } } } }