Print matrix in z form
Here given code implementation process.
/*
C Program
Print matrix in z form
*/
#include <stdio.h>
#define SIZE 6
void z_form(int matrix[SIZE][SIZE])
{
int i = 0, j = 0, c = SIZE - 1;
for (i = 0; i < SIZE && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == SIZE)
{
//display first and last rows
for (j = 0; j < SIZE; ++j)
{
printf("%d ", matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
printf("%d ", matrix[i][c]);
}
}
}
int main()
{
//Define matrix elements
int matrix[SIZE][SIZE] = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
z_form(matrix);
return 0;
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Java Program
Print matrix in z form
*/
class MyMatrix
{
public void z_form(int[][] matrix)
{
int size = matrix.length;
if (size != matrix[0].length)
{
// not NxN matrix
return;
}
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
System.out.print(" " + matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
System.out.print(" " + matrix[i][c]);
}
}
}
public static void main(String[] args)
{
MyMatrix obj = new MyMatrix();
int[][] matrix = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
C++ Program
Print matrix in z form
*/
#include<iostream>
#define SIZE 6
using namespace std;
class MyMatrix
{
public: void z_form(int matrix[SIZE][SIZE])
{
//Accept NxN matrix
int size = SIZE;
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
cout << " " << matrix[i][j];
}
}
else
{
cout << " " << matrix[i][c];
}
}
}
};
int main()
{
MyMatrix obj = MyMatrix();
int matrix[SIZE][SIZE] = {
{3, 4, 5, 6, 7, 8},
{58,41,38,18,9, 12},
{55,42,37,10,13,33},
{50,43,11,20,12,54},
{49,12,35,21,11,65},
{13,45,34,31,10,14}
};
obj.z_form(matrix);
return 0;
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
C# Program
Print matrix in z form
*/
using System;
class MyMatrix
{
public void z_form(int[,] matrix)
{
int size = matrix.GetLength(0);
if (size != matrix.GetLength(1)){
// not NxN matrix
return;
}
int i = 0, j = 0, c = size - 1;
for (i = 0; i < size && c >= 0; i++, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; j++)
{
Console.Write(" " + matrix[i,j]);
}
}
else
{
Console.Write(" " + matrix[i,c]);
}
}
}
public static void Main(String[] args)
{
MyMatrix obj = new MyMatrix();
int[,] matrix = {
{
3,
4,
5,
6,
7,
8
},
{
58,
41,
38,
18,
9,
12
},
{
55,
42,
37,
10,
13,
33
},
{
50,
43,
11,
20,
12,
54
},
{
49,
12,
35,
21,
11,
65
},
{
13,
45,
34,
31,
10,
14
}
};
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
<?php
/*
Php Program
Print matrix in z form
*/
class MyMatrix
{
public function z_form( & $matrix)
{
$size = count($matrix);
if ($size != count($matrix[0]))
{
return;
}
$i = 0;
$j = 0;
$c = $size - 1;
for ($i = 0; $i < $size && $c >= 0; ++$i, $c--)
{
if ($i == 0 || $i + 1 == $size)
{
//display first and last rows
for ($j = 0; $j < $size; ++$j)
{
echo(" ". $matrix[$i][$j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
echo(" ". $matrix[$i][$c]);
}
}
}
}
function main()
{
$obj = new MyMatrix();
$matrix = array(
array(3, 4, 5, 6, 7, 8),
array(58, 41, 38, 18, 9, 12),
array(55, 42, 37, 10, 13, 33),
array(50, 43, 11, 20, 12, 54),
array(49, 12, 35, 21, 11, 65),
array(13, 45, 34, 31, 10, 14));
$obj->z_form($matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Node Js Program
Print matrix in z form
*/
class MyMatrix
{
z_form(matrix)
{
var size = matrix.length;
if (size != matrix[0].length)
{
return;
}
var i = 0;
var j = 0;
var c = size - 1;
for (i = 0; i < size && c >= 0; ++i, c--)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
for (j = 0; j < size; ++j)
{
process.stdout.write(" " + matrix[i][j]);
}
}
else
{
//Display diagonal elements from top right to bottom left
process.stdout.write(" " + matrix[i][c]);
}
}
}
}
function main(args)
{
var obj = new MyMatrix();
var matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
];
obj.z_form(matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
#
# Python 3 Program
# Print matrix in z form
class MyMatrix :
def z_form(self, matrix) :
size = len(matrix)
if (size != len(matrix[0])) :
return
i = 0
j = 0
c = size - 1
while (i < size and c >= 0) :
if (i == 0 or i + 1 == size) :
# display first and last rows
j = 0
while (j < size) :
print(" ", matrix[i][j], end = "")
j += 1
else :
print(" ", matrix[i][c], end = "")
i += 1
c -= 1
def main() :
obj = MyMatrix()
matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
]
obj.z_form(matrix)
if __name__ == "__main__": main()
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
# Ruby Program
# Print matrix in z form
class MyMatrix
def z_form(matrix)
size = matrix.length
if (size != matrix[0].length)
return
end
i = 0
j = 0
c = size - 1
while (i < size && c >= 0)
if (i == 0 || i + 1 == size)
# display first and last rows
j = 0
while (j < size)
print(" ", matrix[i][j])
j += 1
end
else
print(" ", matrix[i][c])
end
i += 1
c -= 1
end
end
end
def main()
obj = MyMatrix.new()
matrix = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
]
obj.z_form(matrix)
end
main()
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Scala Program
Print matrix in z form
*/
class MyMatrix
{
def z_form(matrix: Array[Array[Int]]): Unit = {
var size: Int = matrix.length;
if (size != matrix(0).length)
{
return;
}
var i: Int = 0;
var j: Int = 0;
var c: Int = size - 1;
while (i < size && c >= 0)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
j = 0;
while (j < size)
{
print(" " + matrix(i)(j));
j += 1;
}
}
else
{
print(" " + matrix(i)(c));
}
i += 1;
c -= 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
var matrix: Array[Array[Int]] = Array(
Array(3, 4, 5, 6, 7, 8),
Array(58, 41, 38, 18, 9, 12),
Array(55, 42, 37, 10, 13, 33),
Array(50, 43, 11, 20, 12, 54),
Array(49, 12, 35, 21, 11, 65),
Array(13, 45, 34, 31, 10, 14));
obj.z_form(matrix);
}
}
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
/*
Swift Program
Print matrix in z form
*/
class MyMatrix
{
func z_form(_ matrix: [
[Int]
])
{
let size: Int = matrix.count;
if (size != matrix[0].count)
{
return;
}
var i: Int = 0;
var j: Int = 0;
var c: Int = size - 1;
while (i < size && c >= 0)
{
if (i == 0 || i + 1 == size)
{
//display first and last rows
j = 0;
while (j < size)
{
print(" ", matrix[i][j], terminator: "");
j += 1;
}
}
else
{
print(" ", matrix[i][c], terminator: "");
}
i += 1;
c -= 1;
}
}
}
func main()
{
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[3, 4, 5, 6, 7, 8],
[58, 41, 38, 18, 9, 12],
[55, 42, 37, 10, 13, 33],
[50, 43, 11, 20, 12, 54],
[49, 12, 35, 21, 11, 65],
[13, 45, 34, 31, 10, 14]
];
obj.z_form(matrix);
}
main();
Output
3 4 5 6 7 8 9 10 11 12 13 45 34 31 10 14
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment