Easy Tutorial
❮ Zepto Js Source Analysis Android Tutorial Seekbar ❯

Matrix Row Swapping in C

Category Programming Techniques

Description

Given a 5x5 matrix (in mathematics, an r×c matrix is a rectangular array of elements arranged in r rows and c columns), swap the nth and mth rows and output the result after the swap.

Input consists of 6 lines in total, with the first 5 lines being the elements of each row of the matrix, separated by a space.

The 6th line contains two integers m and n, separated by a space. (1 <= m, n <= 5) Output the matrix after the swap, with each row of the matrix occupying one line, and the elements separated by a space. Sample input:

1 2 2 1 2
5 6 7 8 3
9 3 0 5 3
7 2 1 4 6
3 0 8 2 4
1 5

Sample output:

3 0 8 2 4
5 6 7 8 3
9 3 0 5 3
7 2 1 4 6
1 2 2 1 2

Example

#include <iostream>
using namespace std;
int main()
{
    int a[5][5], m, n, i, j;
    for (i=0; i&lt;5; i++)
        for (j=0; j&lt;5; j++)
            cin >> a[i][j];
    cin >> m >> n;
    for (j=0; j&lt;5; j++){
        i = a[m-1][j];
        a[m-1][j] = a[n-1][j];
        a[n-1][j] = i;
    }
    for (i=0; i&lt;5; i++){
        for (j=0; j&lt;5; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

>

Original article: http://noi.openjudge.cn/ch0108/01/

** Click here to share your notes

Cancel

-

-

-

❮ Zepto Js Source Analysis Android Tutorial Seekbar ❯