Advertisement
sergAccount

Untitled

Mar 7th, 2021
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 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 javaapplication4;
  7.  
  8. /**
  9.  *
  10.  * @author Admin
  11.  */
  12. public class JavaApplication4 {
  13.    
  14.     /*
  15.     Задача1
  16.     Создать двумерный массив значений типа double
  17.     Заполнить его числами  
  18.     Создать метод который находит сумму элементов
  19.     двумерного массива    
  20.     */    
  21.     public static double calcSum(double[][] arr){
  22.         double res = 0;
  23.         for (int i = 0; i < arr.length; i++) {
  24.             for (int j = 0; j < arr[i].length; j++) {
  25.                 res += arr[i][j];
  26.             }
  27.         }
  28.         return res;
  29.     }    
  30.     /*
  31.     Задача2
  32.     Создать двумерный массив значений типа int
  33.     Заполнить его числами  
  34.     Создать метод который находит сумму элементов
  35.     которые находятся на главной диагонали
  36.     */
  37.     public static int calcDiagSum(int[][] arr){
  38.         int res = 0;
  39.         for (int i = 0; i < arr.length; i++) {
  40.             res += arr[i][i];
  41.         }
  42.         return res;
  43.     }    
  44.     /**
  45.      * @param args the command line arguments
  46.      */
  47.     public static void main(String[] args) {
  48.         // TODO code application logic here
  49.         double[][] arr = {
  50.             {1.0, 2.0},
  51.             {1.0, 2.0}
  52.         };
  53.         double result = calcSum(arr);
  54.         System.out.println("result=" + result);
  55.        
  56.         int[][] arr2 = {
  57.             {0, 2},
  58.             {3, 8}            
  59.         };
  60.         // 1 - [0][0] 4 - [1][1]
  61.         int res = calcDiagSum(arr2);
  62.         System.out.println("res=" + res);
  63.     }
  64.    
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement