# Reverse the diamond elements in square matrix

Here given code implementation process.

``````/*
C Program
+ Reverse the diamond elements in square matrix
*/
#include <stdio.h>
//Size of matrix
#define ROW 5
#define COL 5

void change(int matrix[ROW][COL],int row_s,int col_s)
{
int temp = 0;
for (int i = col_s; i < (COL)-i; ++i)
{
//Swap element value
temp=matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(COL-1)-i];
matrix[row_s][(COL-1)-i]=temp;
}
}
//Reversing diamond position element in row wise
void reverse_row(int matrix[ROW][COL])
{
if(ROW != COL || COL%2==0)
{
printf("\nThis is not a perfect odd rows or columns matrix");
return;
}

int col_s = COL / 2;

for (int i = 0; i < ROW; ++i)
{
change(matrix,i,col_s);

if(i < ROW/2)
{

col_s--;
}
else
{
col_s++;

}
}

}
//Display matrix elements
void show_data(int matrix[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%3d",matrix[i][j] );
}
printf("\n");
}
printf("\n");
}
int main(){

int matrix[ROW][COL] ={
{1,  2,   3,  4,  5},
{6,  7,   8,  9,  10},
{11, 12, 13, 15,  16},
{17, 18, 19, 20,  21},
{22, 23, 24, 25,  26}

};

show_data(matrix);

reverse_row(matrix);

show_data(matrix);

return 0;
}```
```

#### Output

``````  1  2  3  4  5
6  7  8  9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1  2  3  4  5
6  9  8  7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
C++ Program
Reverse the diamond elements in square matrix
*/
#include<iostream>
#define ROW 5
#define COL 5
using namespace std;

