0% found this document useful (0 votes)
1 views31 pages

2D-Array

The document provides an overview of 2D arrays in Java, including their declaration, instantiation, and basic operations such as reading, printing, adding matrices, and finding transposes. It also includes examples of calculating total and average marks for students, summing rows and columns, and determining the frequency of odd and even numbers in a matrix. Additionally, it discusses sparse matrices and how to identify them based on the count of zero elements.

Uploaded by

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

2D-Array

The document provides an overview of 2D arrays in Java, including their declaration, instantiation, and basic operations such as reading, printing, adding matrices, and finding transposes. It also includes examples of calculating total and average marks for students, summing rows and columns, and determining the frequency of odd and even numbers in a matrix. Additionally, it discusses sparse matrices and how to identify them based on the count of zero elements.

Uploaded by

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

2D Array

Types of Array
• Single Dimensional Array
• Multidimensional Array [2D]
2D Array
Syntax to Declare 2D Array

dataType[][] arrayRefVar; (or)

dataType [][]arrayRefVar; (or)

dataType arrayRefVar[][]; (or)

dataType []arrayRefVar[];
instantiate Multidimensional

int[][] arr=new int[3][3];//3 row and 3 column

arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4; int arr[][]={{1,2,3},{4,5,6},{7,8,9}};
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
for ( int i=0; i<n ;i++)
{
for (int j=0; j<n; j++)
{
a[i][j] = 0;
}
}
Java program to read 2D matrix
import [Link]; and print the 2D matrix
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
// Read number of rows and columns
[Link]("Enter number of rows: ");
int rows = [Link]();

[Link]("Enter number of columns: ");


int cols = [Link]();

// Declare 2D array
int matrix[][] = new int[rows][cols];
// Read array elements
[Link]("Enter the matrix elements:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = [Link]();
}
}
// Print 2D array
[Link]("The matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}
}
}
Enter number of rows: 3
Enter number of columns: 3
Enter the matrix elements:
10 20 30
40 50 60
70 80 90

The matrix is:


10 20 30
40 50 60
70 80 90
Addition of two matrix
import [Link];
public class sample Matrix addition program in java
{
public static void main(String[] args) {
Scanner ed = new Scanner([Link]);
int row, col, i, j;
[Link]("Enter number of rows");
row = [Link]();
[Link]("Enter number of column");
col = [Link]();
int[][] a = new int[row][col];
int[][] b = new int[row][col];
int[][] sum = new int[row][col];
[Link]("Enter first matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
a[i][j] = [Link]();
} }
[Link]("Enter Second matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
b[i][j] = [Link]();
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
sum[i][j] = b[i][j] + a[i][j];
}
}
[Link]("Sum of two matrices");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
[Link](sum[i][j]+" ");
}
[Link](" ");
}
}
}
import [Link];
public class Transpose Java Program to Find Transpose of a Matrix
{
public static void main(String args[])
{
int i, j;
[Link]("Enter total rows and columns: ");
Scanner s = new Scanner([Link]);
int row = [Link]();
int column = [Link]();
int array[][] = new int[row][column];
[Link]("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{ Transpose of a Matrix in Java is
array[i][j] = [Link]();
[Link](" "); the new matrix formed by
}
} interchanging its rows and
columns.
[Link]("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
[Link](array[i][j]+" ");
}
[Link](" ");
}
[Link]("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
[Link](array[j][i]+" ");
}
[Link](" ");
}
}
}
Enter total rows and columns:
2
2
Enter matrix:
1235
The above matrix before Transpose is
12
35
The above matrix after Transpose is
13
25
A college wants to automate the result processing of its students. The
marks obtained by each student in different subjects are stored in a
two-dimensional array, where each row represents a student and
each column represents the marks obtained in a subject. Write a Java
program to read the marks of all students, calculate the total marks
and average marks for each student, and determine whether the
student has passed or failed. A student is declared Pass only if he/she
scores at least 40 marks in every subject; otherwise, the student is
declared Fail. Display the total marks, average marks, and result of
each student.
[Link]("Enter the number of students: ");
int students = [Link]();
[Link]("Enter the number of subjects: ");
int subjects = [Link]();
int marks[][] = new int[students][subjects];
[Link]("Enter the marks:");
for (int i = 0; i < students; i++)
{
[Link]("Student " + (i + 1) + ":");
for (int j = 0; j < subjects; j++)
{
marks[i][j] = [Link]();
}
}
Sample Input:
Enter the number of students: 2
Enter the number of subjects: 4
Sample output:
Enter the marks:
Student Result
Student 1:
Student 1
78 85 67 90
Total = 320
Student 2:
Average = 80.00
65 35 80 72
Result = Pass
Student 2
Total = 252
Average = 63.00
Result = Fail
Write a Java program to find the sum of each row and the
sum of each column in a given matrix

