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

Ads









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
How to calculate average of array in Java?
array average across column
how to find inverse of n*n 2d array in java
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
How to using Binary Search Array Java ?
Simplest way to print an array in Java
how to find subarray from given large 2d array of arbritary dimension
java 2d arrays
Storing the Color Image pixel values into 2d array
Selecting elements of 2D array with GUI
How to add a column with checkboxes for each record in my table in JSP
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.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 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 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 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.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 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 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.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.3 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.3 in Maven based Java projects

Ads