calculate sum column wise in a matrix:
Write a java program to calculate sum column wise in a matrix:
import java.util.Random;
import java.util.Scanner;
public class ColumnWiseLeast {
public static void main(String[] args) {
int mat[][]=readmatrix();
dispMat(mat);
System.out.println("Sum of Diagonal element is : ");
int arr[]=leastColWise(mat);
dispSingleArray(arr);
}
static int[] leastColWise(int mat[][]) {
int least[]=new int[mat[0].length];
for(int i=0;i<mat[0].length;i++) {
least[i]=mat[0][i];
for(int j=0;j<mat.length;j++) {
if(least[i]>mat[j][i]) {
least[i]=mat[j][i];
}
}
}return least;
}
static int[][] readmatrix(){
Random rx=new Random();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Row size and column size :");
int row=sc.nextInt();
int col=sc.nextInt();
int mat[][]=new int[row][col];
System.out.println("Enter Elements in Table : ");
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
mat[i][j]=rx.nextInt(25);
}
sc.close();
}return mat;
}static void dispMat(int mat[][]) {
for(int i=0;i<mat.length;i++) {
for(int j=0;j<mat[i].length;j++) {
System.out.print(mat[i][j]+" ");
}System.out.println();
}
}
static void dispSingleArray(int arr[]) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
Comments
Post a Comment