0% found this document useful (0 votes)
18 views15 pages

Java 3x3 Matrix Operations Guide

double dimensional array

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Java 3x3 Matrix Operations Guide

double dimensional array

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Double Dimensional Array

1. WAP in Java to store the numbers in a 3 by 3 Matrix in a Double Dimensional Array.


Find the sum of all the numbers of the Matrix and display the sum of all elements.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of elements = 42
import [Link].*;
class Matrix1
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int A[][]=new int[3][3];
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
A[i][j]=[Link]();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
}
[Link]("sum of matrix = "+sum);
}
}
class Matrix1
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
}
[Link]("sum of matrix = "+sum);
}
}
2. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the number of each row.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Row 1 = 15
Sum of Row 2 = 11
Sum of Row 3 = 16
class Matrix2
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
[Link]("sum of Row "+(i+1)+" = "+sum);
sum=0;
}

}
}
3. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the number of each columns.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Column 1 = 16
Sum of Column 2 = 14
Sum of Column 3 = 12
class Matrix3
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+A[i][j];
}
[Link]("sum of Column "+(i+1)+" = "+sum);
sum=0;
}

}
}
4. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the numbers in the Left diagonal.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum Left diagonal = 6
class Matrix4
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+A[i][j];
}
}
[Link]("sum of left diagonal = "+sum);
}
}
5. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Find the sum of the numbers in the Right diagonal.
Sample Input:
1 5 9
7 3 1
8 6 2
Sample Output:
Sum of Right diagonal = 20
class Matrix5
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==2)
sum=sum+A[i][j];
}
}
[Link]("sum of right diagonal = "+sum);
}
}
6. WAP in Java to store the numbers in 3x3 matrix in a Double Dimensional Array.
Display the Highest and the Lowest number among the stored number in the matrix.
Sample Input:
1 5 9
7 3 4
8 6 2
Sample Output:
Highest number = 9
Lowest number = 1
class Matrix3
{
public static void main(int A[][])
{
int i,j,High=A[0][0],Low=A[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]>High)
High=A[i][j];
else if(A[i][j]<Low)
Low=A[i][j];
}
}
[Link]("Smallest number = "+Low);
[Link]("Greatest number = "+High);
}
}
7. WAP in Java to store the numbers in a 3x3 matrix in Double Dimensional Array.
Display the elements of the matrix by replacing each element of the left diagonal with the zero.
Sample Input: Sample Output:
1 5 9 0 5 9
7 3 1 7 0 1
8 6 2 8 6 0
class Matrix3
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
[Link]("0");
else
[Link](A[i][j]);
}
[Link]();
}
}
}
8. WAP in java to store the numbers in a 3x3 matrix in Double Dimensional Array.
Display the sum of the border elements of the matrix.
Sample Input:
1 5 9
7 3 4
8 6 2
Sample Output:
Sum of Border elements = 42
class Matrix8
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==0||j==0||i==2||j==2)
sum=sum+A[i][j];
}
}
[Link]("sum of border elements = "+sum);
}
}

9. WAP to accept value into 3x3 array and check if it is a Special array.
(An Array is special array if the sum of the even elements = sum of odd elements)
Sample Input:
4 5 6
5 3 2
4 2 5
Sample Output:
Sum of even elements = 4+6+2+4+2=18
Sum of odd elements = 5+5+3+5=18
class Matrix9
{
public static void main(int A[][])
{
int i,j,Esum=0,Osum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]%2==0)
Esum=Esum+A[i][j];
else
Osum=Osum+A[i][j];
}
}
if(Esum==Osum)
[Link]("It is Special Array");
else
[Link]("It is not Special Array");
}
}

10. WAP to create a Double Dimension character Array as:


char ch[][]=new ch[3][3].

The program store nine different uppercase letters in the double dimensional array. Display the letter which are
vowels.
Sample Input:
P E R
W D A
U M K
Sample Output: E,A,U
class Matrix10
{
public static void main(char A[][])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(A[i][j]=='A'||A[i][j]=='E'||A[i][j]=='I'||A[i][j]=='O'||A[i][j]=='U')
[Link](A[i][j]+" ");
}
}
}
}

