2-D array Random input and Find Biggest Element

Write a java program to find biggest element from matrix using random input : 

import java.util.Random;
import java.util.Scanner;

public class BiggestElementMatrix {
static Random rx=new Random();
public static void main(String[] args) {
System.out.println("Enter a matrix : ");
int [][]mat=readmatrix();
int big=biggestMatrix(mat);
System.out.println("Matrix is ");
dispMat(mat);
System.out.println("Biggest element is : "+big);
}
static int[][] readmatrix(){
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(50);
}
}return mat;
}
static int biggestMatrix(int[][] mat) {
int temp=0;
for(int i=0;i<mat.length;i++) {
for(int j=0;j<mat[i].length;j++) {
if(temp<mat[i][j]) {
temp=mat[i][j];
}
}
}return temp;
}
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();
}
  }
}

Comments

Popular posts from this blog

program to calculate product of each column in matrix