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

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, youll need to store the row totals and row averages for each row once youve 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!