Comments on: Print elements of a matrix in diagonal order https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/ Coding / System Design Interviews Thu, 06 Dec 2018 02:47:46 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Siva Sankar https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/#comment-1893 Thu, 06 Dec 2018 02:47:46 +0000 http://www.ritambhara.in/?p=95#comment-1893 Here is the JavaScript version of answer
https://gist.github.com/sivasankars/0c1496c30997844fbc77498203f8238e

]]>
By: Vishnu Nk https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/#comment-1892 Thu, 28 Jul 2016 07:52:53 +0000 http://www.ritambhara.in/?p=95#comment-1892 /*
Author: Vishnu NK
A program to print all diagonal elements in a matrix.
In the given matrix output should be like:
3->2->4->7->1->8->3->8->7->9->4->2->6->6->0->6->1->5->3->8->9->4->2->1->2
*/
#include <iostream>
using namespace std;
int main(){
int i,j,m=5,n=5;
int mat[5][5] = { {3,4,8,9,0},
{2,1,7,6,3},
{7,8,6,5,4},
{3,2,1,9,1},
{4,6,8,2,2}
};
for(i=0; i<5; i++){
for(j=0; j<5; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
int row=0, col=0;
int count = 0;
while(count < (m+ (n-1))){
//cout << count << endl;
for(row=0; row<5; row++){
for(col=0; col<5; col++){
if(row+col == count){
//cout << "Row: " << row << " :: Col: " << col << " :: Count: " << count << endl;
cout << mat[col][row] << " ";
}
}
}
cout << endl;
count++;
}
return 0;
}

]]>
By: Ajit Kumar https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/#comment-1891 Tue, 21 Jun 2016 11:53:07 +0000 http://www.ritambhara.in/?p=95#comment-1891 public class Ajit {
private static void diagonalPrint(int[][] m) {
int row=m.length;
int col = m[0].length;
for (int i =1;i<=(row+col-1);i++){
int start_col = max(0,i-row);
int count = min(i, (col-start_col), row);
for (int j=0; j<count; j++)
System.out.print(m[min(row,i)-j-1][start_col+j]+" ");
System.out.println();
}
}
static int min(int a, int b) {
return (a < b) ? a : b;
}
static int min(int a, int b, int c) {
return min(min(a, b), c);
}
static int max(int a, int b) {
return (a > b) ? a : b;
}
private static void print(int[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++)
System.out.print(m[i][j]+" ");
System.out.println();
}
}
public static void main(String[] args) {
int M[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },};
print(M);
diagonalPrint(M);
}
}

]]>
By: BharatMataKiJai https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/#comment-1890 Wed, 13 Apr 2016 18:01:44 +0000 http://www.ritambhara.in/?p=95#comment-1890 import java.util.Scanner;
class Sumit
{
public static void main(String args[])
{
int n,i,j,count=1;
Scanner in = new Scanner(System.in);
System.out.println(“Enter the number for diagonal matrix”);
n = in.nextInt();
int matrix[][] = new int[n][n];
for ( int c = 0 ; c < n ; c++ )
for ( int d = 0 ; d < n ; d++ )
{
matrix[c][d] = count;
count++;
}
for(int cnt=0;cnt<2*n-1;cnt++)
{
//System.out.println();
if(cnt=0&&j<n)
{
System.out.print(" "+matrix[i][j]);
i–;
j++;
}
}
}
}

]]>
By: Print both diagonals of a matrix | Ritambhara https://demo.ritambhara.in/print-elements-of-a-matrix-in-diagonal-order/#comment-1889 Mon, 11 Jun 2012 10:55:10 +0000 http://www.ritambhara.in/?p=95#comment-1889 […] Here is another problem on printing elements of a matrix I posted earlier that prints the elements in diagonal order. […]

]]>