Java Program: (name this Lab13_Problem2) You can reuse a lot of code from Proble
ID: 3769835 • Letter: J
Question
Java Program: (name this Lab13_Problem2)
You can reuse a lot of code from Problem 1 for this problem. Again in main()construct a two-dimensional array with 20 rows and 5 columns named iNums.
Write and call the method fvPop() to populate it with the random integers between 1 and 1000. Pass the array.
Write and call the method fvDisplay() to display it. Pass the array.
Write a method fiSmallest() to find the smallest value in the array. Pass the array; return the smallest value found. Display it in main().
Write a method fiLargest() to find the largest value in the array. Display it in main().
Sample output (allocate 8 horizontal spaces for each value):
Explanation / Answer
code:
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
int [][] iNums = new int[20][5];
fvPop(iNums);
fvDisplay(iNums);
int small = fiSmallest(iNums);
int large = fiLargest(iNums);
System.out.printf("Smallest = %-8d Largest = %-8d ",small,large);
}
public static void fvPop(int [][] iNums)
{
int ele;
for(int i=0;i<iNums.length;i++)
{
for(int j=0;j<iNums[i].length;j++)
{
iNums[i][j] = (int)(Math.random()*1000+1);
}
}
}
public static void fvDisplay(int [][] iNums)
{
for(int i=0;i<iNums.length;i++)
{
for(int j=0;j<iNums[i].length;j++)
{
System.out.printf("%-8d",iNums[i][j]);
}
System.out.println();
}
}
public static int fiSmallest(int [][] iNums)
{
int small = 1001;
for(int i=0;i<iNums.length;i++)
{
for(int j=0;j<iNums[i].length;j++)
{
if(iNums[i][j] < small)
small = iNums[i][j];
}
}
return small;
}
public static int fiLargest(int [][] iNums)
{
int large = 0;
for(int i=0;i<iNums.length;i++)
{
for(int j=0;j<iNums[i].length;j++)
{
if(iNums[i][j] > large)
large = iNums[i][j];
}
}
return large;
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.