gabi11

Multidimensional Arrays - 2. Sum Matrix Columns

May 12th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 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.             for (int col = 0; col < cols; col++)
  37.             {
  38.                 var sum = 0;
  39.  
  40.                 for (int row = 0; row < rows; row++)
  41.                 {
  42.                     sum += matrix[row, col];
  43.                 }
  44.  
  45.                 Console.WriteLine(sum);
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment