Advertisement
gabi11

Multidimensional Arrays - 3. Primary Diagonal

May 12th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int dimentions = int.Parse(Console.ReadLine());
  14.  
  15.             int rows = dimentions;
  16.             int cols = dimentions;
  17.  
  18.             var matrix = new int[rows, cols];
  19.  
  20.             for (int row = 0; row < rows; row++)
  21.             {
  22.                 var currentRow = Console.ReadLine()
  23.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  24.                 .Select(int.Parse)
  25.                 .ToArray();
  26.  
  27.                 for (int col = 0; col < cols; col++)
  28.                 {
  29.                     matrix[row, col] = currentRow[col];
  30.                 }
  31.             }
  32.  
  33.             int primaryRow = 0;
  34.             int primaryCol = 0;
  35.             var sum = 0;
  36.  
  37.             while (true)
  38.             {
  39.                 if (primaryRow < 0
  40.                     || primaryRow >= matrix.GetLength(0)
  41.                     || primaryCol < 0
  42.                     || primaryCol >= matrix.GetLength(1))
  43.                 {
  44.                     break;
  45.                 }
  46.  
  47.                 sum += matrix[primaryRow, primaryCol];
  48.                 primaryRow++;
  49.                 primaryCol++;
  50.             }
  51.             Console.WriteLine(sum);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement