Advertisement
simonradev

SumMatrixElements

May 30th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. namespace SumMatrixElements
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class Startup
  8.     {
  9.         private static int totalSum;
  10.  
  11.         public static void Main()
  12.         {
  13.             int[] dimensions = SplitStringToArray();
  14.  
  15.             int rowsOfMatrix = dimensions[0];
  16.             int colsOfMatrix = dimensions[1];
  17.  
  18.             int[][] matrix = new int[rowsOfMatrix][];
  19.  
  20.             totalSum = 0;
  21.             for (int row = 0; row < rowsOfMatrix; row++)
  22.             {
  23.                 SplitStringToArray();
  24.             }
  25.  
  26.             StringBuilder result = new StringBuilder();
  27.             result.AppendLine(rowsOfMatrix.ToString());
  28.             result.AppendLine(colsOfMatrix.ToString());
  29.             result.AppendLine(totalSum.ToString());
  30.  
  31.             Console.Write(result);
  32.         }
  33.  
  34.         private static int[] SplitStringToArray()
  35.         {
  36.             return Console.ReadLine().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(ParseAndAddNumberToSum).ToArray();
  37.         }
  38.  
  39.         private static int ParseAndAddNumberToSum(string numberToParse)
  40.         {
  41.             int numberToReturn = int.Parse(numberToParse);
  42.  
  43.             totalSum += numberToReturn;
  44.  
  45.             return numberToReturn;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement