C 语言程序,用于执行矩阵的加法和减法运算,并求迹


2022 年 3 月 1 日, 学习 eTutorial
3093

矩阵是由行和列中的数字或元素组成的集合。矩阵中的每个数字都称为矩阵元素。在这个 C 语言程序中,我们需要执行矩阵加法矩阵减法。完成加法或减法后,我们需要求出结果矩阵的迹。为了实现矩阵上的这个操作,它应该遵循条件。条件是,矩阵的阶数应该相等,这意味着行数和列数。矩阵的阶数用 'a x b' 表示,其中 'a' 是行数,'b' 是矩阵中的列数。

为了进行矩阵的加法运算,我们需要将矩阵 A 中的每个元素与矩阵 B 中对应的元素相加。即,

 "sum[i][j] = A[i][j] + B[i][j]"

为了进行矩阵的减法运算,我们需要将矩阵 A 中的每个元素与矩阵 B 中对应的元素相减。即,
 
 "diffmat[i][j] = A[i][j] - B[i][j]"。

矩阵 A 的迹是矩阵对角线元素的和。因此,通过将矩阵的对角线元素相加来计算。即,

 "Trace = A[1][1]+A[2][2]+A[3][3]....A[n][n]".

这个 C 程序的逻辑是,我们必须从用户那里读取矩阵的阶数。然后我们使用 For 循环接受两个矩阵的元素。获取矩阵的元素后,我们将其打印出来。然后显示一条消息给用户,要求输入,

  • 1 执行加法
  • 2 执行减法”。

如果用户输入 1,则我们使用嵌套的 for 循环执行矩阵加法。其中外层 for 循环用于行,内层循环用于列。在 for 循环内部,我们将矩阵 A 中的每个元素与矩阵 B 中的每个元素相加。然后,我们调用 Trace 函数来获取输出矩阵的迹。

如果用户输入 2,则我们以相同的方式执行矩阵减法,并调用 Trace 函数来找出结果矩阵的迹。Trace 函数使用 for 循环将给定矩阵的对角线元素相加,并检查条件 'i=j'。

算法

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

步骤 2:初始化和定义 C 程序中要使用的矩阵和变量。

步骤 3:声明用户定义的函数 Trace,以查找相加和相减矩阵的迹。

步骤 4:使用 C 编程中的 printfscanf 函数接受矩阵 AB 的阶数。

步骤 5:使用嵌套的 for 循环scanf 函数接受矩阵 A 的元素。

步骤 6:使用嵌套的 for 循环和 printf 函数显示矩阵 A

步骤 7:使用嵌套的 for 循环和 scanf 函数接受矩阵 B,然后使用 printf 函数打印。

步骤 8:接受用户的选择,1 表示加法,2 表示减法。

步骤 9:如果用户输入 1 执行加法,则遵循步骤 10 和步骤 11。

步骤 10:打开嵌套的 for 循环,并使用公式 sumat[i][j] = A[i][j] + B[i][j] 进行矩阵元素加法,然后使用嵌套的 for 循环打印结果矩阵。

步骤 11:调用函数 Trace 以获取结果相加矩阵的迹。

步骤 12:如果用户输入选项 2 执行减法,则遵循步骤 13 和步骤 14。

步骤 13:使用嵌套的 for 循环,通过公式 diffmat[i][j] = A[i][j] - B[i][j] 查找减法矩阵,并打印结果矩阵。

步骤 14:调用函数 Trace 以查找相减矩阵的迹。

用户定义函数

a:从调用函数中接受矩阵作为 Trace 函数的参数。

b:打开一个嵌套的 for 循环,并在循环内部使用 if 条件检查条件 (i = j)

c:如果满足条件,则将元素与迹相加,公式为 trace = trace + arr[i][j]

d:使用 C 编程语言将迹返回给被调用函数。

C 语言源代码

                                          #include <stdio.h>
#include <conio.h>

void main()
{

  int A[10][10], B[10][10], sumat[10][10], diffmat[10][10]; /* declares matrix sum and difference as integers  */
  int i, j, M, N, option;
  void trace(int arr[][10], int M, int N); /* declares function trace as void */
  clrscr();
  printf("Enter the order of the matrice A and B\n");
  scanf("%d %d", & M, & N);
  printf("Enter the elements of matrix A\n"); /* enter the order and elements of two matrices */
  for (i = 0; i < M; i++) {
    for (j = 0; j < M; i++) {
      for (j = 0; j < N; j++) {
        printf("=", A[i][j]);
      }
      printf("\n");
    }
    printf("Enter the elements of matrix B\n");
    for (i = 0; i < M; i++) {
      for (j = 0; j < M; i++) {
        for (j = 0; j < M; i++) /* using switch, switch control to addition or multiplication case */ {
          for (j = 0; j < N; j++) {
            sumat[i][j] = A[i][j] + B[i][j];
          }
        }
        printf("Sum matrix is\n");
        for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            printf("=", sumat[i][j]); /* print and calculate the matrix sum */
          }
          printf("\n");
        }
        trace(sumat, M, N); /* calling the function trace for sum output */
        break;
        case 2: for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            diffmat[i][j] = A[i][j] - B[i][j];
          }
        }
        printf("Difference matrix is\n");
        for (i = 0; i < M; i++) {
          for (j = 0; j < N; j++) {
            printf("=", diffmat[i][j]); /* calculating and printing the difference of the matrix */
          }
          printf("\n");
        }
        trace(diffmat, M, N); /* calling function trace for difference output */
        break;
      }
      getch();
    } /* End of main() */
    /*Function to find the trace of a given matrix and print it*/

    void trace(int arr[][10], int M, int N)

    {
      int i, j, trace = 0;
      for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) /* calculating trace by adding elements that match i=j as we discussed in logic of program */ {
          if (i == j) {
            trace = trace + arr[i][j];
          }
        }
      }
      printf("Trace of the resultant matrix is = %d\n", trace);
    }
                                      

输出

Enter the order of the matrix A and B
2* 2

Enter the elements of matrix A
1 1
2 2

MATRIX A is
1  1
2  2

Enter the elements of matrix B
3 3
4 4

MATRIX B is
3  3
4  4

Enter your option: 1 for Addition and 2 for Subtraction
1

Sum matrix is
4  4
6  6

Trace of the resultant matrix is = 10