【C语言】判断布尔矩阵是否自反、非自反、对称、非对称、反对称、传递与否,并计算其互补关系和逆矩阵

  • 实验目的

Using C-language to judge whether a Boolean Matrix is reflexive, irreflexive, symmetric, asymmetric, antisymmetric, transitive, or not, and calculate the complementary relation and the inverse.

  • 实验内容

Design the algorithm to judge whether a Boolean Matrix is reflexive, irreflexive, symmetric, asymmetric, antisymmetric, transitive, or not, and calculate the complementary relation and the inverse, and output the result.

  • 使用环境

Win 11 & visual studio code 2022

  • 算法介绍

Algorithm: using alternative branch to judge,

Input: A square matrix about relations

Output: the properties of the matrix and the complement and inverse of the matrix

Begin(inverse)

Step 1: loop through a matrix

Step 2: change the entries of 1 to 0, and 0 to 1.

End: Output the matrix

Begin(complement)

Step 1: loop through the upper triangle of matrix(half a matrix)

Step 2: make a[i][j] = a[j][i] and a[j][i] = a[i][j]

End: Output the matrix

  • 调试过程

  1. 调试代码

#include<stdio.h>

int reflex(int arr[10][10],int r);

void symme(int arr[10][10],int r);

void inver(int a[10][10]);

void comp(int a[10][10]);

void main()

{

    int i ,j,R,array1[10][10];

    printf("input the rank of matrix: ");

    scanf("%d",&R);

    printf("input the matrix: ");

    for (i = 0; i < R; i++)

        for (j = 0; j < R; j++)

        {

            scanf("%d", &array1[i][j]);

        }

    symme(array1,R);  //函数中调用了reflex函数

    comp(array1);

    inver(array1);   

}



int reflex(int arr[10][10],int r)  //判断反身性

{

    int i,s=0;

    for (i = 0; i < r; i++)

        if (arr[i][i] == 1) s++;   

    if (s==r) printf("the matrix is reflexive\n");

    else

    {

        if (s==0) printf("the matrix is irreflexive\n");

    }

    return s;

}

void symme(int arr[10][10],int r)  //判断对称性

{

    int i,j,s=0;

    for (i=0;i<r;i++)

        for (j=0;j<r;j++)

            if (arr[i][j]==arr[j][i]) s++;

    if (s == (r*r))

    {

        reflex(arr,r);

        printf("the matrix is symmetric\n");

    }

    else

    {

        if (0 == reflex(arr,r) && s==r) printf("the matrix is asymmetric\n");

        else printf("the matrix is not asymmetric\n");

        printf("the matrix is not symmetric\n");

    }

    if (s==r)  printf("the matrix is not antisymmetric\n");

    else printf("the matrix is not antisymmetric\n");

}

void inver(int a[10][10])  //输出矩阵的逆

{

int b[10][10], i, j, n = 0;

for (i = 0; i < 4; i++)

          for (j = 0; j < 4; j++)

                  b[j][i] = a[i][j];

printf("The inverse of the A matrix is\n");

for (i = 0; i < 4; i++)

{



          for (j = 0; j < 4; j++)

          {

                  printf("%d ", b[i][j]);

                  n++;

                  if (n % 4 == 0)

                  {

                          printf("\n");

                  }

          }

}

}

void comp(int a[10][10])  //输出补关系

{



int i, j;

printf("The complement of the A matrix is\n");

for (i = 0; i < 4; i++)

{

          for (j = 0; j < 4; j++)

          {

                  if (a[i][j] == 1)

                          printf("0 ");

                  if (a[i][j] == 0)

                          printf("1 ");

          }

          printf("\n");

}

}
  1. 运行结果

  • 总结

Through the method of writing programs, the distinction between the properties of relation matrix is more understood and clarified. Writing each algorithm into a function can make the program clearer.

  • 参考文献

[1]谭浩强,C 程序设计[M] (第四版).北京:清华大学出版社,2010年6月(中国高等院校计算机基础教育课程体系规划教材)

[2]谭浩强, C 程序设计( 第四版 )学习辅导 ,北京:清华大学出版社,2010年7月(中国高等院校计算机基础教育课程体系规划教材)

[3]C Primer Plus (第6版)中文版,Stephen Prata 著;姜佑译 ——北京 :人名邮电出版社,2019.11

  • 28
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于闭包的问题,我可以帮你解答。以下是C语言代码实现: ```c #include <stdio.h> #define MAX_SIZE 100 void reflexive_closure(int matrix[MAX_SIZE][MAX_SIZE], int size) { // 计算自反闭包 for (int i = 0; i < size; i++) { matrix[i][i] = 1; } } void symmetric_closure(int matrix[MAX_SIZE][MAX_SIZE], int size) { // 计算对称闭包 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (matrix[i][j] == 1) { matrix[j][i] = 1; } } } } void transitive_closure(int matrix[MAX_SIZE][MAX_SIZE], int size) { // 计算传递闭包 for (int k = 0; k < size; k++) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (matrix[i][k] == 1 && matrix[k][j] == 1) { matrix[i][j] = 1; } } } } } void print_matrix(int matrix[MAX_SIZE][MAX_SIZE], int size) { // 打印矩阵 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int matrix[MAX_SIZE][MAX_SIZE]; int size; // 输入矩阵 printf("请输入关系的大小:"); scanf("%d", &size); printf("请输入关系矩阵:\n"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { scanf("%d", &matrix[i][j]); } } // 计算闭包 reflexive_closure(matrix, size); symmetric_closure(matrix, size); transitive_closure(matrix, size); // 打印闭包矩阵 printf("\n自反闭包、对称闭包、传递闭包:\n"); print_matrix(matrix, size); return 0; } ``` 在上面的代码中,我们定义了三个函数:reflexive_closure() 、symmetric_closure() 和 transitive_closure() ,分别用于计算自反闭包、对称闭包和传递闭包。由于这三种闭包是可以交换顺序的,因此我们可以按任意顺序调用这三个函数。 在主函数中,我们首先输入关系矩阵,然后计算出三种闭包,最后打印出闭包矩阵

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值