Advertisement
gabi11

Multidimensional Arrays - 1. Sum Matrix Elements

May 12th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 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.             var dimentions = Console.ReadLine()
  14.                 .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse)
  16.                 .ToArray();
  17.  
  18.             int rows = dimentions[0];
  19.             int cols = dimentions[1];
  20.  
  21.             var matrix = new int[rows, cols];
  22.  
  23.             for (int row = 0; row < rows; row++)
  24.             {
  25.                 var currentRow = Console.ReadLine()
  26.                 .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  27.                 .Select(int.Parse)
  28.                 .ToArray();
  29.  
  30.                 for (int col = 0; col < cols; col++)
  31.                 {
  32.                     matrix[row, col] = currentRow[col];
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine(matrix.GetLength(0));
  37.             Console.WriteLine(matrix.GetLength(1));
  38.  
  39.             var sum = 0;
  40.  
  41.             foreach (var item in matrix)
  42.             {
  43.                 sum += item;
  44.             }
  45.  
  46.             Console.WriteLine(sum);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement