Arsalan’s Musings

Musings on Software…

Adding rows and columns in a square matrix (C# Solution)

leave a comment »

This is a variation to the problem/solution posted on Ali Abbas Rizvi’s blog. I wanted to allow the user to enter matrix size for each run and to get a view of a randomly generated matrix in addition to all the sums for rows and columns. I also wanted to create a design that would lend itself favorably to adding new functionality. As an example, two methods (AddAllRows and AddAllColumns) have been added to the SquareMatrix class.

// Problem: Given a matrix, add rows and columns
// Author: Ahmed Arsalan
// (c) 2008. Please cite source if used in your presentation.

using System;
using System.Linq;
using System.Text;

namespace SolvingSmallProblems
{
    class Program
    {
        static void Main(string[] args)
        {
            SquareMatrix aMatrix;
            int matrixSize;

            Console.Out.WriteLine("Please enter matrix size (e.g. 10):");
            try
            {
                matrixSize = Int32.Parse(Console.In.ReadLine().Trim());
            }
            catch (Exception)
            {
                Console.Out.WriteLine("Please entger a valid integer.");
                throw new Exception("Invalid size entered!");
            }
            Console.Out.WriteLine("The randomly generated matrix looks like this:");

            aMatrix = new SquareMatrix(matrixSize);

            for (int underline = 0; underline < matrixSize; underline++)
            {
                Console.Out.Write("____");
            }
            aMatrix.Show();

            Console.Out.WriteLine("Press ENTER to generate sums...");
            Console.In.ReadLine();

            // display sums
            long [] rowSums = new long [matrixSize];
            long [] columnSums = new long [matrixSize];
            for (int rowOrColumn = 0; rowOrColumn < matrixSize; rowOrColumn++)
            {
                rowSums[rowOrColumn] = aMatrix.AddRow(rowOrColumn);
                columnSums[rowOrColumn] = aMatrix.AddColumn(rowOrColumn);

                Console.Out.WriteLine (String.Format ("The sum of row {0} is {1}, " +
                       "and the sum of column {2} is {3}. ", rowOrColumn,
                       rowSums[rowOrColumn], rowOrColumn, columnSums[rowOrColumn]));
            }

            Console.Out.WriteLine (String.Format ("The MAX of all rows is {0}, " +
                  "and the MAX of all columns is {1}", rowSums.Max(), columnSums.Max()));
            Console.In.ReadLine();
        }
    }

    class SquareMatrix
    {
        # region ------------------------ Instance Variables ------------------------
        private const int DEFAULT_MATRIX_SIZE = 10;
        int[,] matrix;
        Random randomNumber;
        int selectedMatrixSize;
        #endregion

        #region ------------------------ Constructors ------------------------
        public SquareMatrix() :this(DEFAULT_MATRIX_SIZE)
        {
            // use default size
        }

        public SquareMatrix(int matrixSize)
        {
            randomNumber = new Random(99);
            selectedMatrixSize = matrixSize;

            matrix = new int[matrixSize, matrixSize];
            for (int row = 0; row < matrixSize; row++)
            {
                for (int column = 0; column < matrixSize; column++)
                {
                    try
                    {
                        matrix[row, column] = RandomNumber; // random # between 1 and 99
                    }
                    catch (Exception)
                    {
                        Console.out.WriteLine ("Error generating random number!");
                        throw new Exception ("Error generating random number.");
                    }
                }
            }
        }
        #endregion

        # region ------------------------ Public Properties ------------------------
        public int RandomNumber
        {
            get
            {
                return randomNumber.Next(1, 99);
            }
        }
        #endregion

        #region ------------------------ Public Methods ------------------------
        public void Show()
        {
            for (int row = 0; row < selectedMatrixSize; row++);

                for (int column = 0; column < selectedMatrixSize; column++)
                {
                    Console.Out.Write(matrix[row, column]);
                    Console.Out.Write("  ");
                }

                Console.Out.WriteLine();
            }
        }

        public long AddRow(int rowNumber)
        {
            long addedRow = 0;
            for (int column = 0; column < selectedMatrixSize; column++)
            {
                addedRow += matrix[rowNumber, column];
            }

            return addedRow;

        }

        public long AddColumn(int columnNumber)
        {
            long addedColumn = 0;
            for (int row = 0; row < selectedMatrixSize; row++)
            {
                addedColumn += matrix[row, columnNumber];
            }

            return addedColumn;

        }

        public long AddAllRows()
        {
            long addedAllRows = 0;
            for (int row = 0; row < selectedMatrixSize; row++)
            {
                addedAllRows += AddRow(row);
            }
            return addedAllRows;
        }

        public long AddAllColumns()
        {
            long addedAllColumns = 0;
            for (int column = 0; column < selectedMatrixSize; column++)
            {
                addedAllColumns += AddColumn(column);
            }
            return addedAllColumns;
        }
        #endregion

    }
}

Written by Arsalan

January 8, 2008 at 1:36 am

Posted in 1

Leave a Reply