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

Multi-Dimentional Array

Assignment

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

1. Total and display the sum of each row and each column

2. Find the product of each row and column

3. 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

Expected Output

The following pages show a sample run with two iterations. User input is in Bold.

Welcome! This program that accepts an n x n matrix, totals the rows and columns & finds the product of each row and column.

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

The Matrix****************************************

You Entered the Following Matrix:

3           8

7            9

Row Totals****************************************

The sum of row 1 is: 11

The sum of row 2 is: 16

Column Totals*************************************

The sum of column 1 is: 10

The sum of column 2 is: 17

Row Products*************************************

The product of row 1 is: 24

The product of row 2 is: 63

Column Products*************************************

The product of column 1 is: 21

The product of column 2 is: 72

The greatest value in this matrix is: 9

The lowest value in this matrix is: 3

Enter another matrix? (Y or N) y (if yest it loops)

Enter another matrix? (Y or N) n Thank you for using the program! Goodbye!

Expert Answer

 

import java.util.*;
public class Matrix
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i;
String c,d=”Y”;;
do{
System.out.print(“Enter number of rows & columns (they will be the same)”);
i=sc.nextInt();
int arr[][]=new int[i][i];
for(int j=0;j<i;j++)
{
for(int k=0;k<i;k++)
{
System.out.print(“Enter value for row”+(j+1)+ “,column “+(k+1)+ “: “);
arr[j][k]=sc.nextInt();
}}
System.out.println(“The Matrix ******** ****************”);
System.out.println(“You entered the following matrix”);
for(int j=0;j<i;j++)
{
for(int k=0;k<i;k++)
{
System.out.print(arr[j][k]+ ” “);
}
System.out.println();
}sumofrows(arr,i);
sumofcolumns(arr,i);
productofrows(arr,i);
productofcolumns(arr,i);
max(arr,i);
min(arr,i);
System.out.print(“Enter another Matrix (Y or N)”);
c=sc.next();
}while(c.equals(d));
System.out.print(“n Thank you for using the program! Goodbye!”);
}

public static void sumofrows(int arr[][], int i)
{
int sum=0;
System.out.println(“Row Totals *****************”);
for(int j=0;j<i;j++ )
{
for(int k=0;k<i;k++){
sum = sum + arr[j][k] ;
}
System.out.println(“sum of row “+ (j+1) + “is ” + sum);
sum=0;
}}

public static void sumofcolumns(int arr[][], int i)
{
int sum=0;
System.out.println(“Column Totals *****************”);
for(int k=0;k<i;k++ )
{
for(int j=0;j<i;j++){
sum = sum + arr[j][k] ;
}
System.out.println(“sum of Column “+ (k+1) + “is ” + sum);
sum=0;
}}

public static void productofrows(int arr[][], int i)
{
int product=1;
System.out.println(“Row products *****************”);
for(int j=0;j<i;j++ )
{
for(int k=0;k<i;k++){
product = product * arr[j][k] ;
}
System.out.println(“Product of row “+ (j+1) + “is ” + product);
product=1;
}}

public static void productofcolumns(int arr[][], int i)
{
int product=1;
System.out.println(“Column products *****************”);
for(int k=0;k<i;k++ )
{
for(int j=0;j<i;j++){
product = product * arr[j][k] ;
}
System.out.println(“sum of Column “+ (k+1) + “is ” + product);
product=1;
}}

public static void max(int arr[][],int i)
{
int max=0;
for(int j=0;j<i;j++)
{
for(int k=0;k<i;k++)
{
if(arr[j][k]>max)
{max=arr[j][k];
}}}
System.out.println(“The greatest value in the matrix is : “+ max);
}

public static void min(int arr[][],int i)
{
int min=arr[0][0];
for(int j=0;j<i;j++)
{
for(int k=0;k<i;k++)
{
if(arr[j][k]<min)
{min=arr[j][k];
}}}
System.out.println(“The Lowest value in the matrix is : “+ min);
}

}

Still stressed from student homework?
Get quality assistance from academic writers!