Square Elements of Two Dimensional Array

Posted by devappandroid February - 15 - 2013 - Friday ADD COMMENTS

This is a simple java program for implementing the two dimensional array program and its square. This session provide you the best explanation with mathematical operation.

Description of progarm:

We are going to display the square of two matrix. Firstly, we have to define a class “SquareMatrix“.  Then we take an integer type array that contains integer type values. After this, we use two ‘for’ loop that denotes rows and columns of a matrix. After getting both the matrix with us we need to square both matrix. When we go to square this array then we use “square[i][j] =square[i][j] * square[i][j]“. So, the output will be displayed on the screen command prompt by using the println() method.

Here is the code of this program

public class SquareMatrix {
public static void main(String[] args) {
int square[][]= {{1,3,5},{2,4,6}};
System.out.println(“Your Original Matrix: “);
for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
System.out.print(square[i][j] + ” “);
}
System.out.println();
}
for(int i = 0; i for(int j = 0; j square[i][j] = square[i][j] * square[i][j];
}
}
System.out.println(“Matrix after changes: “);
for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
System.out.print(square[i][j] + ” “);
}
System.out.println();
}
}
}

Enjoy Code

Post to Twitter

Multiplication of two Matrix In JAVA

Posted by devappandroid January - 8 - 2013 - Tuesday ADD COMMENTS

This is a simple java program that teaches you for multiplying two matrix to each other. Here providing you Java source code with understanding the Java developing application program. We are going to make a simple program that will multiply two matrix. Two dimensional array represents the matrix. 

Now, make this program, you have to declare two multidimensional array of type integer. Program uses two for loops to get number of rows and columns by using the array1.length. After getting both matrix then multiply to it. Both matrix will be multiplied to each other by using ‘for’ loop. So the output will be displayed on the screen command prompt by using the println() method.

Here is the code of this program:

class MatrixMultiply{
  public static void main(String[] args)  {
  int array[][] {{5,6,7},{4,8,9}};
  int array1[][] {{6,4},{5,7},{1,1}};
  int array2[][] new int[3][3];
  int x= array.length;
  System.out.println("Matrix 1 : ");
  for(int i = 0; i < x; i++) {
  for(int j = 0; j <= x; j++) {
  System.out.print(" "+ array[i][j]);
  }
  System.out.println();
  }  
  int y= array1.length;
  System.out.println("Matrix 2 : ");
  for(int i = 0; i < y; i++) {
  for(int j = 0; j < y-1; j++) {
  System.out.print(" "+array1[i][j]);
  }  
  System.out.println();
  }
  
  for(int i = 0; i < x; i++) {
  for(int j = 0; j < y-1; j++) {
  for(int k = 0; k < y; k++){
  
  array2[i][j+= array[i][k]*array1[k][j];
  }
  }  
 }
  System.out.println("Multiply of both matrix : ");
  for(int i = 0; i < x; i++) {
  for(int j = 0; j < y-1; j++) {
  System.out.print(" "+array2[i][j]);
  }  
  System.out.println();
  }
  }
}

Enjoy Code.

Post to Twitter

Sum of two Matrix In JAVA

Posted by devappandroid December - 4 - 2012 - Tuesday ADD COMMENTS

We are going to calculate the sum of two matrix and containing its rows and columns.

To make this program, we need to declare two dimensional array of type integer. Firstly it calculates the length of the both the arrays. Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix.

After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with matrix1[i][j]+matrix2[i][j]. The output will be displayed by using the println() method.

Here is the code of this program:

class MatrixSum{
public static void main(String[] args)  {
int matrix1[][]= {{7,8,9},{1,5,2}};
int matrix2[][]= {{1,6,4},{2,7,3}};
System.out.println("Number of Row= " + matrix1.length);
System.out.println("Number of Column= " + matrix1[1].length);
int l = matrix1.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < l; i++) {
for(int j = 0; j <= l; j++) {
System.out.print(" "+ matrix1[i][j]);
}
System.out.println();
}
int m = matrix2.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < m; i++) {
for(int j = 0; j <= m; j++) {
System.out.print(" "+matrix2[i][j]);
}
System.out.println();
}
System.out.println("Addition of both matrix : ");
for(int i = 0; i < m; i++) {
for(int j = 0; j <= m; j++) {
System.out.print(" "+(matrix1[i][j]+matrix2[i][j]));
}
System.out.println();
}
}
}

Enjoy Code.

Post to Twitter

Matrix Example in Java

Posted by devappandroid November - 28 - 2012 - Wednesday ADD COMMENTS

In Java tutorial, you will learn about Array and Matrix. If we create a variable of integer type then, the array of int can only store the int values. It can’t store other than int data type.

Defination of Matrix: A Matrix is a collection of all data in rows and columns format.

Description of program:

Now, we are going to Implement a matrix. To make a program over the two dimensional array, first of all we have to declare class named as SimpleMatrixExample that has one static method matrixoutput() which takes integer type array and represents it. For displaying the matrix we need to its rows and column by using the MatrixArray.length method. Now, we use the for loop to print all the values stored in the array. At last use the main() method inside which we are going to declare the values of the multidimensional array which we are going to use. Call the matrixoutput() method inside the main method. The output will be displayed to the user by println() method.

Here is the code of this Example:

class SimpleMatrixExample {
  public static void main(String[] args)  {
  int MatrixArray[][]{{7,4,1},{8,5,2}};
  System.out.println("Row size= " MatrixArray.length);
  System.out.println("Column size = " + MatrixArray[1].length);
  matrixoutput(array);
  }
  
 public static void matrixoutput(int[][] array) {
 int rowSize = MatrixArray.length;
 int columnSize = MatrixArray[0].length;
 for(int i = 0; i <= 1; i++) {
 System.out.print("[");
 for(int j = 0; j <= 2; j++) {
 System.out.print(" " + MatrixArray[i][j]);
 }
 System.out.println(" ]");
 }
 System.out.println();
 }
}

Enjoy Code.

Post to Twitter

Average of Array

Posted by devappandroid October - 18 - 2012 - Thursday ADD COMMENTS

In this section, we will learn how to get an average of array. For this, first of all we have to define a class name “Average” that has double type array to contain some values. Now we take a double type data that calculates the average of array (result/nums.length). And finally it will display the result on the command prompt with message by using the System.out.println().

Here is the code of this program:

public class Average{
  public static void main(String[] args) {
  double nums[]={1.0,2.3,3.4,4.5,40.5};
  double result=0.0;
  int i=0;
  for(i=0; i < nums.length; i++){
  result=result + nums[i];
  }
  System.out.println("Average is =" + result/nums.length);
  }
}

Enjoy Code…..

Post to Twitter