0% found this document useful (0 votes)
9 views2 pages

Program 8

The document is a Java program that performs various operations on a 2D array, including calculating the sum of all elements, the diagonal sum, and the boundary sum. It also computes the determinant of a 3x3 matrix. The program demonstrates basic matrix operations and includes comments outlining its functionality.

Uploaded by

thalamovie234
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)
9 views2 pages

Program 8

The document is a Java program that performs various operations on a 2D array, including calculating the sum of all elements, the diagonal sum, and the boundary sum. It also computes the determinant of a 3x3 matrix. The program demonstrates basic matrix operations and includes comments outlining its functionality.

Uploaded by

thalamovie234
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

public class program8 {

public static void main(String[] args) {


// [Link] 2d array and finding sum of all numbers
// [Link] sum in 2d array
// [Link] sum in 2d array
// [Link] of 3x3 matrix
// [Link] and sub 2 3*3 matrices
// [Link] 2 matrices
// [Link] a class Animal and then create 2 objects for that
class

int[][] array = new int[][]{{1,2,3},{4,5,6},{7,8,9}};


int rows=[Link],cols = array[0].length;
int array_sum = 0;
for(int i=0;i<[Link];i++) //number of rows
{
for(int j=0;j<array[0].length;j++) // number of cols
{
// [Link](array[i][j]);
array_sum += array[i][j];
}
}
[Link]("The array sum is "+array_sum);

// [Link] sum

int diagonal_sum = 0;

for(int i=0,j=0;i<rows&&j<cols;i++,j++)
{
diagonal_sum += array[i][j];
}
[Link]("The diagonal sum is"+diagonal_sum);

// 3. boundary sum
int boundarySum = 0;

for(int i=0,j=0;i<rows;i++)
boundarySum+= array[i][j];

for(int i=rows-1,j=1;j<cols;j++)
boundarySum+= array[i][j];

for(int i=rows-2,j=cols-1;i>=0;i--)
boundarySum+= array[i][j];

for(int i=0,j=cols-2;j>0;j--)
boundarySum+= array[i][j];

[Link]("Boundary sum is"+boundarySum);

// 4. determinant of 3*3 matrix


// 1,2,3
// 4,5,6
// 7,8,9
// first = 1(5x9-6x8) second = 2(4x9-6x7) third = 3(4x8-7x5)
int first = array[0][0] * (array[1][1]*array[2][2] -
array[1][2]*array[2][1]);
int second = array[0][1] * (array[1][0]*array[2][2] -
array[1][2]*array[2][0]);
int third = array[0][2] * (array[1][0]*array[2][1] -
array[2][0]*array[1][1]);
int determinant= first - second + third;

[Link]("the determinant sum is"+determinant);

}
}

You might also like