Java Program: (name this Lab13_Problem1) Write a main() method that constructs a
ID: 3769834 • Letter: J
Question
Java Program: (name this Lab13_Problem1)
Write a main() method that constructs 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 in the range 1 to 500, inclusive. Pass the array.
Write and call the method fvDisplay() to display the array. Pass the array. Allocate 8 horizontal spaces for each value's output.
When you get that working, go back to main and prompt the user for a value to search for in the array. Store that to an input-capture variable.
Pass the value the user provides to another method, fvSearch(); also pass the array. In the method, search the array for the value supplied in the parameter. If found, display the row and column (in "Base 1") where the first instance of the value was found, or not found if that's the case.
Sample output (assuming user types 19):
Value 19 found in Row 1, Column 4
or
Value 19 not found
Explanation / Answer
import java.util.*;
class arr2d_fill_disp_search
{
public static int[][] fvPop(int a[][], int r, int c)
{
int Low = 1;
int High =500;
Random ra = new Random();
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=ra.nextInt(High-Low)+Low;
}
}
return a;
}
public static void fvDisplay(int a[][], int r, int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void fvSearch(int a[][], int r, int c, int s)
{
int i;
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(a[i][j]==s)
{
System.out.print(" Value "+s+" found in Row " + i + " Column " + j);
break;
}
}
}
if(i==r)
{
System.out.println("Value "+ s+" not found");
}
}
public static void main(String args[])
{
int iNums[][] = new int[20][5];
int b[][]=new int[20][5];
b=fvPop(iNums, 20, 5);
fvDisplay(b,20,5);
Scanner scan = new Scanner(System.in);
System.out.println("Enter number to search");
int s=scan.nextInt();
fvSearch(b,20,5,s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.