Two-dimensional Arrays. My question is based on this info did i code the right ? if not then let me know where did i made mistake?
Input Data File here :
88 90 94 102 111 122 134
75 77 80 86 94 103 113
80 83 85 94 100 111 121
68 71 76 85 96 110 125
77 84 91 98 105 112 119
81 85 90 96 102 109 120
Method to input data from the data file
Method to display the table (Please align columns, but no need to use borders, line separators, etc.)
Method to compute the average of each row and display it
Method to compute the average of each column and display it
Method to compute the overall average
NOT require to create a user-defined class (reference type).
The most straightforward way to approach this is to create 3 parallel arrays
(one for the names, one for the scores, and one for the deviations) in your main method.
methods for the input, for the output, for the average,
and for the deviation calculation. These methods will be called from method main.
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class Part2
{
static final int rows = 7; //this can be set to any number
static final int columns = 3; //this can be set to any number
static Scanner console = new Scanner(System.in);
public static void main (String [] args) throws FileNotFoundException
{
Scanner inFile = new Scanner (new FileReader("score.txt"));
}
public static boolean inputdata(Scanner inFile,int testScore,StringBuffer studentName)
throws FileNotFoundException
{
if (!inFile.hasNext())
return false;
studentName.delete(0,studentName.length());
//employeeName.append(inFile.nextLine());
testScore.getNum (inFile.nextInt());
inFile.nextLine(); //discards CR
return true;
}
public static void printMatrix(int[][] matrix)
{
int row, col;
for (row = 0; row < matrix.length; row++)
{
for (col = 0; col < matrix[row].length; col++)
System.out.printf("%7d", matrix[row][col]);
System.out.println();
}
}
public static void sumRows(int[][] matrix)
{
int row, col;
int sum;
//sum of each individual row
for (row = 0; row < matrix.length; row++)
{
sum = 0;
for (col = 0; col < matrix[row].length; col++)
sum = sum + matrix[row][col];
System.out.println("The sum of the elements of row "
+ (row + 1) + " = " + sum);
}
}
public static void sumColumns(int[][] matrix)
{
int row, col;
int sum;
//sum of each individual column
for (col = 0; col < matrix[0].length; col++)
{
sum = 0;
for (row = 0; row < matrix.length; row++)
sum = sum + matrix[row][col];
System.out.println("The sum of the elements of column "
+ (col + 1) + " = " + sum);
}
}
}