/** Create a program that prompts the user for a number of rows and number of co
ID: 3669233 • Letter: #
Question
/**
Create a program that prompts the user for a number of rows and number of columns to create a 2 dimensional array (matrix) of integer values. Then ask the user for each value in the matrix. After you get those values, print the matrix with row averages, then print a second time with column averages. KEEEP ALL NUMBERS as INTEGER.
Submit one java file.
be sure the user inputs two integers and then numbers for the matrix. Hint: no strings allowed or dimensions as decimal numbers.
sample output that gets 10 extra points:
3.0, 3.0, 3.0, 3.0, avg = 3.0
7.0, 7.0, 7.0, 7.0, avg = 7.0
9.0, 9.0, 9.0, 9.0, avg = 9.0
avg= 6.3, 6.3, 6.3, 6.3,
Please modify the code below to print the average of each row
and the average of each column
import java.util.Scanner;
class MatrixLab_start
{
public static void main(String[] args)
{
int[][] M = {
{4,5,6,7},
{1,2,3,4},
{8,11,13,2}};
int rows=M.length;
int cols=M[0].length;
for(int i=0; i {
for(int k=0; k {
System.out.printf("%3d ,",M[i][k]);
}
System.out.println();
}
}
}
Can anyone modify exactly the same code, please no new context. Would be grateful if modified same codes. I tried to compile this code in Dr.Java but its giving too many errors
Explanation / Answer
public static void main (String[] args)
{
int[][] m = { {4,5,6,7},
{1,2,3,4},
{8,11,13,2},
final int rows = M.length;
final int cols = M[0].length;
int[] rowsSum = new int[rows];
int[] colsSum = new int[cols];
for (int i=0; i < rows; i++)
for (int j=0; j < cols; j++)
{
rows[i] += M[i][j];
cols[j] += M[i][j];
}
DecimalFormat fmt = new DecimalFormat ("0.#");
System.out.println ("Averages: ");
for (int i=0; i < rows; i++)
System.out.println ("i #" + (i+1) + ": " +
fmt.format ((float)rows[i]/j));
System.out.println ();
for (int j =0; j< cols; j++)
System.out.println ("j #" + (j+1) + ": " +
fmt.format ((float)cols[j]/M));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.