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

In this homework, you are going to write code to find the sum and average from c

ID: 3604638 • Letter: I

Question

In this homework, you are going to write code to find the sum and average from columns and rows in a dataset using Arrays and StdLib. In order to do this, you will need an input file (e.g., see five_by_five.txt). Also, you will write to an output file (spreadsheet.txt) such that the output file has both the data from the input file and the sum and average for each row and column of the input data. Your program will first read in the number of rows and then the number of columns from the input file. Then, declare and initialize an array that has dimensions 2 entries bigger than your number of columns and the number of rows specified in the input file (i.e., [rows + 2][cols + 2]). Your input file will look something like this: 5 5 3.0 4.7 8.4 7.3 9.2 2.0 1.7 11.3 4.4 7.8 14.1 9.7 8.8 1.3 2.4 5.5 8.6 6.7 9.9 7.7 11.3 2.4 8.7 6.8 7.9 Using nested for loops, set the array values equal to the input from the input file by using StdIn.readDouble(); . Also in these loops, you will have to keep track of the sum of the row that you are currently in. As you are reading in the data from each row, you’ll need to store the row totals and row averages for each row once you’ve gone through your entire inner loop (added each entry from that row to your sum). array1[i][cols] = sum; array1[i][cols+1] = sum/rows; Then using a new nested for loop where your columns are represented in the outer loop, calculate the average and sum of each column and store these in the last rows of your array: array1[rows][j] = sum; array1[rows+1][j] = sum/cols; Finally, loop through the array you have created and print out to a file in your project folder named spreadsheet.txt. The output should give the original data that you read in from the input file along with a column of row sums and averages and a row of column sums and averages. The output should look something like it does below. This line of code is a hint as to how you can have the output look like this. StdOut.printf("%.1f |",array1[i][j]);
To run this in the command line from your project folder, you can run something along the lines of: java -cp bin;stdlib.jar HW3 < five_by_five.txt > spreadsheet.txt Extra Credit (0.5 points) As you can see in the output above on the rightmost column in the last row, the average for all entries in the original data are displayed (6.9). If you are able to get the overall average for the data, you will receive extra credit of 0.5 points. In this homework, you are going to write code to find the sum and average from columns and rows in a dataset using Arrays and StdLib. In order to do this, you will need an input file (e.g., see five_by_five.txt). Also, you will write to an output file (spreadsheet.txt) such that the output file has both the data from the input file and the sum and average for each row and column of the input data. Your program will first read in the number of rows and then the number of columns from the input file. Then, declare and initialize an array that has dimensions 2 entries bigger than your number of columns and the number of rows specified in the input file (i.e., [rows + 2][cols + 2]). Your input file will look something like this: 5 5 3.0 4.7 8.4 7.3 9.2 2.0 1.7 11.3 4.4 7.8 14.1 9.7 8.8 1.3 2.4 5.5 8.6 6.7 9.9 7.7 11.3 2.4 8.7 6.8 7.9 Using nested for loops, set the array values equal to the input from the input file by using StdIn.readDouble(); . Also in these loops, you will have to keep track of the sum of the row that you are currently in. As you are reading in the data from each row, you’ll need to store the row totals and row averages for each row once you’ve gone through your entire inner loop (added each entry from that row to your sum). array1[i][cols] = sum; array1[i][cols+1] = sum/rows; Then using a new nested for loop where your columns are represented in the outer loop, calculate the average and sum of each column and store these in the last rows of your array: array1[rows][j] = sum; array1[rows+1][j] = sum/cols; Finally, loop through the array you have created and print out to a file in your project folder named spreadsheet.txt. The output should give the original data that you read in from the input file along with a column of row sums and averages and a row of column sums and averages. The output should look something like it does below. This line of code is a hint as to how you can have the output look like this. StdOut.printf("%.1f |",array1[i][j]);
To run this in the command line from your project folder, you can run something along the lines of: java -cp bin;stdlib.jar HW3 < five_by_five.txt > spreadsheet.txt Extra Credit (0.5 points) As you can see in the output above on the rightmost column in the last row, the average for all entries in the original data are displayed (6.9). If you are able to get the overall average for the data, you will receive extra credit of 0.5 points. In this homework, you are going to write code to find the sum and average from columns and rows in a dataset using Arrays and StdLib. In order to do this, you will need an input file (e.g., see five_by_five.txt). Also, you will write to an output file (spreadsheet.txt) such that the output file has both the data from the input file and the sum and average for each row and column of the input data. Your program will first read in the number of rows and then the number of columns from the input file. Then, declare and initialize an array that has dimensions 2 entries bigger than your number of columns and the number of rows specified in the input file (i.e., [rows + 2][cols + 2]). Your input file will look something like this: 5 5 3.0 4.7 8.4 7.3 9.2 2.0 1.7 11.3 4.4 7.8 14.1 9.7 8.8 1.3 2.4 5.5 8.6 6.7 9.9 7.7 11.3 2.4 8.7 6.8 7.9 Using nested for loops, set the array values equal to the input from the input file by using StdIn.readDouble(); . Also in these loops, you will have to keep track of the sum of the row that you are currently in. As you are reading in the data from each row, you’ll need to store the row totals and row averages for each row once you’ve gone through your entire inner loop (added each entry from that row to your sum). array1[i][cols] = sum; array1[i][cols+1] = sum/rows; Then using a new nested for loop where your columns are represented in the outer loop, calculate the average and sum of each column and store these in the last rows of your array: array1[rows][j] = sum; array1[rows+1][j] = sum/cols; Finally, loop through the array you have created and print out to a file in your project folder named spreadsheet.txt. The output should give the original data that you read in from the input file along with a column of row sums and averages and a row of column sums and averages. The output should look something like it does below. This line of code is a hint as to how you can have the output look like this. StdOut.printf("%.1f |",array1[i][j]);
To run this in the command line from your project folder, you can run something along the lines of: java -cp bin;stdlib.jar HW3 < five_by_five.txt > spreadsheet.txt Extra Credit (0.5 points) As you can see in the output above on the rightmost column in the last row, the average for all entries in the original data are displayed (6.9). If you are able to get the overall average for the data, you will receive extra credit of 0.5 points. In this homework, you are going to write code to find the sum and average from columns and rows in a dataset using Arrays and StdLib. In order to do this, you will need an input file (e.g., see five_by_five.txt). Also, you will write to an output file (spreadsheet.txt) such that the output file has both the data from the input file and the sum and average for each row and column of the input data. Your program will first read in the number of rows and then the number of columns from the input file. Then, declare and initialize an array that has dimensions 2 entries bigger than your number of columns and the number of rows specified in the input file (i.e., [rows + 2][cols + 2]). Your input file will look something like this: 5 5 3.0 4.7 8.4 7.3 9.2 2.0 1.7 11.3 4.4 7.8 14.1 9.7 8.8 1.3 2.4 5.5 8.6 6.7 9.9 7.7 11.3 2.4 8.7 6.8 7.9 Using nested for loops, set the array values equal to the input from the input file by using StdIn.readDouble(); . Also in these loops, you will have to keep track of the sum of the row that you are currently in. As you are reading in the data from each row, you’ll need to store the row totals and row averages for each row once you’ve gone through your entire inner loop (added each entry from that row to your sum). array1[i][cols] = sum; array1[i][cols+1] = sum/rows; Then using a new nested for loop where your columns are represented in the outer loop, calculate the average and sum of each column and store these in the last rows of your array: array1[rows][j] = sum; array1[rows+1][j] = sum/cols; Finally, loop through the array you have created and print out to a file in your project folder named spreadsheet.txt. The output should give the original data that you read in from the input file along with a column of row sums and averages and a row of column sums and averages. The output should look something like it does below. This line of code is a hint as to how you can have the output look like this. StdOut.printf("%.1f |",array1[i][j]);
To run this in the command line from your project folder, you can run something along the lines of: java -cp bin;stdlib.jar HW3 < five_by_five.txt > spreadsheet.txt Extra Credit (0.5 points) As you can see in the output above on the rightmost column in the last row, the average for all entries in the original data are displayed (6.9). If you are able to get the overall average for the data, you will receive extra credit of 0.5 points. In this homework, you are going to write code to find the sum and average from columns and rows in a dataset using Arrays and StdLib. In order to do this, you will need an input file (e.g., see five_by_five.txt). Also, you will write to an output file (spreadsheet.txt) such that the output file has both the data from the input file and the sum and average for each row and column of the input data. Your program will first read in the number of rows and then the number of columns from the input file. Then, declare and initialize an array that has dimensions 2 entries bigger than your number of columns and the number of rows specified in the input file (i.e., [rows + 2][cols + 2]). Your input file will look something like this: 5 5 3.0 4.7 8.4 7.3 9.2 2.0 1.7 11.3 4.4 7.8 14.1 9.7 8.8 1.3 2.4 5.5 8.6 6.7 9.9 7.7 11.3 2.4 8.7 6.8 7.9 Using nested for loops, set the array values equal to the input from the input file by using StdIn.readDouble(); . Also in these loops, you will have to keep track of the sum of the row that you are currently in. As you are reading in the data from each row, you’ll need to store the row totals and row averages for each row once you’ve gone through your entire inner loop (added each entry from that row to your sum). array1[i][cols] = sum; array1[i][cols+1] = sum/rows; Then using a new nested for loop where your columns are represented in the outer loop, calculate the average and sum of each column and store these in the last rows of your array: array1[rows][j] = sum; array1[rows+1][j] = sum/cols; Finally, loop through the array you have created and print out to a file in your project folder named spreadsheet.txt. The output should give the original data that you read in from the input file along with a column of row sums and averages and a row of column sums and averages. The output should look something like it does below. This line of code is a hint as to how you can have the output look like this. StdOut.printf("%.1f |",array1[i][j]);
To run this in the command line from your project folder, you can run something along the lines of: java -cp bin;stdlib.jar HW3 < five_by_five.txt > spreadsheet.txt Extra Credit (0.5 points) As you can see in the output above on the rightmost column in the last row, the average for all entries in the original data are displayed (6.9). If you are able to get the overall average for the data, you will receive extra credit of 0.5 points.

