Write a java program that will do the following Create a 5 x
Write a java program that will do the following:
Create a 5 x 5 array of integers; fill it with random numbers in the range of 1 – 1000.
Print the array in a tabular format
Calculate/Print out the total of all values in the array.
Calculate/Print out the average of all values in the array.
Calculate/Print out the total of each row in the array.
Calculate/Print out the total of each column in the array.
Print out the highest value in each row in the array.
Print out the smallest value in each row in the array.
Solution
// RandomMatrix.java
import java.util.Scanner;
public class RandomMatrix
{
public static void main(String[] args)
{
//create the matrix
final int row = 5;
final int col = 5;
int [][] matrix = new int [row][col];
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
matrix[i][j] = (int)(Math.random()*1000) + 1;
}
}
//display matrix
System.out.println(\"\ Matrix: \");
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
System.out.print(matrix[i][j] + \"\\t\");
}
System.out.println();
}
double sum = 0;
int rowSum;
int columnSum;
int highestRow = 0;
int highestColumn = 0;
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
sum = sum + matrix[i][j];
}
}
double average = sum/25;
System.out.println(\"\ Total of all values in the array: \" + sum);
System.out.println(\"Total of all values in the array: \" + average);
System.out.println();
for (int i = 0; i < matrix.length; i++)
{
rowSum = 0;
highestRow = matrix[i][0];
for (int j = 0; j < matrix[i].length; j++)
{
rowSum = rowSum + matrix[i][j];
if(matrix[i][j] > highestRow)
highestRow = matrix[i][j];
}
System.out.println(\"Total of row \" + (i+1) + \": \" + rowSum);
System.out.println(\"Highest value in row \" + (i+1) + \": \" + highestRow);
}
System.out.println();
for (int i = 0; i < matrix.length; i++)
{
columnSum = 0;
highestColumn = matrix[0][i];
for (int j = 0; j < matrix[i].length; j++)
{
columnSum = columnSum + matrix[j][i];
if(matrix[j][i] > highestColumn)
highestColumn = matrix[j][i];
}
System.out.println(\"Total of column \" + (i+1) + \": \" + columnSum);
System.out.println(\"Highest value in column \" + (i+1) + \": \" + highestColumn);
}
}
}
/*
output:
Matrix:
715 823 726 72 832
478 269 356 869 340
56 198 548 182 704
877 174 336 295 438
82 883 281 607 776
Total of all values in the array: 11917.0
Total of all values in the array: 476.68
Total of row 1: 3168
Highest value in row 1: 832
Total of row 2: 2312
Highest value in row 2: 869
Total of row 3: 1688
Highest value in row 3: 704
Total of row 4: 2120
Highest value in row 4: 877
Total of row 5: 2629
Highest value in row 5: 883
Total of column 1: 2208
Highest value in column 1: 877
Total of column 2: 2347
Highest value in column 2: 883
Total of column 3: 2247
Highest value in column 3: 726
Total of column 4: 2025
Highest value in column 4: 869
Total of column 5: 3090
Highest value in column 5: 832
*/


