how to print the average of each column of 2d n*m array using java

how to print the average of each column of 2d n*m array using java

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 Tutorials/Questions & Answers:
how to print the average of each column of 2d n*m array using java
using 2D Array
Advertisements
using 2D Array
array average across column
How to calculate average of array in Java?
Read text file to 2D array and sorting the second column
how to find inverse of n*n 2d array in java
code to display images using 2d array - Swing AWT
how to iterate a map in java using for each
how to iterate a map in java using for each
how to iterate a map in java using for each
How to Print Array in JavaScript
Average of Array
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.7 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.7 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.3.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.3.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.3.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.3.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.4.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.4.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.9.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.9.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.8.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.8.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.6 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.6 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.5 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.5 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.4 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.4 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.3 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.3 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.0.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.0.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.0.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.0.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.8 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.8 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.6 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.6 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.5 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.5 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.4 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.4 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.3 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.3 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.2 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.2 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 2.25 is released. Learn to use n2o-platform-starter-jaxrs-server version 2.25 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 2.24 is released. Learn to use n2o-platform-starter-jaxrs-server version 2.24 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 2.23 is released. Learn to use n2o-platform-starter-jaxrs-server version 2.23 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 5.1.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 5.1.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.5.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.5.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 5.0.0-java8 is released. Learn to use n2o-platform-starter-jaxrs-server version 5.0.0-java8 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 5.0.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 5.0.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.10.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.10.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 5.1.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 5.1.1 in Maven based Java projects
game Nim - Java Beginners
Simplest way to print an array in Java
java 2d arrays
Storing the Color Image pixel values into 2d array
how to find subarray from given large 2d array of arbritary dimension
Selecting elements of 2D array with GUI
How to add a column with checkboxes for each record in my table in JSP
How to using Binary Search Array Java ?

Ads