Program to find the normal and trace of a matrix

Problem Statement:- Program to Find the normal and trace of a matrix.

Definition of normal of a Matrix:- Normal is the sum of squares of all the elements of the matrix.


Definition of the trace of a Matrix:- Trance is the sum of the diagonal elements of Matrix.


Sample Input/Output:-


Sample Input First:2 2

4 5 7 8

Sample Output First: 

4.90

12

Sample Input Second: 3 3

5 9 3 9 4 2 2 0 1

Sample Output Second: 

5.91608

10


Data requirement:-

  Input Data:- row_size, col_size, matrix[][]


  Output Data:-normal, trance


  Additional Data:- i, j, sum


Program in C

Here is the source code of the C Program to find the normal and trace of a matrix.


Code:


#include<stdio.h> #include<math.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int trace=0,sum=0; //Calculate sum of the diagonals element //and Calculate sum of all the element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { trace+=matrix[i][j]; } sum+=matrix[i][j]; } } double normal=sqrt(sum); //Display the normal and trace of the matrix printf("Normal Of the Matrix is: %0.2lf\n",normal); printf("Trace Of the Matrix is: %d\n",trace); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 4 5 7 8 Normal Of the Matrix is: 4.90 Trace Of the Matrix is: 12


Program in C++

Here is the source code of the C++ Program to find the normal and trace of a matrix.


Code:


#include<iostream> #include<math.h> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int trace=0,sum=0; //Calculate sum of the diagonals element //and Calculate sum of all the element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { trace+=matrix[i][j]; } sum+=matrix[i][j]; } } double normal=sqrt(sum); //Display the normal and trace of the matrix cout<<"Normal Of the Matrix is: "<<normal; cout<<"\nTrace Of the Matrix is: "<<trace; }

Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 5 9 3 9 4 2 2 0 1 Normal Of the Matrix is: 5.91608 Trace Of the Matrix is: 10


Program in Java

Here is the source code of the Java Program to find the normal and trace of a matrix.


Code:


import java.util.Scanner; public class ComputeNormalAndTraceOfMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int trace=0,sum=0; //Calculate sum of the diagonals element //and Calculate sum of all the element for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i==j) { trace+=matrix[i][j]; } sum+=matrix[i][j]; } } double normal=Math.sqrt(sum); //Display the normal and trace of the matrix System.out.println("Normal Of the Matrix is: "+normal); System.out.println("Trace Of the Matrix is: "+trace); cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 9 3 4 0 Normal Of the Matrix is: 4.0 Trace Of the Matrix is: 9 


Program in Python

Here is the source code of the Python Program to find the normal and trace of a matrix.


Code:


import math # Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) # Calculate sum of the diagonals element # and Calculate sum of all the element trace=0 sum=0 for i in range(0, row_size): for j in range(0, col_size): if i==j: trace += matrix[i][j] sum+=matrix[i][j] normal=math.sqrt(sum) # Display the normal and trace of the matrix print("Normal Of the Matrix is: ",normal) print("Trace Of the Matrix is: ",trace)


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 7 3 2 1 4 9 2 1 Normal Of the Matrix is: 5.744562646538029 Trace Of the Matrix is: 6


Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments