File: Untitled Document 1 Page 1 of 3
package Array;
/**
* Write a description of class MatrixA here.
*
* @author (your name)
* @version (a version number or a date)
*/
import [Link].*;
public class MatrixA
{
float [] [] mat;//This instance variable stores the value of a matrix
//This function reads a matrix
void readMat()
{
Scanner sc = new Scanner([Link]);
int r = 0;
int c = 0;
[Link]("Enter the number of rows");
r = [Link]();
[Link]("Enter the number of columns");
c = [Link]();
mat = new float[r][c];
for(int i = 0; i<r;i+=1)
{
for(int n = 0; n<c; n+=1)
{
[Link]("Enter the element of row " + (i+1) + " and columns " +
(n+1));
mat[i][n] = [Link]();
}
}
}
//It takes a 2D array and a scalar value and multiplies every array in the element by
a scalar
void scalarMultiplication (float [][] mat , float s)
{
for(int i = 0; i<[Link];i++)
{
for(int k = 0;k<mat[i].length;k++)
{
mat[i][k] = (mat[i][k])*s;
}
}
printMat(mat);
/**
* Output:
*
* 4.0 6.0
6.0 8.0
*/
}
//This function prints a matrix
void printMat(float [][] mat)
{
for(int c = 0; c < [Link]; c++)
{
for(int m = 0 ;m<mat[c].length;m++ )
{
[Link](mat[c][m] + "\t");
}
File: Untitled Document 1 Page 2 of 3
[Link]();
}
/**
* Output:
*
* 4.0 6.0
6.0 8.0
*/
}
//This function adds two matrices
void scalarAddition (float [][] mat , float [][] mat2)
{
float [][] mat_final = new float[[Link]][mat[0].length];
for(int i = 0; i<[Link];i++)
{
for(int k = 0;k<mat[i].length;k++)
{
mat_final[i][k] = mat[i][k] + mat2[i][k];
}
}
printMat(mat_final);
/**
* Output:
*
* 6.0 3.0
4.0 7.0
*/
}
//This function changes the rows to column and the column to rows
void transposeMat (float [][] mat)
{
float [][] mat_final = new float[mat[0].length][[Link]];
for(int i = 0; i<mat[0].length;i+=1)
{
for(int k = 0;k<[Link];k++)
{
mat_final[i][k] = mat[k][i];
}
}
printMat(mat_final);
/**
* Output:
*
* 2.0 3.0
3.0 4.0
*/
}
//This function rotates the matrix by 180 degree
void rotate1(float [][] mat)
{
int l = mat[0].length-1;
float [][] mat_final = new float[[Link]][mat[0].length];
for(int i = 0; i<=l;i++)
{
for(int k = 0; k<[Link];k++)
{
mat_final[k][l-i] = mat[i][k];
}
}
printMat(mat_final);
/**
* Output:
File: Untitled Document 1 Page 3 of 3
7.0 4.0 1.0
8.0 5.0 2.0
9.0 6.0 3.0
*/
}
//This function rotates the matrix's ring according to the input
void rotate2(float [][] mat , int r)//not working ask sir.
{
float [][] mat_final = new float[[Link]][mat[0].length];
int l = mat[0].length-1;
for(int i = 0; i<=l;i++)
{
for(int k = 0; k<[Link];k++)
{
if(k<=r||i<=r||k>=([Link] - r)||i>=(l-r)||(k==l)&&(i==l))
mat_final[k][l-i] = mat[i][k];
else
mat_final[i][k]=mat[i][k];
}
}
printMat(mat);
[Link]("-----------------------------------------------------");
printMat(mat_final);
}
}