TWO DIMENSIONAL ARRAY -(MATRIX)programs
[Link] a program to enter the size of a two dimensional integer [Link]
create the array and enter elements in to array using scanner class and display
the array in the correct format .
//entering elements into the matrix and display the matrix
import [Link].*;
public class abc
{
public void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the no of rows");
int m=[Link]();
[Link]("enter the no of columns");
int n=[Link]();
//creation of the array
int a[][]=new int[m][n];
//entering elements into array
[Link]("enter the elements into array");
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=[Link]();
//display
[Link]. println(\nThe array is”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
[Link](a[i][j]+" ");
[Link]();
}
}
}
2..Write a programto enter the size of the integer [Link] create the
matrix and enter elements in to array using scanner class and display the array
in the correct format and also find the sum of all the elements and display it
/*entering elements into 2 dimensional array
and display the array and sum of all the elements*/
import [Link].*;
public class abc1
{
public void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the no of rows");
int m=[Link]();
[Link]("enter the no of columns");
int n=[Link]();
//creation of the array
int a[][]=new int[m][n];
//entering elements into array and finding sum
[Link]("enter the elements into array");
int i,j,s=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
a[i][j]=[Link]();
s=s+a[i][j];
}
//display
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
[Link](a[i][j]+" ");
[Link]();
}
[Link]("sum="+s);
}
}
3. Develop a program to enter elements into 2 dimensional array and find
the transpose and displaythe original array and transpose of the array.
/*entering elements into 2 dimensional array and find
the transpose and displaythe original array and transpose of the array*/
import [Link].*;
public class abc2
{
public void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the no of rows");
int m=[Link]();
[Link]("enter the no of columns");
int n=[Link]();
//creation of the arrays
int a[][]=new int[m][n];
int b[][]=new int[n][m];
//Entering values into the array
[Link]("enter the elements into the array");
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=[Link]();
//finding transpose
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[i][j]=a[j][i];
//display of original array
[Link]("\nOriginal array");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
[Link](a[i][j]+" ");
[Link]();
}
//display of transpose array
[Link]("\nTranspose array");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
[Link](b[i][j]+" ");
[Link]();
}
}
}
4)Write a program to create a square matrix and enter values and find the sum of
left diagonal and right diagonal separately and print the result
// To find the sum of left diagonal and right diagonal of a square Matrix
import [Link].*;
public class abc3
{
public void main()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the size of the square matrix");
int m=[Link]();
//creation of the array
int a[][]=new int[m][m];
//entering values into matrix
[Link]("enter the elements into the array");
int i,j,d1=0,d2=0;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][j]=[Link]();
//finding sum of left diagonal and right diagonal
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
if(i==j)
d1=d1+a[i][j]; //sum of left diagonal
if((i+j)==(m-1))
d2=d2+a[i][j]; //sum of right diagonal
}
[Link]("sum of left diagonal="+d1);
[Link]("sum of right diagonal="+d2);
}
}
[Link] a program to create a 3x3 matrix and enter values and find the sum of
even numbers and product of odd nos and print the result
//pgm to find the sum of even nos and product of odd nos of a matrix
import [Link].*;
public class abc4
{
public void main()
{
Scanner sc=new Scanner([Link]);
//creation of the array
int a[][]=new int[3][3];
//entering values
[Link]("enter the elements into the array");
int i,j,sum=0,p=1;
//checking even or odd
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
a[i][j]=[Link]();
if(a[i][j]%2==0)
sum=sum+a[i][j]; //sum of even nos
else
p=p*a[i][j]; //product of odd nos
}
[Link]("sum of the even elements="+sum);
[Link]("product of odd elements="+p);
}
}