Assume that mat in a N by N 2D array that stores N2 integers

Assume that mat in a N by N 2-D array that stores N^2 integers, write Java code to sum only the integers in the last column of mat. write Java code to sum only the integers in first row of mat. write Java code to sum the integers in the major diagonal of mat. Given the following java program, write down the output in the correct order.

Solution

import java.util.Scanner;
class sub
{
public static void main(String args[])
{
int n,i,j,csum,rsum,sum;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int mat[][]=new int[n][n]; //matrix creation
System.out.println(\"enter the array elemets\");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mat[i][j]=sc.nextInt();
}
}
//a.sum of the last column integers
System.out.println(\"sum of the last column itegers\") ;
csum=0;
for(i=n;i>n;i++)
{
  
for(j=0;j<n;j++)
{
csum=csum+mat[i][j];
}
}
System.out.println(\"sum of the last column is:\"+csum);
//b.sum of the first row inteegers
System.out.println(\"sum of the first row of integers i mat\");
rsum=0;
for(i=0;i<0;i++)
{
  
for (j=0;j<n;j++)
{
rsum=rsum+mat[i][j] ;
}
}
System.out.println(\"sum of the first row is:\"+rsum);
//c.sum of diagonal integers
System.out.println(\"sum of the diagonal matrix\");
sum=0;
for(i=0,j=0;i<n&&j<n;i++,j++)
{
sum=sum+mat[i][j];
}
System.out.println(\"sum of the diagonal elements :\"+sum);
}
}

 Assume that mat in a N by N 2-D array that stores N^2 integers, write Java code to sum only the integers in the last column of mat. write Java code to sum only

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site