Matrix M consists of 6 rows, 8 columns. Find out how many elements in each row exceed the arithmetic average value determined for this row. From the values ​​found to make an array R.

Ps made a variable, it was conceived as an arithmetic average of the line, but it did not work out

var theArithmeticMean = 0; function getRandomInt(min, max){//Функция для генерации случайного числа return Math.floor(Math.random() * (max - min)) + min; } function CreateAnArray(rows,columns){ //Функция, которая создаёт двумерный массив var arr = new Array(); for(var i=0; i<rows; i++){ arr[i] = new Array(); for(var j=0; j<columns; j++){ arr[i][j] = getRandomInt(0, 10); theArithmeticMean += parseInt(arr[i][j]); } } return arr; } var myMatrix = CreateAnArray(8,6); //Вызов функции для создания массива console.log(myMatrix); console.log(theArithmeticMean); 
  • Why did not work? That does not work? Judging by the code in the matrix of 8 rows and 6 columns, and not vice versa. - holden321
  • Well, look, it is impossible to calculate the sum of each line for further action. In the above code, he considers the sum of all the elements of the array - user321627
  • Well, get an array for the amounts of the lines. - holden321
  • Then how can I refer to a specific line? - user321627

1 answer 1

 /** * Функция для генерации случайного целого числа */ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } /** * Функция, которая создаёт двумерный массив */ function CreateAnArray(rows, columns) { var arr = new Array(); for (var i = 0; i < rows; i++) { arr[i] = new Array(); for (var j = 0; j < columns; j++) { arr[i][j] = getRandomInt(0, 10); } } return arr; } /** * Возвращает массив с количеством элементов превышающих средние по срокам в матрице */ function getCountsAverages(matrix) { var result = []; for (var i = 0; i < matrix.length; i++) { // считаем среднее для строки var sum = 0; for (var j = 0; j < matrix[i].length; j++) { sum += matrix[i][j]; } var average = sum / matrix[i].length; // считаем количество элементов превысивших среднее по строке result[i] = 0; for (var j = 0; j < matrix[i].length; j++) { if (matrix[i][j] > average) { result[i] += 1; } } // для проверки выводим в консоль console.log("строка " + (i + 1) + ": " + matrix[i], "среднее: " + average, "количество: " + result[i]); } return result; } var myMatrix = CreateAnArray(8, 6); // Вызов функции для создания массива //console.log(myMatrix); var P = getCountsAverages(myMatrix); console.log("результат: " + P);