Venciity

[Jumpstart C#] Multidimensional-Arrays 03-FiniteElementsSum

Mar 5th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. //  Намерете сумата от крайните елементи на матрицата A(M,N).
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02_NegaitveElements
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int[,] a = new int[4, 5]
  15.             {
  16.                 {5, 1, 6, -9, 4},
  17.                 {3, 7, -5, 0, -3},
  18.                 {-1, 4, -2, 6, 3},
  19.                 {-1, 4, -2, 6, 3}
  20.             };
  21.  
  22.             int rows = a.GetLength(0);
  23.             int cols = a.GetLength(1);
  24.  
  25.             int sum = 0;
  26.  
  27.             for (int i = 0; i < cols; i++)
  28.             {
  29.                 sum += a[0, i];
  30.                 sum += a[rows - 1, i];
  31.             }
  32.  
  33.             for (int i = 1; i < rows-1; i++)
  34.             {
  35.                 sum += a[i, 0];
  36.                 sum += a[i, cols-1];
  37.             }
  38.  
  39.             Console.WriteLine("Sum = {0}",sum);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment