Question 3. (Arrays2D.java, 20 marks) Write a Java program that will show off 2D
ID: 3778213 • Letter: Q
Question
Question 3. (Arrays2D.java, 20 marks) Write a Java program that will show off 2D arrays. Your program should use 2D arrays and nested for loops to reproduce the following output. Note: the array is always 4 x 5 and is filled with random numbers between 1 and 10. Only the count occurrences number is collected from the user. You may do all of this work in a single main() method.
Creating array (4x5, random numbers) 1 9 5 7 6 2 5 2 1 3 4 8 5 1 2 7 9 1 2 6
Sum of array is: 86
Avg of array is: 4.3
Max of rows is: 0:9 1:5 2:8 3:9
Min of columns is: 0 1 2 3 4 1 5 1 1 2
Count occurrences of:1 4
(Extra 5 marks): Modify the program to allow for more user input as follows. Invalid indices should be handled using loops.
Enter two +ve integers (rows then cols): 3 4 //(loop for bad input) Creating array (3x4, random numbers)
1 8 3 7 2 1 6 2 5 2 5 1
Sum of array is: 43
Avg of array is: 3.583333333
Find the max of which row: 4
Find the max of which row: 3 3:5
Find the max of which col: -1
Find the max of which col: 2 2 6
Count occurrences of:1 4
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class Arrays2d {
static void array2d(int a [][] ,int x,int y)//modofied
{
//variable declaration...
int i,j,sum=0,max,min;
float average,n=x*y;
Scanner sc = new Scanner(System.in);
Random r =new Random();
System.out.print(" Creating array ("+x+"x"+y+", random numbers)");
for(i=0;i<x;i++)//generating random array with range 1 to 9
{
for(j=0;j<y;j++)
{
a[i][j]=r.nextInt(9)+1;
System.out.print(a[i][j]+" ");
}
}
System.out.println();
for(i=0;i<x;i++)//calculating sum
{
for(j=0;j<y;j++)
{
sum=sum+a[i][j];
}
}
average =sum/n;//calculating average//
System.out.println("Sum of array is:-"+sum+" Avg of array is:-"+average+" ");
System.out.print("Find the max of which row:");
min = sc.nextInt();
if(min<x && min>=0 )
{//finding max element of given row and printing
max = a[min][0];
for(j=0;j<y;j++)
{
if(max<a[min][j])max=a[min][j];
}
System.out.print(min+":"+max+" ");
}
System.out.println();
System.out.print("Find the max of which row:");
min = sc.nextInt();
if(min<x && min>=0 )
{//finding max element of given row and printing
max = a[min][0];
for(j=0;j<y;j++)
{
if(max<a[min][j])max=a[min][j];
}
System.out.print(min+":"+max+" ");
}
System.out.println();
System.out.print("Find the max of which col:");
min = sc.nextInt();
if(min<y && min>=0 )
{//finding max element of given col and printing
max = a[0][min];
for(j=0;j<x;j++)
{
if(max<a[j][min])max=a[j][min];
}
System.out.print(min+":"+max+" ");
}
System.out.println();
System.out.print("Find the max of which col:");
min = sc.nextInt();
if(min<y && min>=0 )
{//finding max element of given col and printing
max = a[0][min];
for(j=0;j<x;j++)
{
if(max<a[j][min])max=a[j][min];
}
System.out.print(min+":"+max+" ");
}
System.out.println();
System.out.print("Count occurrences of:");
min=sc.nextInt();
sum=0;
for(i=0;i<x;i++)//counting occurrences of given number
{
for(j=0;j<y;j++)
{
if(min==a[i][j])sum=sum+1;
}
}
System.out.println(sum);
}
static void array2m(int a [][] ,int x,int y)
{
//variable declaration...
int i,j,sum=0,max,min;
float average,n=x*y;
Scanner sc = new Scanner(System.in);
Random r =new Random();
System.out.print(" Creating array ("+x+"x"+y+", random numbers)");
for(i=0;i<x;i++)//generating random array with range 1 to 9
{
for(j=0;j<y;j++)
{
a[i][j]=r.nextInt(9)+1;
System.out.print(a[i][j]+" ");
}
}
System.out.print(" ");
for(i=0;i<x;i++)//calculating sum
{
for(j=0;j<y;j++)
{
sum=sum+a[i][j];
}
}
average =sum/n;//calculating average//
System.out.println("Sum of array is:-"+sum+" Avg of array is:-"+average+" ");
System.out.print("Max of rows is:");
for(i=0;i<x;i++)//finding max elements row wise and printing
{
max = a[i][0];
for(j=0;j<y;j++)
{
if(max<a[i][j])max=a[i][j];
}
System.out.print(i+":"+max+" ");
}
System.out.println();
System.out.print("Min of cols is:");
for(i=0;i<y;i++)//finding min elements col wise and printing
{
min = a[0][i];
for(j=0;j<x;j++)
{
if(min>a[j][i])min=a[i][j];
}
System.out.print(i+":"+min+" ");
}
System.out.println();
System.out.print("Count occurrences of:");
min=sc.nextInt();
sum=0;
for(i=0;i<x;i++)//counting occurrences of given number
{
for(j=0;j<y;j++)
{
if(min==a[i][j])sum=sum+1;
}
}
System.out.println(sum);
}
public static void main(String argv[])
{
int m,n;
int a[][] = new int[4][5];
array2m(a,4,5);
Scanner sc = new Scanner(System.in);
//modified
System.out.print("Enter two +ve integers (rows then cols): ");
m = sc.nextInt();
n = sc.nextInt();
array2d(a,m,n);
}
}
ouput:-
run:
Creating array (4x5, random numbers)6 1 8 1 2 7 1 8 8 2 7 3 1 9 7 9 9 2 9 9
Sum of array is:-109
Avg of array is:-5.45
Max of rows is:0:8 1:8 2:9 3:9
Min of cols is:0:6 1:1 2:1 3:1 4:2
Count occurrences of:1
4
Enter two +ve integers (rows then cols): 3 4
Creating array (3x4, random numbers)6 2 8 4 2 1 8 2 2 9 2 1
Sum of array is:-47
Avg of array is:-3.9166667
Find the max of which row:2
2:9
Find the max of which row:4
Find the max of which col:3
3:4
Find the max of which col:-1
Count occurrences of:2
5
BUILD SUCCESSFUL (total time: 23 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.