Saturday, September 5, 2020

Comparison of Sum of Matrix Row Elements in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class MatrixRowAddition
    {
        static int Total = 0;
        static int RowNumber = 0;
        static int equals = 0;
        static bool IsEqual = true;
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");
            int[] RowsCols= Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
            Console.WriteLine("Enter Matrix Elements in Matrix Form separated by single Space");
            for (int Rows = 0; Rows < RowsCols[0]; Rows++)
            {
                int[] Elements = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), b => int.Parse(b));
                if (Rows == 0)
                {
                    equals = Elements.Sum();
                }
                if (Rows > 0)
                {
                    if (equals == Elements.Sum())
                    {
                    }
                    else
                    {
                        IsEqual = false;
                    }
                }
                if (Elements.Sum() > Total)
                {
                    Total = Elements.Sum();
                    RowNumber = Rows;
                }
            }
            if (IsEqual == false)
            {
                Console.WriteLine("Sum of Row {0} Elements are greater", RowNumber + 1);
            }
            else
            {
                Console.WriteLine("All Rows are Equal");
            }
            Console.Read();
        }
    }
}

Output 1 :











Output 2 :