Eri M
Eri M

Reputation: 1

Finding the average of a column in a matrix with unknown elements

Can anyone help me with my code? I have to find the average of every column in a matrix but I don't know what's wrong with my code cause it doesn't work.This is my code: (By the way it shows no mistakes and I had to put the numbers with JOptionPane, thanks for your help)

import javax.swing.JOptionPane;
public class Matrix {
    private static final int String = 0;

    public static void main(String[] args) {
        double[] numbers = new double[10]; // 10 doubles
        double sum = 0.0;

        for (int i = 0; i < numbers.length; ++i) {    
            sum += numbers[i];
            String input = JOptionPane.showInputDialog("Enter a number");
            double d = Double.parseDouble(input);
            double avg = 0.0;
            avg = sum/numbers[i];
        }
    }
}

Upvotes: 0

Views: 244

Answers (1)

rgettman
rgettman

Reputation: 178263

You never assign any numbers to your numbers array, so they all default to 0.

Try:

numbers[i] = Double.parseDouble(input);
double avg = 0.0;
sum += numbers[i];
avg = sum / (i + 1); // (i + 1) is the number of inputted numbers

Upvotes: 2

Related Questions