Explanation / Answer

five_by_five.txt

5
5
3.0 4.7 8.4 7.3 9.2
2.0 1.7 11.3 4.4 7.8
14.1 9.7 8.8 1.3 2.4
5.5 8.6 6.7 9.9 7.7
11.3 2.4 8.7 6.8 7.9

MatrixSumAverage.java

package org.students;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class MatrixSumAverage {

public static void main(String[] args) throws IOException {

int rows = 0, cols = 0;
double matrix[][] = null;
double overallTot = 0.0, overallAvg = 0.0;
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat(".0");
try {
//Opering the input file
Scanner sc = new Scanner(new File("five_by_five.txt"));

//Reading the data from the file
while (sc.hasNext()) {
rows = Integer.parseInt(sc.next());
cols = Integer.parseInt(sc.next());
matrix = new double[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = Double.parseDouble(sc.next());
}
}
}
} catch (FileNotFoundException e) {

e.printStackTrace();
}

//Writing to the output file
FileWriter out = new FileWriter("spreadsheet.txt");
BufferedWriter writer = new BufferedWriter(out);
double tot = 0.0, sum = 0.0, avg = 0.0;
System.out.println(" Row1 Row2 Row3 Row4 Row5 Sum Average");
writer.write(" Row1 Row2 Row3 Row4 Row5 Sum Average");
writer.newLine();
for (int i = 0; i < rows; i++) {
tot = 0.0;
System.out.print(" Col" + (i + 1) + ":");
writer.write(" Col" + (i + 1) + ":");
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
writer.write(matrix[i][j] + " ");
tot += matrix[i][j];
overallTot += matrix[i][j];

}

System.out.printf("%.1f ", tot);
writer.write(df.format(tot) + " ");
avg = tot / rows;
System.out.printf("%.1f", avg);
writer.write(df.format(avg));

System.out.println();
writer.newLine();
}
System.out.print(" Sum:");
writer.write(" Sum:");
for (int i = 0; i < rows; i++) {
tot = 0.0;

for (int j = 0; j < cols; j++) {

tot += matrix[j][i];

}
System.out.printf("%.1f ", tot);
writer.write(df.format(tot) + " ");

}

writer.newLine();
System.out.print(" Avg:");
writer.write(" Avg:");
for (int i = 0; i < rows; i++) {
tot = 0.0;

for (int j = 0; j < cols; j++) {

tot += matrix[j][i];

}
avg = tot / cols;
System.out.printf("%.1f ", avg);
writer.write(df.format(avg) + " ");

}

writer.close();
out.close();
System.out.printf(" Overall Average :%.1f", overallTot / (rows * cols));
}

}

_________________

Output:

Row1 Row2 Row3 Row4 Row5 Sum Average

Col1:3.0 4.7 8.4 7.3 9.2 32.6 6.5

Col2:2.0 1.7 11.3 4.4 7.8 27.2 5.4

Col3:14.1 9.7 8.8 1.3 2.4 36.3 7.3

Col4:5.5 8.6 6.7 9.9 7.7 38.4 7.7

Col5:11.3 2.4 8.7 6.8 7.9 37.1 7.4

Sum:35.9 27.1 43.9 29.7 35.0

Avg:7.2 5.4 8.8 5.9 7.0

_________________

spreadsheet.txt

Row1 Row2 Row3 Row4 Row5 Sum Average

Col1:3.0 4.7 8.4 7.3 9.2 32.6 6.5

Col2:2.0 1.7 11.3 4.4 7.8 27.2 5.4

Col3:14.1 9.7 8.8 1.3 2.4 36.3 7.3

Col4:5.5 8.6 6.7 9.9 7.7 38.4 7.7

Col5:11.3 2.4 8.7 6.8 7.9 37.1 7.4

Sum:35.9 27.1 43.9 29.7 35.0

Avg:7.2 5.4 8.8 5.9 7.0

Overall Average :6.9

_____________Could you rate me well.Plz .Thank You

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