Sample output:

Enter row and column size:


33
Enter Matrix:
20 19 18
17 16 15
14 13 12
Row 1 sum = 57
Row 2 sum = 48
Row 3 sum = 39
Column 1 sum = 51
Column 2 sum = 48
Column-3 sum = 45
//Compute row-wise
for (int i = 0; i < rows; i++)
{
int rSum = 0;
for (int j = 0; j < cols; j++)
{
rSum += arr[i][j];
}
[Link]("Row " + (i + 1) + " sum = " + rSum);
}
import [Link];
public class row_col_sum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();
[Link]("Enter number of columns: ");
int cols = [Link]();

int[][] matrix = new int[rows][cols];

// Input: Matrix elements


[Link]("Enter the matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
// Sum of each row
for (int i = 0; i < rows; i++) {
int rowSum = 0;
for (int j = 0; j < cols; j++) {
rowSum += matrix[i][j];
}
[Link]("Sum of Row " + (i + 1) + " = " + rowSum);
}
// Sum of each column
for (int j = 0; j < cols; j++)
{
int colSum = 0;
for (int i = 0; i < rows; i++)
{
colSum += matrix[i][j];
}
[Link]("Sum of Column " + (j + 1) + " = " + colSum);
}
JAVA program to find the sum of diagonal
elements of a square matrix
// Check for main diagonal element.
if (i == j)
{
Enter the number of rows: sum += a[i][j];
3 }
Enter the number of columns:
3
Enter the elements of the matrix
145673456
The elements of the matrix
1 4 5
6 7 3
4 5 6
SUM of DIAGONAL elements of the matrix = 14
import [Link].*;
class diagnolsum
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);

int i,j,row,col,sum=0;
[Link]("Enter the number of rows:");
row = [Link]();
[Link]("Enter the number of columns:");
col = [Link]();

int mat[][] = new int[row][col];

[Link]("Enter the elements of the matrix") ;


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mat[i][j] = [Link]();
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j) //this condition checks for diagonal
{
sum = sum + mat[i][j];
}
}
}

[Link]("SUM of DIAGONAL elements of the matrix = "+sum) ;


}
}
Java Program to Find the Frequency of Odd and
Even Numbers in Matrix

Enter number of rows in matrix:3


Enter number of columns in matrix:3
Enter all the elements of matrix:
135246789
Given Matrix:
135
246
789
Even number frequency:4
Odd number frequency:5
int even=0,odd=0; //Variables to store even and odd
elements

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(arr[i][j]%2==0)
{
even++;
}
else
{
odd++;
}
}
}
A sparse matrix is a matrix in which most of the
elements are 0.
import [Link];
public class Sparse
{
public static void main(String args[])
{
int i, j, zero = 0, count = 0;
int array[][] = new int[10][10];
[Link]("Enter total rows and columns: ");
Scanner s = new Scanner([Link]);
int row = [Link]();
int column = [Link]();
[Link]("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = [Link]();
[Link](" ");
}
}
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
if(array[i][j] == 0)
{
zero++;
}
else
{
count++;
}
}
}
if(zero>count)
{
[Link]("the matrix is sparse matrix");
}
else
{
[Link]("the matrix is not a sparse matrix");
}
}
}
for(int i = 0; i < m; i++) Another logic for
{ sparse matrix
for(int j = 0; j < n; j++)
{
if(a[i][j] == 0) //Check if element is 0 or not
count++;
}
}
if(count>(size/2))
[Link]("It is a sparse matrix");
else
[Link]("It is not a sparse matrix");
}
}

You might also like