Matrix Operation (2D array)

0

1. Matrix Addition

using System;

class MatrixAddition
{
    static void Main()
    {
        int[,] matrixA = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[,] matrixB = { { 9, 8, 7 }, { 6, 5, 4 }, { 3, 2, 1 } };

        int rowCount = matrixA.GetLength(0);
        int columnCount = matrixA.GetLength(1);
        int[,] resultMatrix = new int[rowCount, columnCount];

        for (int i = 0; i < rowCount; i++)
        {
            for (int j = 0; j < columnCount; j++)
            {
                resultMatrix[i, j] = matrixA[i, j] + matrixB[i, j];
            }
        }

        Console.WriteLine("\nMatrix A + B:");
        PrintMatrix(resultMatrix);
    }

    static void PrintMatrix(int[,] matrix)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
     } 
}

2. Matrix Multiplication

using System;

class MatrixMultiplication
{
    static void Main()
    {
        int[,] matrixC = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
        int[,] matrixD = { { 7, 8 }, { 9, 10 } };

        int rowCountC = matrixC.GetLength(0);
        int columnCountC = matrixC.GetLength(1);
        int columnCountD = matrixD.GetLength(1);

        int[,] resultMatrix = new int[rowCountC, columnCountD];

       //Matrix Multiplication logic
        for (int i = 0; i < rowCountC; i++)
        {
            for (int j = 0; j < columnCountD; j++)
            {
                for (int k = 0; k < columnCountC; k++)
                {
                    resultMatrix[i, j] += matrixC[i, k] * matrixD[k, j];
                }
            }
        }

        Console.WriteLine("\nMatrix C * D:");
        PrintMatrix(resultMatrix);
    }

    static void PrintMatrix(int[,] matrix)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

3. Sum of Diagonals

using System;

class DiagonalSum
{
    static void Main()
    {
        int[,] matrixE = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        int PrimaryDiagonalSum = 0;
        int secondaryDiagonalSum = 0;

        for (int i = 0; i < matrixE.GetLength(0); i++)
        {
            PrimaryDiagonalSum += matrixE[i, i];
            secondaryDiagonalSum += matrixE[i, matrixE.GetLength(1) - 1 - i];
        }

        Console.WriteLine("Sum of Primary diagonal: " + PrimaryDiagonalSum );
        Console.WriteLine("Sum of secondary diagonal: " + secondaryDiagonalSum);
    }
}

4. Searching Element in Matrix

using System;

class MatrixSearchAndSecondLargest
{
    static void Main()
    {
        int[,] matrix = {
            { 3, 12, 5 },
            { 8, 6, 10 },
            { 2, 9, 7 }
        };

        Console.Write("\nEnter the element to search: ");
        int searchElement = int.Parse(Console.ReadLine());

        bool isElementFound = SearchElement(matrix, searchElement);

        if (isElementFound)
        {
            Console.WriteLine("Element " + searchElement + " is found in the matrix.");          
        }
        else
        {
            Console.WriteLine("Element " + searchElement + " not found in the matrix.");
        }
    }

    static bool SearchElement(int[,] matrix, int target)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                if (matrix[i, j] == target)
                {
                    Console.WriteLine("Element " + target + " found at position (" + (i + 1) + ", " + (j + 1) + ").");
                    return true;
                }
            }
        }
        return false;
    }
}

3. Find Second largest Element in Matrix

using System;

class MatrixSearchAndSecondLargest
{
    static void Main()
    {
        int[,] matrix = {
            { 3, 12, 5 },
            { 8, 6, 10 },
            { 2, 9, 7 }
        };

         int secondLargest = FindSecondLargest(matrix);
         Console.WriteLine("Second Largest Element: " + secondLargest);

    }

    //Find second Largest Element in matrix
    static int FindSecondLargest(int[,] matrix)
    {
        int largest = int.MinValue;
        int secondLargest = int.MinValue;

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                if (matrix[i, j] > largest)
                {
                    secondLargest = largest;
                    largest = matrix[i, j];
                }
                else if (matrix[i, j] > secondLargest && matrix[i, j] < largest)
                {
                    secondLargest = matrix[i, j];
                }
            }
        }

        return secondLargest;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *