In this assignment, your task is to read the file data3.txt, located at the webs
ID: 3787632 • Letter: I
Question
In this assignment, your task is to read the file data3.txt, located at the website. Once you read the file, populate a two dimensional array with the information in the file. Notice that the first line of the file corresponds to the actual dimensions of the array (i.e., rows and columns), the rest of the lines in the file corresponds to the actual data that must be stored in the array. Once you fill out your array, test the four methods you implemented which are: getRowTotal: takes two arguments a 2D array that contains ints, and an int representing the row for the array, which it is desired to find the total of that row. getColTotal: takes two arguments a 2D array that contains ints, and an int representing the col for the array, which it is desired to find the total of that column. getsum: will take the 2D array of ints and will return the sum of all the indexes of the array. get AVG: will take the 2D array of ints and will return the average of the array providedExplanation / Answer
HI buddy, please find the below java program. The code is self explanatory. Change the variable "path" to point to your file.
import java.io.*;
/**
*
* @author PrudhviNIT
*/
public class EasyProb{
/**
* @param args the command line arguments
*/
static int getRowTotal(int arr[][],int row){
int ans = 0;
int cols = arr[0].length;
for(int i=0;i<cols;i++){
ans = ans + arr[row][i];
}
return ans;
}
static int getColTotal(int arr[][],int col){
int ans = 0;
int rows = arr.length;
for(int i=0;i<rows;i++){
ans = ans + arr[i][col];
}
return ans;
}
static int getSum(int arr[][]){
int sum = 0;
int rows = arr.length;
int cols = arr[0].length;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
sum = sum+arr[i][j];
}
}
return sum;
}
static double getAVG(int arr[][]){
int sum = 0;
int rows = arr.length;
int cols = arr[0].length;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
sum = sum+arr[i][j];
}
}
//Average is returned as the sum/total elements count (rows*columns)
return (sum*1d)/(rows*cols);
}
public static void main(String[] args) throws IOException{
// TODO code application logic here
String path = "data3.txt";
FileInputStream fis = new FileInputStream(new File(path));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String dim[] = br.readLine().trim().split(" ");
int r = Integer.parseInt(dim[0]);
int c = Integer.parseInt(dim[1]);
int arr[][] = new int[r][c];
for(int i=0;i<r;i++){
String str[] = br.readLine().trim().split(" ");
for(int j=0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.