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

Write a program which does the following: Give the user a menu choice Option 1:

ID: 3835885 • Letter: W

Question

Write a program which does the following: Give the user a menu choice Option 1: Randomly Generate a 2D array. This option will ask the user for the number of rows and columns in the array, as well as the range of numbers to generate, (i.e. The user can choose the min and max of the random number formula.) The random numbers should be integers. Option 2: Populate an array using File I/O. This option will ask the user to enter the name of a file OR use JFileChooser to read data from an input file. The file will have the following format: The first two numbers will be the dimensions of the array (rows and columns). The rest of the numbers will be the data for the array. Once the array has been created using one of the above two options. Display the following results: Display the array in table format. (Print the 2D array) Calculate and display the sum and average of the entire array. Calculate and display the sum and average of each row. Calculate and display the sum and average of each column. Calculate and display the sum and average of the major and minor diagonals *see below. Display the row and col with the highest average. Display the row and col with the lowest average. Be sure to use appropriate methods or the program will be worth no credit.

Explanation / Answer

PROGRAM CODE:

package numerical;

import java.util.Scanner;

public class TwoDimensionalArray {

   public static int[][] readFromFile(String filename)
   {
       Scanner filereader = new Scanner(filename);
       int rows = filereader.nextInt();
       int columns = filereader.nextInt();
       int array[][] = new int[rows][columns];
       for(int i=0; i<rows; i++)
       {
           for(int j=0; j<columns; j++)
               array[i][j] = filereader.nextInt();
       }
       filereader.close();
       return array;
   }
  
   public static int[][] readFromConsole()
   {
       Scanner keyboard = new Scanner(System.in);
       int rows, columns;
       int array[][];
       System.out.print("Enter the number of rows: ");
       rows = keyboard.nextInt();
       System.out.print("Enter the number of columns: ");
       columns = keyboard.nextInt();
       array = new int[rows][columns];
       for(int i=0; i<rows; i++)
       {
           for(int j=0; j<columns; j++)
           {
               System.out.print("Enter number for row " + (i+1) + " column " + (j+1) + ": " );
               array[i][j] = keyboard.nextInt();
              
           }
       }
       keyboard.close();
       return array;
   }
  
   public static void printArray(int array[][])
   {
       int rows = array.length;
       int columns = array[0].length;
  
       for(int i=0; i<rows; i++)
       {
           for(int j=0; j<columns; j++)
               System.out.print(array[i][j] + " ");
           System.out.println();
       }
   }
  
   public static int sum(int array1D[])
   {
       int sum = 0;
       for(int x=0; x<array1D.length; x++)
           sum+= array1D[x];
       return sum;
   }
  
   public static double average(int array1D[])
   {
       double sum = 0;
       for(int x=0; x<array1D.length; x++)
           sum+= array1D[x];
       return sum/array1D.length;
   }
  
   public static int sum(int array[][])
   {
       int rows = array.length;
       int fullSum = 0;
       for(int i=0; i<rows; i++)
       {
           fullSum += sum(array[i]);
       }
       return fullSum;
   }
  
   public static double average(int array[][])
   {
       int rows = array.length;
       double fullSum = 0;
       for(int i=0; i<rows; i++)
       {
           fullSum += average(array[i]);
       }
       return fullSum/rows;
   }
  
