Users Online

· Guests Online: 23

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Find the Sum of Each Row of the Matrix

C# Program to Find the Sum of Each Row of the Matrix

 

 

This is a C# Program to find sum of the elements of each row of the given matrix.

Problem Description

This C# Program Finds Sum of the Elements of each Row of the Given Matrix.

Problem Solution

Here the sum of each row of the matrix is obtained by adding the elemnts of that corresponding row.

Program/Source Code

Here is source code of the C# Program to Find Sum of the Elements of each Row of the Given Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find Sum of the Elements of each Row 
 * of the Given Matrix
 */
using System;
using System.Collections.Generic;
using System.Text;
namespace matrix_row_sum
{
    class mat
    {
        int i, j, m, n;
        int[,] a = new int[20, 20];
        public void getmatrix()
        {
            Console.WriteLine("Enter The Number of Rows : ");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter The Number of Columns : ");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Elements");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("Given Matrix");
            for (i = 1; i <= m; i++)
            {
                for (j = 1; j <= n; j++)
                {
                    Console.Write("\t{0}", a[i, j]);
                }
                Console.WriteLine();
            }
        }
        public void row()
        {
            int r;
            for (i = 1; i <= m; i++)
            {
                r = 0;
                for (j = 1; j <= n; j++)
                {
                    r = r + a[i, j];
                }
                Console.WriteLine("{0} Row Sum : {1}", i, r);
            }
        }
   }
    class matrowsum
    {
        static void Main(string[] args)
        {
            mat ma = new mat();
            ma.getmatrix();
            ma.row();
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are creating an object variable ‘ma’ to the mat class. The getmatrix() method is used to find sum of the elements of each row of the given matrix. We are reading the number of rows and columns of the matrix using ‘m’ and ‘n’ variables respectively.

 

Using for loop we are entering the coefficient element values for the array variable a[i,j]. Using for loop in row() method, compute the sum of the elements of each column. Print the sum of the elements of each row of the matrix.

Runtime Test Cases
 
Enter the Number of Rows : 2
Enter the Number of Columns : 2
Enter the Elements : 
1
2
3
4
Given Matrix :
        1      2
        3      4
1 Row Sum : 3
2 Row Sum : 7

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.14 seconds
14,558,306 unique visits