C程序交换矩阵的两行和两列


2022年4月10日, Learn eTutorial
2653

什么是矩阵?如何交换矩阵的列和行?

我们想交换给定矩阵的行和列,正如我们所讨论的。矩阵是按行和列格式排列的元素集合。所以我们要做的就是交换行和列。例如,一个 **矩阵 [123]**,交换列 **2** 和 **3** 意味着结果矩阵将是 **[132]**,行也一样。

在这个C程序中,在接受用户矩阵后,我们获取需要交换的列和行。首先,我们交换行;在行交换之后,我们交换列。

pythagorean triples in a range

我们如何使用C语言编程实现这个程序逻辑?

这个C程序的逻辑是:首先声明两个矩阵“**m1**”和“**m2**”。然后读取矩阵的阶数到“**m**”和“**n**”中。然后从用户读取矩阵并将其保存到“**m1**”和“**m2**”中。读取要交换的行号到变量“**a**”和“**b**”中。然后使用for循环交换行并将其保存在矩阵**m1**中。读取要交换的列号到变量“**p**”和“**q**”中。显示以**m2**中元素表示的给定矩阵。然后使用for循环交换“**m2**”中的列并将其保存在**m2**中。最后,显示行交换后的矩阵**m1**和列交换后的矩阵**m2**。

算法

步骤1: 包含头文件,以便在C程序中使用内置函数。

步骤2: 声明矩阵变量 m1[ ][ ],m2[ ][ ] 为整型。

步骤3: 声明整型变量 i,j,m,n,a,b,c,p,q,r

步骤4: 读取矩阵的阶数到 mn 中。

步骤5:使用嵌套的for循环,条件为 ij,用于读取矩阵 m1 的元素,并将相同的值赋给矩阵 m2

步骤6读取要交换的行号到变量 a b

步骤7:使用带有条件 ifor循环,执行步骤8

步骤8:按如下方式交换行的元素

  1. c = m1[a-1][i]
  2. m1[a-1][i] = m1[b-1][i]
  3. m1[b-1][i] = c。

步骤9:读取要交换的列号到变量 pq 中。

步骤10:使用for循环显示存储在 m2 中的给定矩阵,因为 m1 包含行交换后的矩阵。

步骤11:使用带有条件 i 的 for 循环,执行步骤12

步骤12:按如下方式交换列的元素

  1. r = m2[i][p-1]
  2. m2[i][p-1] = m2[i][q-1]
  3. m2[i][q-1] = r。

步骤13:显示交换两行后的矩阵 m1

步骤14:显示交换两列后的矩阵 m2


此矩阵程序使用了C语言编程的以下概念,请参考这些主题以获得更好的理解

C 语言源代码

                                          #include <stdio.h>

void main() {
  static int m1[10][10], m2[10][10];
  int i, j, m, n, a, b, c, p, q, r;
  printf("Enter the order of the matrix\n");
  scanf("%d * %d", & m, & n);
  printf("Enter the coefficients of the matrix\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      scanf("%d", & m1[i][j]);
      m2[i][j] = m1[i][j];
    }
  }
  printf("Enter the numbers of two rows to be exchanged \n");
  scanf("%d %d", & a, & b);
  for (i = 0; i < m; ++i) {
    c = m1[a - 1][i]; /* first row has index is 0 */
    m1[a - 1][i] = m1[b - 1][i];
    m1[b - 1][i] = c;
  }
  printf("Enter the numbers of two columns to be exchanged\n");
  scanf("%d %d", & p, & q);
  printf("The given matrix is \n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j)
      printf(" %d", m2[i][j]);
    printf("\n");
  }
  for (i = 0; i < n; ++i) {
    r = m2[i][p - 1]; /* first column index is 0 */
    m2[i][p - 1] = m2[i][q - 1];
    m2[i][q - 1] = r;
  }
  printf("The matrix after interchanging the two rows(in the original matrix)\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      printf(" %d", m1[i][j]);
    }
    printf("\n");
  }
  printf("The matrix after interchanging the two columns(in the original matrix)\n");
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j)
      printf(" %d", m2[i][j]);
    printf("\n");
  }
}
                                      

输出

Enter the order of the matrix
3*3

Enter the co-efficient of the matrix
1 2 4
5 7 9
3 0 6

Enter the numbers of two rows to be exchanged
1 2

Enter the numbers of two columns to be exchanged
2 3

The given matrix is
 1 2 4
 5 7 9
 3 0 6

The matrix after interchanging the two rows (in the original matrix)
 5 7 9
 1 2 4
 3 0 6

The matrix after interchanging the two columns(in the original matrix)
 1 4 2
 5 9 7
 3 6 0