   public static void main(String[] args) {
       int choice = 0;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("MENU 1. Read from file 2. Read from console Enter your choice: ");
       choice = keyboard.nextInt();
       int array2D[][];
       int sums[];
       double averages[];
       if(choice == 1)
       {
           System.out.println("Enter the file name: ");
           String filename = keyboard.nextLine();
           array2D = readFromFile(filename);
       }
       else array2D = readFromConsole();
      
       //Printing the array
       System.out.println("2D array: ");
       printArray(array2D);
       //total sum, average
       System.out.println(" Sum of 2D Array: " + sum(array2D));
       System.out.println(" Average of 2D Array: " + average(array2D));
      
       //sum for each row
       System.out.println(" Sum of each row: ");
      
       sums = new int[array2D.length];
       averages = new double[array2D.length];

       for(int i=0; i<array2D.length; i++)
       {
           sums[i] = sum(array2D[i]);
           System.out.println("Sum of Row " + (i+1) + " : " + sums[i]);
       }
      
       System.out.println(" Average of each row: ");
       for(int i=0; i<array2D.length; i++)
       {
           averages[i] = average(array2D[i]);
           System.out.println("Average of Row " + (i+1) + " : " + averages[i]);
       }
       //highest and lowest for rows
       int highestSum = sums[0];
       int lowestSum = sums[0];
       for(int i=0; i<sums.length; i++)
       {
           if(highestSum < sums[i])
               highestSum = sums[i];
           if(lowestSum > sums[i])
               lowestSum = sums[i];
       }
      
       System.out.println("Highest sum for rows: " + highestSum);
       System.out.println("Lowest sum for rows: " + lowestSum);
      
       double highestAvg = averages[0];
       double lowestAvg = averages[0];
       for(int i=0; i<averages.length; i++)
       {
           if(highestAvg < averages[i])
               highestAvg = averages[i];
           if(lowestAvg > averages[i])
               lowestAvg = averages[i];
       }
      
       System.out.println("Highest average for rows: " + highestAvg);
       System.out.println("Lowest average for rows: " + lowestAvg);
      
       //sum, average for each column
       sums = new int[array2D[0].length];
       averages = new double[array2D[0].length];
      
       System.out.println(" Sum of each column: ");
       for(int i=0; i<array2D[0].length; i++)
       {
           int array[] = new int[array2D.length];
           for(int j=0; j<array2D.length; j++)
               array[j] = array2D[j][i];
           sums[i] = sum(array);
           System.out.println("Sum of Column " + (i+1) + " : " + sums[i]);
       }
      
       System.out.println(" Average of each row: ");
       for(int i=0; i<array2D[0].length; i++)
       {
           int array[] = new int[array2D.length];
           for(int j=0; j<array2D.length; j++)
               array[j] = array2D[j][i];
           averages[i] = average(array);
           System.out.println("Average of Column " + (i+1) + " : " + averages[i]);
       }
      
       highestSum = sums[0];
       lowestSum = sums[0];
       for(int i=0; i<sums.length; i++)
       {
           if(highestSum < sums[i])
               highestSum = sums[i];
           if(lowestSum > sums[i])
               lowestSum = sums[i];
       }
      
       System.out.println("Highest sum for columns: " + highestSum);
       System.out.println("Lowest sum for columns: " + lowestSum);
      
       highestAvg = averages[0];
       lowestAvg = averages[0];
       for(int i=0; i<averages.length; i++)
       {
           if(highestAvg < averages[i])
               highestAvg = averages[i];
           if(lowestAvg > averages[i])
               lowestAvg = averages[i];
       }
      
       System.out.println("Highest average for columns: " + highestAvg);
       System.out.println("Lowest average for columns: " + lowestAvg);
      
       int counter = 0;
       int array[] = new int[array2D[0].length];
       for(int i=0; i<array2D.length; i++)
       {
           for(int j=0; j<array2D[0].length; j++)
           {
               if(i==j)
                   array[counter++] = array2D[i][j];
           }
       }
       System.out.println(" Sum of major diagonal " + " : " + sum(array));
       System.out.println("Average of major diagonal " + " : " + average(array));
      
       counter = 0;
       array = new int[array2D.length];
      
       for(int i=0; i<array2D.length; i++)
       {
           for(int j=array2D[0].length-1; j>=0; j--)
           {
               if(i==(array2D.length - 1 - j))
                   array[counter++] = array2D[i][j];
           }
       }
      
       System.out.println(" Sum of major diagonal " + " : " + sum(array));
       System.out.println("Average of major diagonal " + " : " + average(array));
      
   }

}

OUTPUT:

MENU
1. Read from file
2. Read from console
Enter your choice:
2
Enter the number of rows: 2
Enter the number of columns: 3
Enter number for row 1 column 1: 1
Enter number for row 1 column 2: 1
Enter number for row 1 column 3: 1
Enter number for row 2 column 1: 2
Enter number for row 2 column 2: 2
Enter number for row 2 column 3: 2
2D array:

1 1 1
2 2 2

Sum of 2D Array: 9

Average of 2D Array: 1.5

Sum of each row:

Sum of Row 1 : 3
Sum of Row 2 : 6

Average of each row:

Average of Row 1 : 1.0
Average of Row 2 : 2.0
Highest sum for rows: 6
Lowest sum for rows: 3
Highest average for rows: 2.0
Lowest average for rows: 1.0

Sum of each column:

Sum of Column 1 : 3
Sum of Column 2 : 3
Sum of Column 3 : 3

Average of each row:

Average of Column 1 : 1.5
Average of Column 2 : 1.5
Average of Column 3 : 1.5
Highest sum for columns: 3
Lowest sum for columns: 3
Highest average for columns: 1.5
Lowest average for columns: 1.5

Sum of major diagonal : 3
Average of major diagonal : 1.0

Sum of major diagonal : 3
Average of major diagonal : 1.5

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