HomeCSharpC# Program to Interchange the Rows of a Matrix

C# Program to Interchange the Rows of a Matrix

This C# program is designed to interchange or swap the rows of a matrix. A matrix is a two-dimensional array of numbers organized in rows and columns. Interchanging rows can be useful in various applications, such as matrix transformations, data manipulation, and sorting algorithms.

Problem Statement

Write a C# program to interchange the positions of two rows in a given matrix.

C# Program to Interchange the Rows of a Matrix

using System;

class MatrixRowInterchange
{
    static void Main()
    {
        Console.WriteLine("Enter the number of rows in the matrix:");
        int numRows = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter the number of columns in the matrix:");
        int numCols = int.Parse(Console.ReadLine());

        int[,] matrix = new int[numRows, numCols];

        // Input the matrix elements
        Console.WriteLine("Enter the matrix elements:");

        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < numCols; j++)
            {
                Console.Write($"Enter element at position ({i + 1},{j + 1}): ");
                matrix[i, j] = int.Parse(Console.ReadLine());
            }
        }

        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix);

        // Interchange rows logic
        Console.WriteLine("Enter the indices of the rows you want to interchange (0-based indexing):");
        int row1 = int.Parse(Console.ReadLine());
        int row2 = int.Parse(Console.ReadLine());

        if (row1 >= 0 && row1 < numRows && row2 >= 0 && row2 < numRows)
        {
            // Swap the rows
            for (int j = 0; j < numCols; j++)
            {
                int temp = matrix[row1, j];
                matrix[row1, j] = matrix[row2, j];
                matrix[row2, j] = temp;
            }

            Console.WriteLine("Matrix after interchanging rows:");
            PrintMatrix(matrix);
        }
        else
        {
            Console.WriteLine("Invalid row indices. Please enter valid indices.");
        }
    }

    static void PrintMatrix(int[,] matrix)
    {
        int numRows = matrix.GetLength(0);
        int numCols = matrix.GetLength(1);

        for (int i = 0; i < numRows; i++)
        {
            for (int j = 0; j < numCols; j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

How it Works

  1. Input Collection:
    • The program begins by prompting the user to input the number of rows and columns for the matrix.
    • It then initializes a 2D integer array (matrix) based on the user-provided dimensions.
    • The user is asked to input the elements of the matrix row by row.
  2. Original Matrix Display:
    • After collecting the matrix elements, the program displays the original matrix to the user using the PrintMatrix function.
  3. Row Interchange Input:
    • The user is prompted to enter the indices (0-based) of the two rows they want to interchange. For example, if the user enters 0 2, it means they want to interchange the first row (index 0) with the third row (index 2).
  4. Row Interchange Logic:
    • The program checks if the entered row indices are within the valid range for the matrix (i.e., between 0 and the number of rows ‚Äď 1).
    • If the indices are valid, the program proceeds to swap the rows:
      • It uses a loop to iterate through the columns of the specified rows.
      • For each column, it temporarily stores the value from one row, replaces it with the corresponding value from the other row, and then assigns the temporary value to the other row.
      • This swapping process is done for all columns, effectively interchanging the two rows in the matrix.
  5. Result Display:
    • After interchanging the rows (if the indices were valid), the program displays the modified matrix, showing the result of the row interchange.
  6. Error Handling:
    • If the user provides invalid row indices (e.g., indices that are out of bounds), the program displays an error message and does not perform the interchange operation.
  7. Matrix Printing Function:
    • The PrintMatrix function is used to display the matrix in a neatly formatted manner.

Input/ Output

Share:

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...