Good day) There is a matrix of 100x100, you need to sum each column of the matrix, divide the sum by the number of elements in the column, and add the numbers to a one-dimensional array. But I have something not very good, I can not figure it out (

public class Etalon { public static void etalonMatrix(int [][] convertToMatrix) { int [][] mas = convertToMatrix; int s = 0; for (int i = 0; i < convertToMatrix[0].length; i++){ for (int j = 0; j < convertToMatrix.length; j++){ s = s + mas [j][i]; System.out.println(s + " "); } System.out.println(" "); } } } 

This code even summarizes, but very strange, wrong)

  • “And put numbers into a one-dimensional matrix” is, after all, probably not a matrix , but an array . So, where do you create this array? Do you create? - post_zeew
  • one
    Is it possible that you don’t learn a nerd from whom you can get the right solution with an explanation for a beer / chocolate bar / smile? - E_p
  • @post_zeew yes to array), the matrix is ​​created in a separate class - Dasha Novikova
  • @E_p unfortunately no) I wouldn’t bother like that then) - Dasha Novikova

1 answer 1

The variable s in the loop is not reset - it only accumulates. More true will be this:

 public static void etalonMatrix(int [][] convertToMatrix) { int [][] mas = convertToMatrix; int s = 0; for (int i = 0; i < convertToMatrix[0].length; i++){ s = 0; for (int j = 0; j < convertToMatrix.length; j++){ s = s + mas [j][i]; System.out.println(s + " "); } System.out.println(" "); } }