11. WAP in Java to create a 4x4 matrix. Now swap the elements of 1st row and 4th row. (i.e. interchange the
elements of the 1st row with the 4th row) . Display the result.
Sample Input: Sample Output:
12 4 1 9 2 41 7 9
3 2 5 8 3 2 5 8
1 3 5 7 1 3 5 7
2 41 7 9 12 4 1 9
class Matrix10
{
public static void main(int A[][])
{
int i,j,temp;
for(i=0;i<3;i++)
{
temp=A[0][i];
A[0][i]=A[2][i];
A[2][i]=temp;
}

[Link]("Matrix are : ");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
[Link](A[i][j]);
}
[Link]();
}
}
}
13. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Print the Left diagonal elements in matrix format.
Sample Input: Sample Output:
1 5 9 1
7 3 1 3
8 6 2 2
class Matrix12
{
public static void main(int A[][])
{
int i,j;
[Link]("Left Diagonal Matrix are : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}
14. WAP in Java to store the numbers in a 3x3 Matrix in Double Dimensional Array.
Print the Right diagonal elements in matrix format.
Sample Input: Sample Output:
1 5 9 9
7 3 1 3
8 6 2 8
class Matrix12
{
public static void main(int A[][])
{
int i,j;
[Link]("Right Diagonal Matrix are : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==2)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}
15. WAP in java to store he numbers in a 4x4 matrix in a Double Dimensional Array.
Display only the elements available above the left diagonal of the matrix.
Sample Input: Sample Output:
2 4 1 9 4 1 9
3 2 5 8 5 8
1 3 5 7 7
2 4 7 9
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}
16. WAP in Java to store the numbers in 3x3 matrix in Double Dimensional Array. Display the sum of the
elements that are above and below the left diagonal matrix.
Sample Input:
1 2 3
4 5 6
7 8 9
Sample Output:
Sum of the elements that are above and below the left diagonal = 30
class Matrix12
{
public static void main(int A[][])
{
int i,j,sum=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i!=j)
sum=sum+A[i][j];
}
}
[Link]("Sum of the elements that are above and below the left diagonal = "+sum);
}
}

18. WAP in Java to store numbers in 4x4 matrix. Display the numbers which are below left diagonal as shown
below.
Sample Input: Sample Output:
1 2 3 4
5 6 7 8 5
3 4 5 8 3 4
9 3 2 1 9 3 2
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}
19. WAP in Java to store the numbers in 4x4 matrix. Display the numbers which are above the right diagonal as
shown below.
Sample Input: Sample Output:
1 2 3 4 1 2 3
5 6 7 8 5 6
3 4 5 8 3
9 3 2 1
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if((i+j)<3)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}

20. WAP in Java to store numbers in 4x4 matrix. Display the numbers which are below the right diagonal as
shown.
Sample Input: Sample Output:
1 2 3 4
5 6 7 9 9
3 4 5 8 5 8
9 3 2 1 3 2 1
class Matrix12
{
public static void main(int A[][])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if((i+j)>3)
[Link](A[i][j]);
else
[Link](" ");
}
[Link]();
}
}
}

21. WAP in Java to create a 3x3 matrix and store the different numbers. Display the highest value of each row.
Sample Input: Sample Output:
1 12 3
4 50 6
7 8 1
Sample Output:
Highest value of row 1 = 12
Highest value of row 1 = 50
Highest value of row 1 = 8
class Matrix12
{
public static void main(int A[][])
{
int i,j,High;
for(i=0;i<4;i++)
{
High=A[i][0];
for(j=0;j<4;j++)
{
if(A[i][j]>High)
High=A[i][j];
}
[Link]("Highest value of row "+(i+1)+"="+High);
}
}
}
[Link] in java to create a 3x3 Square Matrix and store elements in it. The programmer wants to check whether
the Matrix is Symmetric or not.
(A square matrix is said to be Symmetric , if the elements of the ith row and jth columns is equal to the element
of the jth row and ith columns.
Sample Input:
1 2 3
2 4 5
3 5 6
Sample Output: It is a Symmetric matrix.
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(A[i][j]!=A[j][i])
{
c++;
break;
}
}
}
if(c==0)
[Link]("It is Symmetric Matrix ");
else
[Link]("Not");
}
}
23. Transpose a matrix mean to change row elements into column and the column elements into rows.
WAP to store the number in a 4x4 matrix in Double Dimensional Array and display the transpose of
the matrix.
Sample Input: Sample Output:
22 14 23 25 22 81 58 55
81 26 31 10 14 26 64 33
58 64 17 12 23 31 17 26
55 33 26 14 25 10 12 14
class Matrix12
{
public static void main(int A[][])
{
int i,j;
int B[][]=new int[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
B[i][j]=A[j][i];
}
}
[Link]("Transpose Matrix are : ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
[Link](B[i][j]);
}
[Link]();
}
}
}
24. WAP in java to input 2-Dsquare matrix and check whether it is a Lower Triangular Matrix or not.
(Lower Triangular Matrix is a square matrix in which all the entries above the main diagonals are zero. The
entries below or on the main diagonal themselves may or may not be zero.)
Sample Input:
5 0 0 0
3 1 0 0
4 9 4 0
6 8 7 2
Sample Output: It is Lower Triangular Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j>i && A[i][j]!=0)
c++;
}
}
if(c==0)
[Link]("it is lower triangular matrix");
else
[Link]("it is not lower triangular matrix");
}
}

