c-sharp
  1. c-sharp-program-to-add-two-matrices-using-multi-dimensional-arrays

Program to Add Two Matrices Using Multi-dimensional Arrays - (C# Basic Programs)

Adding two matrices is a common programming exercise that helps improve problem-solving skills in C#. In this tutorial, we will discuss a program to add two matrices using multi-dimensional arrays in C#.

Syntax

The syntax for adding two matrices using multi-dimensional arrays in C# is as follows:

for (int i = 0; i < rows; i++) {
  for (int j = 0; j < columns; j++) {
    c[i,j] = a[i,j] + b[i,j];
  }
}

Example

using System;

public class Matrix {
    public static void Main() {
        int rows, columns;
        Console.Write("Enter the number of rows for matrix: ");
        rows = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of columns for matrix: ");
        columns = Convert.ToInt32(Console.ReadLine());

        int[,] a = new int[rows,columns];
        int[,] b = new int[rows,columns];
        int[,] c = new int[rows,columns];

        Console.WriteLine("Enter the values of matrix A:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                a[i,j] = Convert.ToInt32(Console.ReadLine());
            }
        }

        Console.WriteLine("Enter the values of matrix B:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                b[i,j] = Convert.ToInt32(Console.ReadLine());
            }
        }

        Console.WriteLine("Matrix A:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                Console.Write(a[i,j] + "\t");
            }
            Console.WriteLine();
        }

        Console.WriteLine("Matrix B:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                Console.Write(b[i,j] + "\t");
            }
            Console.WriteLine();
        }

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                c[i,j] = a[i,j] + b[i,j];
            }
        }

        Console.WriteLine("Resultant Matrix C (A + B):");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                Console.Write(c[i,j] + "\t");
            }
            Console.WriteLine();
        }

        Console.ReadKey();
    }
}

Output

Enter the number of rows for matrix: 3
Enter the number of columns for matrix: 3
Enter the values of matrix A:
1
2
3
4
5
6
7
8
9
Enter the values of matrix B:
9
8
7
6
5
4
3
2
1
Matrix A:
1       2       3
4       5       6
7       8       9
Matrix B:
9       8       7
6       5       4
3       2       1
Resultant Matrix C (A + B):
10      10      10
10      10      10
10      10      10

Explanation

The program prompts the user to enter the number of rows and columns for the matrices. It then creates three multi-dimensional arrays of the same size - one for each of the matrices and one for the resultant matrix. The program prompts the user to enter the values for each matrix, and then prints out matrices A and B. Finally, the program adds matrices A and B to produce the resultant matrix C and prints it out to the user.

Use

This program is useful for learning about multi-dimensional arrays, iteration, and mathematical operations in C#. This program can be used as a starting point for more complex matrix calculations and operations.

Summary

In this tutorial, we discussed a program to add two matrices using multi-dimensional arrays in C#. We provided the syntax, example, explanation, use, and output of the program. By practicing this exercise, programmers can improve their problem-solving skills and become better equipped to tackle complex coding challenges.

Published on: