Advertisement
sergAccount

Untitled

Jul 3rd, 2021
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.dz2;
  7.  
  8. /**
  9.  *
  10.  * @author student
  11.  */
  12. public class Main2 {
  13.     /*
  14.     Задача 7
  15.     Создать метод, который находит
  16.     среднее арифметическое элементов массива
  17.     типа double[] - параметра данного метода
  18.     Метод должен возвращать значение типа double
  19.     В методе main проверить данный метод - вывести полученное значение на экран
  20.     */
  21.     //
  22.     public static double avg(double[] arr){
  23.         // 1)
  24.         double sum = 0;
  25.         for (int i = 0; i < arr.length; i++) {
  26.             //sum+=arr[i];
  27.             sum = sum + arr[i];
  28.         }
  29.         // 2)
  30.         double result = sum / arr.length;
  31.         return result;
  32.     }    
  33.     //
  34.     public static void main(String[] args) {
  35.         //
  36.         double[] arr = {1, 2.2, 5.5, 2.00};
  37.         double result = avg(arr);
  38.         System.out.println("result=" + result);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement