FOR FREE YEAR SOLVED

Interchanging Row and Column in a Matrix

Back to Programming

Description

A matrix contains one or more rows and columns. Here the program is written to exchange one row with another and similarly one column with another. The row numbers or the column numbers to be exchanged is given by the programmer/ user. 

Normally for exchanging two elements a and b where the value of a=5 and b=6 we use the following steps:

t=a [t=5]

a=b [a=6]

b=t [b=5]

 

Where ‘t’ is a temporary variable to store the value temporarily for exchanging. This simple technique is used here to exchange the elements of two rows or two columns.

After exchanging the value of a=6 and b=5. The values are interchanged.

 

For example:

This is a 3×3 matrix i.e. the matrix contains 3 rows and 3 columns.

Now, suppose we want to exchange row 1 with row 3:

 

For this first the first element (here, 10) of the 1st row is stored in the temporary variable. Then first element (70) of the 2nd row (with which the row is to be exchanged) is stored in the position of ‘10’ making the matrix:

 

Now, the value of the temporary element will be stored at the position of ‘70’ (the previous position). Hence the matrix will be:

 

That means the first elements are exchanged with each other. Similarly, the other elements of the rows (20 & 80, 30 & 90) will also be exchanged and will get the new matrix as:

 

The same process will be followed to exchange the two columns also.

For example:

 

For matrix A, if we want to exchange the column 1 with column 2, the same process will be executed and will get the matrix as:

Algorithm

INPUT: A matrix and the row numbers and column numbers to be exchanged.
OUTPUT: The matrices after exchanging the rows and columns.

PROCESS:
Step 1: [taking the inputs]
	Read m, n
	For i=0 to m-1 repeat
		For j=0 to n-1 repeat
			Read mat[i][j]
			Set mat1[i][j]<-mat[i][j]
		[End of ‘for’ loop]
	[End of ‘for’ loop]
	Read r1, r2 [rows to be exchanged]
	Read c1, c2 [columns to be exchanged]
Step 2: [Exchanging rows and columns]
	    [Exchanging the rows r1 and r2]
	    for i = 0 to m-1 repeat
        		Set c <-mat[r1 - 1][i]
       	 	Set mat[r1 - 1][i] <- mat[r2 - 1][i]
        		Set mat[r2 - 1][i] <- c
    	    [End of ‘for’ loop]
    	  for i = 0 to m-1 repeat
        		for j = 0 to n-1 repeat
            			print  mat[i][j]
        		[End of ‘for’ loop]
        		Move to the next line
     	  [End of ‘for’ loop]
	  [Exchanging the rows r1 and r2]
    	  for i = 0 to n-1 repeat
        		Set r <- mat1[i][c1 - 1]
        		Set mat1[i][c1 - 1] <- mat1[i][c2 - 1]
        		Set mat1[i][c2 - 1] <- r
     	   [End of ‘for’ loop]
    	  for i = 0 to m-1 repeat
        		for j = 0 to n-1 repeat
            			print mat1[i][j]
		[End of ‘for’ loop]
        		Move to the next line
    	   [End of ‘for’ loop]
Step 3: Stop.

Code

TIME COMPLEXITY:

    for (i = 0; i < m; ++i)----------------- O(m)

    {   c =mat[r1 - 1][i];

        mat[r1 - 1][i] = mat[r2 - 1][i];

        mat[r2 - 1][i] = c;

    }

    for (i = 0; i < n; ++i)------------------ O(n)

    {   r = mat1[i][c1 - 1];

        mat1[i][c1 - 1] = mat1[i][c2 - 1];

        mat1[i][c2 - 1] = r;

     }

    The time complexity is O(m+n). if the number of rows and columns are same then the time complexity is O(n).