25. 24. WAP in java to input 2-Dsquare matrix and check whether it is a Upper Triangular Matrix or not.
(Upper Triangular Matrix is a square matrix in which all the entries below the main diagonals are zero. The
entries above or on the main diagonal themselves may or may not be zero.)
Sample Input:
5 3 0 7
0 1 9 8
0 0 4 6
0 0 0 2
Sample Output: It is an Upper Triangular Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j<i && A[i][j]!=0)
c++;
}
}
if(c==0)
[Link]("it is upper triangular matrix");
else
[Link]("it is not upper triangular matrix");
}
}
26. WAP in java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.
(A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements
except main diagonal are zero)
Sample Input:
5 0 0 0
0 5 0 0
0 0 5 0
0 0 0 5
Sample Output: It is Scalar Matrix
class Matrix12
{
public static void main(int A[][])
{
int i,j,s=A[0][0],c=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j && A[i][j]!=s)
c++;
if(i!=j && A[i][j]!=0)
c++;
}
}
if(c==0)
[Link]("it is Scalar matrix");
else
[Link]("it is not Scalar matrix");
}
}
27. Write a program in Java to enter natural numbers in a double dimensional array m x n (where m
is the number of rows and n is the number of columns). Display the new matrix in such a way that
the new matrix is the mirror image of the original matrix.
Sample Input: Sample Output:
1 2 3 4 4 3 2 1
7 8 11 18 18 11 8 7
9 10 90 12 12 90 10 9
17 18 70 13 13 70 18 17
class Matrix12
{
public static void main(int A[][],int m,int n)
{
int i,j,k;
int B[][]=new int[m][n];
for(i=0;i<m;i++)
{
k=0;
for(j=n-1;j>=0;j--)
{
B[i][k++]=A[i][j];
}
}
[Link]("Mirror image are : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
[Link](B[i][j]);
}
[Link]();
}
}
}

Common questions

Powered by AI

The program uses a temporary variable to swap elements between the first and fourth rows. For each column index, it saves the first row's element temporarily, assigns the fourth row's element to the first row, then restores the saved element to the fourth row, effectively swapping the rows .

The program initializes variables to hold the highest and lowest values to the first element of the matrix. It then iterates through each element of the matrix. For each element, it checks if it is greater than the current highest or less than the current lowest, updating the variables accordingly. At the end of the iteration, the program outputs these values .

The Java program initializes a sum variable to 0. It then iterates over each element in the 3x3 matrix using nested loops. For each element, it adds the element's value to the sum variable. After completing the iterations, it outputs the total sum .

The program checks symmetry by comparing each element at position (i,j) with the corresponding element at (j,i). If any pair of elements are not equal, the matrix is not symmetric. It iterates through the matrix and breaks the loop if it finds any mismatch, thereby confirming matrix symmetry if no mismatches are found .

A Lower Triangular Matrix has zeroes above the main diagonal, while an Upper Triangular Matrix has zeroes below it. The program checks all elements above (j > i) or below (j < i) the main diagonal to verify they are zero and reports accordingly. If all respective elements are zero, it confirms the matrix type .

To create a mirror image of a matrix, the program constructs a new matrix and reverses the elements in each row of the original matrix. By iterating backwards through each row and copying elements to a new matrix row, it effectively mirrors the original double array .

The program uses nested loops to iterate over the matrix elements. It checks if the column index is greater than the row index (j > i) and prints the element if true, displaying only elements above the main diagonal .

To determine if a 3x3 matrix is a Special Array, the program calculates separate sums for even and odd elements. It iterates through each element, adding even elements to an even sum and odd elements to an odd sum. If the two sums are equal, the matrix is classified as a Special Array .

The program iterates through a 3x3 character matrix and checks each character if it is a vowel ('A', 'E', 'I', 'O', 'U'). If a character is a vowel, it prints the character. This is done for each element, ensuring every vowel is displayed .

For the left diagonal, the program sums elements where the row index equals the column index. For the right diagonal, it sums elements where the row index plus the column index equals the matrix size minus one. These sums are calculated separately and outputted .

You might also like