class MyMatrix {
public:
int rows;
int cols;
MyMatrix() {
//Get the size of matrix
this->rows = ROW;
this->cols = COL;
}
void change(int matrix[][COL], int row_s, int col_s) {
int temp = 0;
for (int i = col_s; i < this->cols - i; ++i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(this->cols - 1) - i];
matrix[row_s][(this->cols - 1) - i] = temp;
}
}
//Reversing diamond position element in row wise
void reverse_row(int matrix[][COL]) {
if (this->rows != this->cols || this->cols % 2 == 0) {
cout << "\nThis is not a perfect odd rows or columns matrix";
return;
}
int col_s = this->cols / 2;
for (int i = 0; i < this->rows; ++i) {
this->change(matrix, i, col_s);
if (i < this->rows / 2) {
col_s--;
} else {
col_s++;
}
}
}
//Display matrix elements
void show_data(int matrix[][COL]) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
cout << " " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
};
int main() {
int matrix[][COL] = {
{
1,
2,
3,
4,
5
},
{
6,
7,
8,
9,
10
},
{
11,
12,
13,
15,
16
},
{
17,
18,
19,
20,
21
},
{
22,
23,
24,
25,
26
}
};
MyMatrix obj;
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
return 0;
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
C# Program
Reverse the diamond elements in square matrix
*/
using System;
public class MyMatrix {
int rows;
int cols;
MyMatrix(int[,] matrix) {
//Get the size of matrix
rows = matrix.GetLength(0);
cols = matrix.GetLength(1);
}
public void change(int[,] matrix, int row_s, int col_s) {
int temp = 0;
for (int i = col_s; i < cols - i; ++i) {
//Swap element value
temp = matrix[row_s,i];
matrix[row_s,i] = matrix[row_s,(cols - 1) - i];
matrix[row_s,(cols - 1) - i] = temp;
}
}
//Reversing diamond position element in row wise
public void reverse_row(int[,] matrix) {
if (rows != cols || cols % 2 == 0) {
Console.Write("\nThis is not a perfect odd rows or columns matrix");
return;
}
int col_s = cols / 2;
for (int i = 0; i < rows; ++i) {
change(matrix, i, col_s);
if (i < rows / 2) {
col_s--;
} else {
col_s++;
}
}
}
//Display matrix elements
public void show_data(int[,] matrix) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args) {
int[,]
//Define matrix
matrix = {
{
1,
2,
3,
4,
5
},
{
6,
7,
8,
9,
10
},
{
11,
12,
13,
15,
16
},
{
17,
18,
19,
20,
21
},
{
22,
23,
24,
25,
26
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````<?php
/*
Php Program
Reverse the diamond elements in square matrix
*/
class MyMatrix {
public \$rows;
public \$cols;

function __construct(\$matrix) {
//Get the size of matrix
\$this->rows = count(\$matrix);
\$this->cols = count(\$matrix[0]);
}
public 	function change(&\$matrix, \$row_s, \$col_s) {
\$temp = 0;
for (\$i = \$col_s; \$i < \$this->cols - \$i; ++\$i) {
//Swap element value
\$temp = \$matrix[\$row_s][\$i];
\$matrix[\$row_s][\$i] = \$matrix[\$row_s][(\$this->cols - 1) - \$i];
\$matrix[\$row_s][(\$this->cols - 1) - \$i] = \$temp;
}
}
//Reversing diamond position element in row wise

public 	function reverse_row(&\$matrix) {
if (\$this->rows != \$this->cols || \$this->cols % 2 == 0) {
echo("\nThis is not a perfect odd rows or columns matrix");
return;
}
\$col_s = intval(\$this->cols / 2);
for (\$i = 0; \$i < \$this->rows; ++\$i) {
\$this->change(\$matrix, \$i, \$col_s);
if (\$i < intval(\$this->rows / 2)) {
\$col_s--;
} else {
\$col_s++;
}
}
}
//Display matrix elements

public 	function show_data(\$matrix) {
for (\$i = 0; \$i < \$this->rows; ++\$i) {
for (\$j = 0; \$j < \$this->cols; ++\$j) {
echo(" ". \$matrix[\$i][\$j]);
}
echo("\n");
}
echo("\n");
}
};

function main() {
//Define matrix
\$matrix = array(
array(1, 2, 3, 4, 5),
array(6, 7, 8, 9, 10),
array(11, 12, 13, 15, 16),
array(17, 18, 19, 20, 21),
array(22, 23, 24, 25, 26)
);
\$obj = new MyMatrix(\$matrix);
\$obj->show_data(\$matrix);
\$obj->reverse_row(\$matrix);
\$obj->show_data(\$matrix);

}
main();```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
Node Js Program
Reverse the diamond elements in square matrix
*/
class MyMatrix {
;;
constructor(matrix) {
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
change(matrix, row_s, col_s) {
var temp = 0;
for (var i = col_s; i < this.cols - i; ++i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(this.cols - 1) - i];
matrix[row_s][(this.cols - 1) - i] = temp;
}
}

//Reversing diamond position element in row wise
reverse_row(matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
process.stdout.write("\nThis is not a perfect odd rows or columns matrix");
return;
}
var col_s = parseInt(this.cols / 2);
for (var i = 0; i < this.rows; ++i) {
this.change(matrix, i, col_s);
if (i < parseInt(this.rows / 2)) {
col_s--;
} else {
col_s++;
}
}
}

//Display matrix elements
show_data(matrix) {
for (var i = 0; i < this.rows; ++i) {
for (var j = 0; j < this.cols; ++j) {
process.stdout.write(" " + matrix[i][j]);
}

process.stdout.write("\n");
}

process.stdout.write("\n");
}
}

function main(args) {
//Define matrix
var matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
];
var obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
main();```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````# Python 3 Program
# Reverse the diamond elements in square matrix
class MyMatrix :

def __init__(self, matrix) :
# Get the size of matrix
self.rows = len(matrix)
self.cols = len(matrix[0])

def change(self, matrix, row_s, col_s) :
temp = 0
i = col_s
while (i < self.cols - i) :
# Swap element value
temp = matrix[row_s][i]
matrix[row_s][i] = matrix[row_s][(self.cols - 1) - i]
matrix[row_s][(self.cols - 1) - i] = temp
i += 1

# Reversing diamond position element in row wise
def reverse_row(self, matrix) :
if (self.rows != self.cols or self.cols % 2 == 0) :
print("\nThis is not a perfect odd rows or columns matrix", end = "")
return

col_s = int(self.cols / 2)
i = 0
while (i < self.rows) :
self.change(matrix, i, col_s)
if (i < int(self.rows / 2)) :
col_s -= 1
else :
col_s += 1

i += 1

# Display matrix elements
def show_data(self, matrix) :
i = 0
while (i < self.rows) :
j = 0
while (j < self.cols) :
print(" ", matrix[i][j], end = "")
j += 1

print("\n", end = "")
i += 1

print("\n", end = "")

def main() :
matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
]
obj = MyMatrix(matrix)
obj.show_data(matrix)
obj.reverse_row(matrix)
obj.show_data(matrix)

if __name__ == "__main__":
main()```
```

#### Output

``````  1  2  3  4  5
6  7  8  9  10
11  12  13  15  16
17  18  19  20  21
22  23  24  25  26

1  2  3  4  5
6  9  8  7  10
16  15  13  12  11
17  20  19  18  21
22  23  24  25  26
``````
``````# Ruby Program
# Reverse the diamond elements in square matrix
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_accessor :rows, :cols
def initialize(matrix)
# Get the size of matrix
@rows = matrix.length
@cols = matrix[0].length
end
def change(matrix, row_s, col_s)
temp = 0
i = col_s
while (i < @cols - i)
# Swap element value
temp = matrix[row_s][i]
matrix[row_s][i] = matrix[row_s][(@cols - 1) - i]
matrix[row_s][(@cols - 1) - i] = temp
i += 1
end
end
# Reversing diamond position element in row wise
def reverse_row(matrix)
if (@rows != @cols or @cols % 2 == 0)
print("\nThis is not a perfect odd rows or columns matrix")
return
end
col_s = @cols / 2
i = 0
while (i < @rows)
self.change(matrix, i, col_s)
if (i < @rows / 2)
col_s -= 1
else
col_s += 1
end
i += 1
end
end
# Display matrix elements
def show_data(matrix)
i = 0
while (i < @rows)
j = 0
while (j < @cols)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
end
def main()
matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
]
obj = MyMatrix.new(matrix)
obj.show_data(matrix)
obj.reverse_row(matrix)
obj.show_data(matrix)
end

main()```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26

``````
``````/*
Scala Program
Reverse the diamond elements in square matrix
*/
class MyMatrix(var rows: Int,var cols: Int) {

def this(matrix: Array[Array[Int]]) {
//Get the size of matrix
this(matrix.length,matrix(0).length);
}
def change(matrix: Array[Array[Int]], row_s: Int, col_s: Int): Unit = {
var temp: Int = 0;
var i: Int = col_s;
while (i < this.cols - i) {
//Swap element value
temp = matrix(row_s)(i);
matrix(row_s)(i) = matrix(row_s)((this.cols - 1) - i);
matrix(row_s)((this.cols - 1) - i) = temp;
i += 1;
}
}
//Reversing diamond position element in row wise
def reverse_row(matrix: Array[Array[Int]]): Unit = {
if (this.rows != this.cols || this.cols % 2 == 0) {
print("\nThis is not a perfect odd rows or columns matrix");

return;
}
var col_s: Int = (this.cols / 2).toInt;
var i: Int = 0;
while (i < this.rows) {
this.change(matrix, i, col_s);

if (i < (this.rows / 2).toInt) {
col_s -= 1;
} else {
col_s += 1;
}
i += 1;
}
}
//Display matrix elements
def show_data(matrix: Array[Array[Int]]): Unit = {
var i: Int = 0;
while (i < this.rows) {
var j: Int = 0;
while (j < this.cols) {
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val matrix: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5),
Array(6, 7, 8, 9, 10),
Array(11, 12, 13, 15, 16),
Array(17, 18, 19, 20, 21),
Array(22, 23, 24, 25, 26));
val obj: MyMatrix = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
Swift 4 Program
Reverse the diamond elements in square matrix
*/
class MyMatrix {
var rows : Int;
var cols : Int;
init(_ matrix: [
[Int]
]) {
//Get the size of matrix
self.rows = matrix.count;
self.cols = matrix[0].count;
}
func change(_ matrix: inout [[Int]], _ row_s: Int, _ col_s: Int) {
var temp: Int = 0;
var i: Int = col_s;
while (i < self.cols - i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(self.cols - 1) - i];
matrix[row_s][(self.cols - 1) - i] = temp;
i += 1;
}
}
//Reversing diamond position element in row wise
func reverse_row(_ matrix: inout [[Int]]) {
if (self.rows != self.cols || self.cols % 2 == 0) {
print("\nThis is not a perfect odd rows or columns matrix", terminator: "");
return;
}
var col_s: Int = self.cols / 2;
var i: Int = 0;
while (i < self.rows) {
self.change(&matrix, i, col_s);
if (i < self.rows / 2) {
col_s -= 1;
} else {
col_s += 1;
}
i += 1;
}
}
//Display matrix elements
func show_data(_ matrix: [
[Int]
]) {
var i: Int = 0;
while (i < self.rows) {
var j: Int = 0;
while (j < self.cols) {
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main() {
var matrix: [[Int]] = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(&matrix);
obj.show_data(matrix);
}
main();```
```

#### Output

``````  1  2  3  4  5
6  7  8  9  10
11  12  13  15  16
17  18  19  20  21
22  23  24  25  26

1  2  3  4  5
6  9  8  7  10
16  15  13  12  11
17  20  19  18  21
22  23  24  25  26
``````

## Comment

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.