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

Write a Java program that will show off 2D arrays. Your program should use 2D ar

ID: 3571173 • Letter: W

Question

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. Sum of array is: 86 Avg of array is: 4.3 Max of rows is: 0: 9 1: 5 2: 8 3: 9 Count: occurrences of: 1 4 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) 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

// Arrays2D.java
import java.util.Scanner;
import java.util.*;

public class Arrays2D
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Random rn = new Random();

        System.out.print("Enter two +ve integers (rows than cols): ");
        int row = in.nextInt();
        int col = in.nextInt();

        int min, max;
        int sum = 0;
        double avg = 0;
        int[][] matrix = new int[row][col];

        System.out.println("Creating array (" + row + "X" + col + ", random numbers)");
        for(int r = 0; r< matrix.length; r++)
        {
            for(int c = 0 ;c< matrix[r].length; c++)
            {
                matrix[r][c] = rn.nextInt(10);
                System.out.print(matrix[r][c] + " ");
                sum = sum + matrix[r][c];
            }
            System.out.println();
        }

        avg = sum/(row*col);
        System.out.println("Sum of array is: " + sum);
        System.out.println("Avg of array is: " + avg);


        System.out.println("Max of rows is:");
        for(int r = 0; r< row; r++)
        {
            max = matrix[r][0];
            for(int c = 0 ;c< col; c++)
            {
                if( matrix[r][c] > max)
                    max = matrix[r][c];
            }
            System.out.println(r + " : " + max);
        }

        System.out.println("Min of columns is:");
        for (int i = 0; i < col ; i ++ )
        {
            System.out.print(i + " ");  
        }
        System.out.println();

        for(int c = 0; c< col; c++)
        {
            min = matrix[0][c];
            for(int r = 0 ;r< row; r++)
            {
                if(matrix[r][c] < min)
                    min = matrix[r][c];
            }

            System.out.print(min + " ");
        }

        System.out.println();

        while(true)
        {
            System.out.print("Find maximum of which row: ");
            int r = in.nextInt();

            if(r < row)
            {
                max = matrix[r][0];
                for(int c = 0 ;c< col; c++)
                {
                    if( matrix[r][c] > max)
                        max = matrix[r][c];
                }
                System.out.println(r + " : " + max);
                break;
            }
        }

        while(true)
        {
            System.out.print("Find maximum of which col: ");
            int c = in.nextInt();

            if(c < col)
            {
                max = matrix[0][c];
                for(int r = 0 ;r< row; r++)
                {
                    if(matrix[r][c] > max)
                        max = matrix[r][c];
                }
                System.out.println(c + " : " + max);
                break;
            }
        }

         System.out.print("Count occurence of: ");
         int search = in.nextInt();
         int count = 0;
         for(int r = 0; r< row; r++)
         {
          
            for(int c = 0 ;c< col; c++)
            {
                if( matrix[r][c] == search)
                    count++;
            }
         }
         System.out.println(count);

    }
}

/*
output:

Enter two +ve integers (rows than cols): 4 5
Creating array (4X5, random numbers)
8   3   6   6   7
6   6   5   0   1
1   5   7   9   1
0   4   8   2   0
Sum of array is: 85
Avg of array is: 4.0
Max of rows is:
0 : 8
1 : 6
2 : 9
3 : 8
Min of columns is:
0   1   2   3   4
0   3   5   0   0
Find maximum of which row: 10
Find maximum of which row: 4
Find maximum of which row: 3
3 : 8
Find maximum of which col: 6
Find maximum of which col: 3
3 : 9
Count occurence of: 5
2


*/

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