0

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);
        }
    }







 }

Edited by pyTony: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by BestJewSinceJC
0

Use code tags when you post code. Also, all you have to do to figure out if your code is correct is print out the array after you read it in. If it doesn't print out the same thing as the input file, then it isn't correct. To refer to an array index, yourArray[0][1] is the first row, second column. So if you want to print out your array, use nested for loops, like this:

for (each row, i){
for (each column in the row, j){
print the integer contained in the array at i,j
}
}

Also, what you posted clearly looks like a school project. Not that I care, because my personal philosophy is that sometimes, a little bit of help will make you learn more. But your teacher might not share that philosophy - so you might not want to post your code, including a copied version of the project directions, on the web.

:)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.