Square of diagonal elements in matrix
In this program, we are dealing with a square matrix and aim to calculate the squares of its diagonal elements. A square matrix has equal rows and columns, and the diagonal elements are those elements that lie on the main diagonal, which is a line connecting the top-left corner to the bottom-right corner of the matrix. There are two main diagonals in a square matrix: the principal diagonal (from the top-left to the bottom-right) and the secondary diagonal (from the top-right to the bottom-left).
Problem Statement
The task is to take a square matrix as input and calculate the square of each diagonal element. We will then print the results for both the principal and secondary diagonals.
Example
Let's consider a square matrix of size 3x3:
[1, 2, 3],
[4, 5, 6],
[9, 8, 7]
The principal diagonal elements are: 1, 5, 7. The squares of these elements are: 1, 25, 49. The secondary diagonal elements are: 3, 5, 9. The squares of these elements are: 9, 25, 81.
Pseudocode
function diagonal_squares(data: 2D array of integers with dimensions ROW x COL)
Print "First Diagonal:"
for i = 0 to ROW - 1
square = data[i][i] * data[i][i]
Print square
end for
Print "Second Diagonal:"
for i = 0 to ROW - 1
square = data[(ROW - 1) - i][i] * data[(ROW - 1) - i][i]
Print square
end for
end function
function main()
Declare ROW as integer
Declare COL as integer
Set ROW = 3
Set COL = 3
Declare arr as 2D array of integers with dimensions ROW x COL and initialize it with given values:
arr = { {1, 2, 3},
{4, 5, 6},
{9, 8, 7} }
Call diagonal_squares(arr)
end function
Note: The pseudocode assumes that ROW
and COL
are constants with a value of 3, as specified in the original code. The diagonal_squares
function takes a 2D array as input and calculates and prints the squares of diagonal elements, as described in the problem statement. The main
function initializes the matrix arr
with the given values and calls the diagonal_squares
function to perform the required calculation.
Algorithm
- Start
- Initialize the 3x3 matrix (arr) with given values.
- Define a function
diagonal_squares(data)
that takes the matrix as input. - Print "First Diagonal:".
- Loop over the range (0 to ROW) with iterator
i
: 5.1. Calculate the square ofdata[i][i]
. 5.2. Print the result. - Print "Second Diagonal:".
- Loop over the range (0 to ROW) with iterator
i
: 7.1. Calculate the square ofdata[(ROW-1)-i][i]
. 7.2. Print the result. - End
Explanation
The code first defines the matrix arr
with the given values and then calls the
diagonal_squares
function, passing the matrix as an argument. The diagonal_squares
function then calculates and prints the squares of the diagonal elements.
In the first loop, the iterator i
runs from 0 to ROW (exclusive). The value of data[i][i]
gives the principal diagonal elements. The square of each principal diagonal element is calculated using
data[i][i] * data[i][i]
and printed.
In the second loop, the iterator i
also runs from 0 to ROW (exclusive). The value of
data[(ROW-1)-i][i]
gives the secondary diagonal elements. The square of each secondary diagonal element
is calculated using data[(ROW-1)-i][i] * data[(ROW-1)-i][i]
and printed.
Code Solution
/*
C Program
+ Square of diagonal elements in matrix
*/
#include<stdio.h>
#define ROW 3
#define COL 3
void diagonal_squares(int data[ROW][COL])
{
printf("\n First :\n");
for (int i = 0; i < ROW; ++i)
{
printf("%4d",data[i][i]*data[i][i]);
}
printf("\n Second :\n");
for (int i = 0; i < ROW; ++i)
{
printf("%4d",data[(ROW-1)-i][i]*data[(ROW-1)-i][i]);
}
}
int main(){
int arr[ROW][COL]= {
{ 1,2,3},
{ 4,5,6},
{ 9,8,7},
};
diagonal_squares(arr);
return 0;
}
Output
First :
1 25 49
Second :
81 25 9
/*
C++ Program
Square of diagonal elements in matrix
*/
#include<iostream>
#define SIZE 3
using namespace std;
class MyMatrix {
public:
int size;
MyMatrix() {
//Assume that given matrix size is NXN
this->size = SIZE;
}
void diagonal_squares(int matrix[][SIZE]) {
cout << "\n First :\n";
for (int i = 0; i < this->size; ++i) {
cout << " " << matrix[i][i] *matrix[i][i];
}
cout << "\n Second :\n";
for (int i = 0; i < this->size; ++i) {
cout << " " << matrix[(this->size - 1) - i][i] *matrix[(this->size - 1) - i][i];
}
}
};
int main() {
int matrix[][SIZE] = {
{
1,
2,
3
},
{
4,
5,
6
},
{
9,
8,
7
}
};
MyMatrix obj ;
obj.diagonal_squares(matrix);
return 0;
}
Output
First :
1 25 49
Second :
81 25 9
/*
Java Program
Square of diagonal elements in matrix
*/
public class MyMatrix {
public int size;
public MyMatrix(int [][]matrix)
{
//Assume that given matrix size is NXN
this.size = matrix.length;
}
public void diagonal_squares(int [][]matrix)
{
System.out.print("\n First :\n");
for (int i = 0; i < this.size; ++i)
{
System.out.print(" "+matrix[i][i]*matrix[i][i]);
}
System.out.print("\n Second :\n");
for (int i = 0; i < this.size; ++i)
{
System.out.print(" "+matrix[(this.size-1)-i][i]*matrix[(this.size-1)-i][i]);
}
}
public static void main(String[] args)
{
//Define matrix element
int [][]matrix =
{
{ 1,2,3},
{ 4,5,6},
{ 9,8,7}
};
MyMatrix obj = new MyMatrix(matrix);
obj.diagonal_squares(matrix);
}
}
Output
First :
1 25 49
Second :
81 25 9
/*
C# Program
Square of diagonal elements in matrix
*/
using System;
public class MyMatrix {
int size;
MyMatrix(int[,] matrix) {
//Assume that given matrix size is NXN
this.size = matrix.GetLength(0);
}
public void diagonal_squares(int[,] matrix) {
Console.Write("\n First :\n");
for (int i = 0; i < this.size; ++i) {
Console.Write(" " + matrix[i,i] * matrix[i,i]);
}
Console.Write("\n Second :\n");
for (int i = 0; i < this.size; ++i) {
Console.Write(" " + matrix[(this.size - 1) - i,i] * matrix[(this.size - 1) - i,i]);
}
}
public static void Main(String[] args) {
int[,]
//Define matrix element
matrix = {
{
1,
2,
3
},
{
4,
5,
6
},
{
9,
8,
7
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.diagonal_squares(matrix);
}
}
Output
First :
1 25 49
Second :
81 25 9
<?php
/*
Php Program
Square of diagonal elements in matrix
*/
class MyMatrix {
public $size;
function __construct($matrix) {
//Assume that given matrix size is NXN
$this->size = count($matrix);
}
public function diagonal_squares($matrix) {
echo("\n First :\n");
for ($i = 0; $i < $this->size; ++$i) {
echo(" ". $matrix[$i][$i] *$matrix[$i][$i]);
}
echo("\n Second :\n");
for ($i = 0; $i < $this->size; ++$i) {
echo(" ". $matrix[($this->size - 1) - $i][$i] *$matrix[($this->size - 1) - $i][$i]);
}
}
}
function main() {
//Define matrix element
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(9, 8, 7));
$obj = new MyMatrix($matrix);
$obj->diagonal_squares($matrix);
}
main();
Output
First :
1 25 49
Second :
81 25 9
/*
Node Js Program
Square of diagonal elements in matrix
*/
class MyMatrix {
constructor(matrix) {
//Assume that given matrix size is NXN
this.size = matrix.length;
}
diagonal_squares(matrix) {
process.stdout.write("\n First :\n");
for (var i = 0; i < this.size; ++i) {
process.stdout.write(" " + matrix[i][i] *matrix[i][i]);
}
process.stdout.write("\n Second :\n");
for (var i = 0; i < this.size; ++i) {
process.stdout.write(" " + matrix[(this.size - 1) - i][i] *matrix[(this.size - 1) - i][i]);
}
}
}
function main(args) {
//Define matrix element
var matrix = [
[1, 2, 3],
[4, 5, 6],
[9, 8, 7]
];
var obj = new MyMatrix(matrix);
obj.diagonal_squares(matrix);
}
main();
Output
First :
1 25 49
Second :
81 25 9
# Python 3 Program
# Square of diagonal elements in matrix
class MyMatrix :
def __init__(self, matrix) :
# Assume that given matrix size is NXN
self.size = len(matrix)
def diagonal_squares(self, matrix) :
print("\n First :\n", end = "")
i = 0
while (i < self.size) :
print(" ", matrix[i][i] * matrix[i][i], end = "")
i += 1
print("\n Second :\n", end = "")
i = 0
while (i < self.size) :
print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i], end = "")
i += 1
def main() :
matrix = [
[1, 2, 3],
[4, 5, 6],
[9, 8, 7]
]
obj = MyMatrix(matrix)
obj.diagonal_squares(matrix)
if __name__ == "__main__":
main()
Output
First :
1 25 49
Second :
81 25 9
# Ruby Program
# Square of diagonal elements in matrix
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_reader :size
attr_accessor :size
def initialize(matrix)
# Assume that given matrix size is NXN
self.size = matrix.length
end
def diagonal_squares(matrix)
print("\n First :\n")
i = 0
while (i < self.size)
print(" ", matrix[i][i] * matrix[i][i])
i += 1
end
print("\n Second :\n")
i = 0
while (i < self.size)
print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i])
i += 1
end
end
end
def main()
matrix = [
[1, 2, 3],
[4, 5, 6],
[9, 8, 7]
]
obj = MyMatrix.new(matrix)
obj.diagonal_squares(matrix)
end
main()
Output
First :
1 25 49
Second :
81 25 9
/*
Scala Program
Square of diagonal elements in matrix
*/
class MyMatrix(var size: Int) {
def this(matrix: Array[Array[Int]]) {
//Assume that given matrix size is NXN
this(matrix.length);
}
def diagonal_squares(matrix: Array[Array[Int]]): Unit = {
print("\n First :\n");
var i: Int = 0;
while (i < this.size) {
print(" " + matrix(i)(i) * matrix(i)(i));
i += 1;
}
print("\n Second :\n");
i = 0;
while (i < this.size) {
print(" " + matrix((this.size - 1) - i)(i) * matrix((this.size - 1) - i)(i));
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val matrix: Array[Array[Int]] = Array(
Array(1, 2, 3),
Array(4, 5, 6),
Array(9, 8, 7));
val obj: MyMatrix = new MyMatrix(matrix);
obj.diagonal_squares(matrix);
}
}
Output
First :
1 25 49
Second :
81 25 9
/*
Swift Program
Square of diagonal elements in matrix
*/
class MyMatrix {
var size: Int;
init(_ matrix: [[Int]]) {
//Assume that given matrix size is NXN
self.size = matrix.count;
}
func diagonal_squares(_ matrix: [[Int]]) {
print("\n First :\n", terminator: "");
var i: Int = 0;
while (i < self.size) {
print(" ", matrix[i][i] * matrix[i][i], terminator: "");
i += 1;
}
print("\n Second :\n", terminator: "");
i = 0;
while (i < self.size) {
print(" ", matrix[(self.size - 1) - i][i] * matrix[(self.size - 1) - i][i], terminator: "");
i += 1;
}
}
}
func main() {
let matrix: [
[Int]
] = [
[1, 2, 3],
[4, 5, 6],
[9, 8, 7]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.diagonal_squares(matrix);
}
main();
Output
First :
1 25 49
Second :
81 25 9
The first line represents the squares of the principal diagonal elements, and the second line represents the squares of the secondary diagonal elements, just as we calculated earlier in the example.
Time Complexity
The time complexity of this code is O(ROW), where ROW is the number of rows in the matrix. This is because the
function diagonal_squares
loops over the diagonal elements, which are limited to the number of rows in
the square matrix. As the size of the matrix increases, the time complexity will grow linearly with the number of
rows.
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