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

Write a program that accepts an n x n matrix. The program should also: Total and

ID: 3729070 • Letter: W

Question

Write a program that accepts an n x n matrix. The program should also:

Total and display the sum of each row and each column

Find the product of each row and column

Find the highest and lowest value within the array

Constraints

The number of rows and columns should be the same

Use nested for loops and methods

Requirements

Build your matrix based upon user input (e.g., ask how many rows and columns - they must be the same)

Use a method to display the matrix (use tabs between each element)

Create methods to display the following:

o Method 1: The sum of each row
o Method 2: The sum of each column
o Method 3: The product of each row
o Method 4: The product of each column
o Method 5: The highest value in the matrix
o Method 6: The lowest value in the matrix

Loop the program to run it until the user wishes to exit

In the example below, the numbers ARE NOT hard coded. Your program should ask the user for input

This program should utilize code learned from Week 1 through Week 8

Hints

Make sure you use Java coding conventions

I'm stuck on the part where the user must input the value of rows and columns. It should look as follows:

Enter number of rows & columns (they will be the same): 2


Enter a value for Row 1, Column 1: 3
Enter a value for Row 1, Column 2: 8
Enter a value for Row 2, Column 1: 7
Enter a value for Row 2, Column 2: 9

I understand the first part of this, getting the number of rows and columns. But then getting a value for each row and column from the user is where I'm getting stuck.

Explanation / Answer

import java.util.*;

class TestMatrix
{
public static void main (String[] args)
{
  int[][] mat = new int[10][10];
  Scanner input = new Scanner(System.in);
  System.out.println("Enter number of rows & columns (they will be the same): ");
  int n = input.nextInt();
  
  //input elements of matrix
  for(int i=0;i<n;i++)
  {
   for(int j=0;j<n;j++)
   {
   System.out.println("Enter a value for Row "+(i+1)+", Column "+(j+1)+": ");
   mat[i][j] = input.nextInt();
   }
  }
  
  //calling all methods
  sumRow(mat,n);
  productRow(mat,n);
  sumCol(mat,n);
  productCol(mat,n);
  highestNum(mat,n);
  lowestNum(mat,n);
  
  
}
public static void sumRow(int[][] mat,int n)
{
  int sum;
  System.out.println("Sum of each row : ");
  for(int i=0;i<n;i++)
  {
   sum = 0;
   for(int j=0;j<n;j++)
   {
   sum = sum + mat[i][j];
   
   }
   System.out.println(sum);
  }
}
public static void sumCol(int[][] mat,int n)
{
  int sum;
  System.out.println("Sum of each column : ");
  for(int i=0;i<n;i++)
  {
   sum = 0;
   for(int j=0;j<n;j++)
   {
   sum = sum + mat[j][i];
   
   }
   System.out.println(sum);
  }
}
public static void productRow(int[][] mat,int n)
{
  int product;
  System.out.println("product of each row : ");
  for(int i=0;i<n;i++)
  {
   product = 1;
   for(int j=0;j<n;j++)
   {
   product = product * mat[i][j];
   
   }
   System.out.println(product);
  }
}
public static void productCol(int[][] mat,int n)
{
  int product;
  System.out.println("product of each column : ");
  for(int i=0;i<n;i++)
  {
   product = 1;
   for(int j=0;j<n;j++)
   {
   product = product * mat[j][i];
   
   }
   System.out.println(product);
  }
}
public static void highestNum(int[][] mat,int n)
{
  int highest = mat[0][0];
  for(int i=0;i<n;i++)
  {
   for(int j=0;j<n;j++)
   {
    if(mat[i][j] > highest)
    highest = mat[i][j];
   }
  }
  System.out.println("Highest number in matrix : "+highest);
}
public static void lowestNum(int[][] mat,int n)
{
  int lowest = mat[0][0];
  for(int i=0;i<n;i++)
  {
   for(int j=0;j<n;j++)
   {
    if(mat[i][j] < lowest)
    lowest= mat[i][j];
    
   }
  }
  System.out.println("lowest number in matrix : "+lowest);
}
}

Output:

Enter number of rows & columns (they will be the same):3
Enter a value for Row 1, Column 1:1
Enter a value for Row 1, Column 2:2
Enter a value for Row 1, Column 3:3
Enter a value for Row 2, Column 1:4
Enter a value for Row 2, Column 2:5
Enter a value for Row 2, Column 3:6
Enter a value for Row 3, Column 1:7
Enter a value for Row 3, Column 2:8
Enter a value for Row 3, Column 3:9
Sum of each row :
6
15
24
product of each row :
6
120
504
Sum of each column :
12
15
18
product of each column :
28
80
162
Highest number in matrix : 9
lowest number in matrix : 1

Do ask if any query. Please upvote if the answer is helpful.

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