实验目的
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
调试过程
调试代码
#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");
}
}
运行结果
总结
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