【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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 07:56:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 07:56:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 07:56:06       87 阅读
  4. Python语言-面向对象

    2024-04-24 07:56:06       96 阅读

热门阅读

  1. MySQL常用函数介绍

    2024-04-24 07:56:06       36 阅读
  2. Qt : 实现串口的同步和异步读写消息

    2024-04-24 07:56:06       42 阅读
  3. npm详解

    2024-04-24 07:56:06       37 阅读
  4. R语言 数据的整理与清洗(第二篇)

    2024-04-24 07:56:06       33 阅读
  5. OceanBase OLAP collation utf8mb4_bin 优先

    2024-04-24 07:56:06       33 阅读
  6. 【Mysql】Mysql8日常优化经验分享

    2024-04-24 07:56:06       30 阅读