Showing posts with label Normal of a matrix. Show all posts
Showing posts with label Normal of a matrix. Show all posts

Monday, 16 February 2015

Cpp Program to calculate norm of a matrix



This is a Cpp Program which calculates the norm of a given matrix.

Generally the norm is calculated as root of sum of squares of all the elements in a matrix.





Cpp - Program to calculate norm of a given matrix

PROGRAM :
#include <iostream>
#include<math.h>

using namespace std;

int main()
{
    int sum=0,rows,columns,i,j;
    float normal;
    cout <<" Enter order of matrix : \t";
    cin >> rows;
    cin >> columns;
    int matrix[rows][columns];
    cout << "Enter elements of the matrix \n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
        {
            cout << "Enter element m"<<i+1<<j+1<<" :\t";
            cin >> matrix[i][j];
        }
    }
    cout << "\n\n MATRIX is :\n";
    for(i=0; i<rows; i++)
    {
        for(j=0; j<columns; j++)
            cout <<"\t" << matrix[i][j] ;
        cout << "\n";

    }
    cout << "\n\nIts NORMAL is:\t";
    for(j=0; j<columns; j++)
    {
        for(i=0; i<rows; i++)
            sum  = sum +pow( matrix[i][j] , 2 );

    }
    normal = sqrt(sum);
    cout << normal;
    return 0;
}


OUTPUT :
Cpp - Program to calculate norm of a given matrix

Monday, 12 January 2015

Java Program to find the normal of a matrix



This is a Java Program which calculates the normal of a given matrix.

Generally normal of a matrix is calculated as the root of sum of squares of all the elements in a matrix.




PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class NormalMatrix {
 public static void main(String[] args) {
  double sum = 0;
  int rows, columns, i, j;
  double normal;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter order of matrix : \t");
  rows = scan.nextInt();
  columns = scan.nextInt();
  int matrix[][] = new int[rows][columns];
  System.out.print("Enter elements of the matrix :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++) {
    System.out.print("Enter element m" + (i + 1) + (j + 1) + ":\t");
    matrix[i][j] = scan.nextInt();
   }
  }
  scan.close();
  System.out.print("\n\nMATRIX is :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++)
    System.out.print(matrix[i][j] + "\t");

   System.out.print("\n");
  }
  System.out.print("\n\nIts NORMAL is:\n");
  for (j = 0; j < columns; j++) {
   for (i = 0; i < rows; i++)
    sum = sum + Math.pow(matrix[i][j], 2);

  }
  normal = Math.sqrt(sum);
  System.out.print(normal);
 }
}

OUTPUT :
Java - Normal of a matrix

Saturday, 10 January 2015

C Program to calculate the normal of a matrix


This is a C Program which calculates the normal of a given matrix.

Generally normal of a matrix is calculated as the root of sum of squares of all the elements in a matrix.


PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
    int sum=0,rows,columns,i,j;
    float normal;
    printf("Enter order of matrix : \t");
    scanf("%d%d",&rows,&columns);
    int matrix[rows][columns];   
    printf("Enter elements of the matrix \n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("Enter element m%d%d: \t",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
    printf("\n\nMATRIX is :\n");
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        printf("%d\t",matrix[i][j]);
        
        printf("\n");
    }
    printf("\n\nIts NORMAL is:\n");
    for(j=0;j<columns;j++)
    {
        for(i=0;i<rows;i++)
        sum  = sum +pow( matrix[i][j] , 2 );
        
    }
    normal = sqrt(sum);
    printf("%f\n",normal);
    return 0;
}

OUTPUT :
C -  Normal of a matrix