Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is written in Java. Write a program that randomly fills in 0-9 into

ID: 3672931 • Letter: T

Question

This program is written in Java.

Write a program that randomly fills in 0-9 into an n times m matrix, prints the matrix, and finds the row's and columns with the largest value. You can use different approaches to solve this problem. For example, you can use two-dimensional arrays to store randomly generated data and Array List for saving the indices. Expected results: Enter the number of rows: 4 Enter the number of columns: 6 The array content is: 7 9 6 0 4 3 0 4 8 4 8 2 2 1 1 4 5 2 7 3 5 9 6 0 The index of the largest row: 3 The index of the largest column: 4

Explanation / Answer

program:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author MDR
*/
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class MatrixCal {
public static void main(String[] args)throws IOException
{
int r,c,maxc=0,maxr=0;
  
Random ran=new Random();
Scanner scan=new Scanner(System.in);
System.out.print("Enter number of rows:");
r=scan.nextInt();
System.out.print(" Enter number of cloumns:");
c=scan.nextInt();
int[][] RandomMatrix=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
RandomMatrix[i][j]=ran.nextInt(9);
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print( RandomMatrix[i][j]+" ");
System.out.println();
  
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(RandomMatrix[i][j]>=RandomMatrix[maxr][maxc])
{
maxr=i;
maxc=j;
}
}
}
System.out.println("The index of largest row:"+maxr);
System.out.println("The index of largest column:"+maxc);
}
}

output:

run:
Enter number of rows:5

Enter number of cloumns:5
6 2 7 4 8
6 1 8 1 3
2 4 5 6 8
6 8 3 0 7
5 6 3 2 3
The index of largest row:3
The index of largest column:1
BUILD SUCCESSFUL (total time: 3 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote