A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to find the sum of each column of the matrix
Q:

C# program to find the sum of each column of the matrix

0

C# program to find the sum of each column of the matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to find the sum of each column of the matrix is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the sum of each column of the matrix.

using System;

class MatrixDemo
{
    public static void Main(string[] args)
    {
        int i   = 0;
        int j   = 0;

        int sum = 0;
        int row = 2;
        int col = 2;
        
        int[,] Matrix = new int[row, col];
        
        Console.Write("Enter the elements of matrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Matrix[i, j] = int.Parse(Console.ReadLine());
            }
        }

        Console.WriteLine("\nMatrix: ");
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                Console.Write(Matrix[i, j] + "\t");
            }
            Console.WriteLine();
        }

        for (i = 0; i < row; i++)
        {
            sum = 0;
            for (j = 0; j < col; j++)
            {
                sum += Matrix[j, i];
            }
            Console.WriteLine("Sum of column[{0}]: {1}", (i + 1), sum);
        }
    }
}

Output:

Enter the elements of matrix: 1
2
3
4

Matrix:
1       2
3       4
Sum of column[1]: 4
Sum of column[2]: 6
Press any key to continue . . .

Explanation:

In the above program, we created a class MatrixDemo that contains a Main() method. The Main() method is the entry point for the program, Here we created a 2-D array to represent a matrix.

Console.Write("Enter the elements of matrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Matrix[i, j] = int.Parse(Console.ReadLine());
    }
}
Console.WriteLine("\nMatrix: ");
for (i = 0; i < row; i++)
{
    for (j = 0; j < col; j++)
    {
        Console.Write(Matrix[i, j] + "\t");
    }
    Console.WriteLine();
}

In the above code, we read the elements of matrix and print the matrix on the console screen.

for (i = 0; i < row; i++)
    {
        sum = 0;
        for (j = 0; j < col; j++)
        {
            sum += Matrix[j, i];
        }
        Console.WriteLine("Sum of column[{0}]: {1}", (i + 1), sum);
    }

Here we calculated the sum of each column of the matrix and then print the sum of each column on the matrix on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now