Home Answers Viewqa Java-Beginners how to print the average of each column of 2d n*m array using java

 
 


Raghav Purankar
how to print the average of each column of 2d n*m array using java
1 Answer(s)      2 years and a month ago
Posted in : Java Beginners

here is my code:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadContents {
    public static void main(String[] args) throws IOException {
        File file = new File("file1.txt");
        List<float[]> list = new ArrayList<float[]>();
        Scanner scanner = new Scanner(file).useDelimiter("\n");
        while (scanner.hasNext()) {     
            String[] values = scanner.next().trim().split(" "); 
             float[] floats = new float[values.length];
             for (int i = 0; i < values.length; i++) {
                 floats[i] = Float.parseFloat(values[i]);
                 }
                 list.add(floats);
                 } 
                 float[][] values = new float[list.size()][];
                 for (int i = 0; i < list.size(); i++) {
                     values[i] = list.get(i);
                     for (int j = 0; j < values[i].length; j++) {
                         System.out.print(values[i][j] + " ");
                         }
                         System.out.println();
                 }

it prints the content of file.now i want to calculate mean(average) of each column present in the given file having say n*m matrix.
View Answers

May 3, 2011 at 10:47 AM


import java.io.File;
import java.io.IOException;
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class ReadContents {
    public static void main(String[] args) throws IOException {
        float column[] = new float[4];
        File file = new File("rfg.txt"); 
        List<float[]> list = new ArrayList<float[]>(); 
        Scanner scanner = new Scanner(file).useDelimiter("\n");
        while (scanner.hasNext()) {
        String[] values = scanner.next().trim().split(" ");
        float[] floats = new float[values.length];
        for (int i = 0; i < values.length; i++) {
            floats[i] = Float.parseFloat(values[i]); 
            } 
            list.add(floats); 
            }
            float[][] values = new float[list.size()][];
            for (int i = 0; i < list.size(); i++) {
                values[i] = list.get(i);
                for (int j = 0; j < values[i].length; j++) {
                    System.out.print(values[i][j] + " ");
                    column[j] = column[j] + values[i][j];
                    }
                    System.out.println();
                    }
                    for (int i = 0; i < column.length; i++) {
                    System.out.println("Sum of column: "+(i+1)+": "+column[i]);
                    }
    }
}









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.