Write a program that reads in a table of integers as a two-dimensional array, th
ID: 3806624 • Letter: W
Question
Write a program that reads in a table of integers as a two-dimensional array, then calculates the sum of each row and column.
Notes
The input file will begin with a single integer n, which indicates how many rows andcolumns will be in the table. For example, a 3 indicates a 3 x 3 table of data. This program does not need to check for invalid data. Format your output to look clean and professional.You should format your output in a way that shows the totals are separate from the rest of the numbers.
Do not hard code the input file into the code. I will be using reader.Next() to read in the values from the .dat file. Include while(reader.hasNext) to read through the file until all the values are read in.
Example of input/output:
o 3 9 3 99 3 4 3 5 5 1 1 3 2 14 3 9 9 3 24 4 3 5 17 1 1 2 7 11 8 16 25 17 o 3 9 2 8 8 3 99 3 1 2 4 3 5 5 3 4 1 1 7 9 8 9 3 9 File in dat cution File in2.dat.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RowColumnSum {
public static void averageEachRow(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; i++) {
double sum = 0;
for (int j = 0; j < col; j++) {
sum += matrix[i][j];
}
System.out.println("Average of " + (i + 1) + " row: "
+ String.format("%.3f", sum /col));
}
}
public static void averageEachCol(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < col; i++) {
double sum = 0;
for (int j = 0; j < row; j++) {
sum += matrix[j][i];
}
System.out.println("Average of " + (i + 1) + " column: "
+ String.format("%.3f", sum /row));
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter input file name: ");
String fileName = sc.next();
// opening input file
Scanner reader = new Scanner(new File(fileName));
// reading dimension
int n = reader.nextInt();
// creating 2D array
int [][] matrix = new int[n][n];
// reading in matrix
int i=0;
while(i < n && reader.hasNextInt()){
int j = 0;
while(j < n && reader.hasNextInt()){
matrix[i][j] = reader.nextInt();
j++;
}
i++;
}
// array to store row and column sum
int rowSum[] = new int[n];
int colSum[] = new int[n];
// row sum
for (int r = 0; r < n; r++) {
int sum = 0;
for (int c = 0; c < n; c++) {
sum += matrix[r][c];
}
rowSum[r] = sum;
}
// col sum
for (int c = 0; c < n; c++) {
int sum = 0;
for (int r = 0; r < n; r++) {
sum += matrix[r][c];
}
colSum[c] = sum;
}
// display
for (int r = 0; r < n; r++) {
int sum = 0;
for (int c = 0; c < n; c++) {
System.out.print(matrix[r][c]+" ");
}
System.out.println(" : "+rowSum[r]);
}
System.out.println("---------------------------");
for(int c=0; c<n; c++)
System.out.print(colSum[c]+" ");
System.out.println();
}
}
/*
######## inputData.txt ##########
4
0 3 9 2
3 9 9 3
4 3 5 5
1 1 2 7
Sample output:
Enter input file name: inputData.txt
0 3 9 2 : 14
3 9 9 3 : 24
4 3 5 5 : 17
1 1 2 7 : 11
---------------------------
8 16 25 17
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.