Bool矩阵的操作,C语言实现

实验目的

Using C-language to achieve the Join and Meet operation of Boolean matrices.

实验内容

Input two matrices A and B, and calculate and output the meet of A and B, the join of A and B.

使用环境

Win 11 & visual studio code 2022

算法介绍

Algorithm: Define an array of variable sizes in a large range, input the number(0&1) to get the matrix. Use pointer function branches to determine the return address, calculate by retrieves the value in the address.

Input: matrices A and B

Output: the meet of A and B, the join of A and B.

Take Meet operation as an example

Begin: Compares whether the first digit of the array A is 0, if so, return the digit

Step 1: if not, comparing whether the second digit of the array B is 1. If so, return the second digit.

Step 2: if not, still return the second digit.

End: Compare them in order like this.

调试过程

调试代码

objectivec 复制代码
#include <stdio.h>

void input(int rows, int cols, int ar[][256]);
int* join(int rows, int cols, int ar1[][256], int ar2[][256]);
int* meet(int rows, int cols, int ar1[][256], int ar2[][256]);

int main(void)
{
    int M, N, i, j, * p;
    printf("please decide Rows and Columns of matrix: ");
    scanf("%d %d", &M, &N);
    int arry1[256][256];
    int arry2[256][256];
    input(M, N, arry1);
    printf("the next matrix ");
    input(M, N, arry2);
    printf("the meet of A and B:\n");
    for (i = 0; i <N; i++)
    {
        for (j = 0; j < M; j++)
        {
            p = meet(i, j, arry1, arry2);
            printf("%d ", *p);
        }
        printf("\n");
    }
    printf("\n");
    printf("the join of A and B:\n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        {
            p = join(i, j, arry1, arry2);
            printf("%d ", *p);
        }
        printf("\n");
    }
    getchar();
}

void input(int rows, int cols, int ar[][256])
{
    int i, j;
    printf("input: ");
    for (i = 0; i < rows; i++)
        for (j = 0; j < cols; j++)
        {
            scanf("%d", &ar[i][j]);
            getchar();
        }
}

int* meet(int rows, int cols, int ar1[][256], int ar2[][256])
{
    if (0 == ar1[rows][cols]) return &ar1[rows][cols];
    else
    {
        if (1 == ar2[rows][cols]) return &ar2[rows][cols];
        else return &ar2[rows][cols];
    }
}

int* join(int rows, int cols, int ar1[][256], int ar2[][256])
{
    if (1 == ar1[rows][cols]) return &ar1[rows][cols];
    else
    {
        if (0 == ar2[rows][cols]) return &ar2[rows][cols];
        else return &ar2[rows][cols];
    }
}

Another way to achieve the goal, the code removed the function of obtaining input.

objectivec 复制代码
#include <stdio.h>



void fun1(int (*P)[3]);

void meet(int (*P)[3],int (*Q)[3]);

void join(int (*P)[3],int (*Q)[3]);

void main()

{

    int arr1[3][3] = {{1, 0, 1}, {0, 1, 0}, {1, 1, 0}};

    int arr2[3][3] = {{0, 0, 1}, {1, 1, 0}, {0, 1, 1}};  //初始化数组

    fun1(arr1);

    printf("the other matrix: \n");

    fun1(arr2);

    printf("A∧B\n");

    meet(arr1,arr2);

    printf("A∨B\n");

    join(arr1,arr2);

    getchar();

}



void fun1(int (*P)[3])  //打印二维数组

{

    int i, j;

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

    {

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

        {

            printf("%2d", *(*(P + i) + j));

        }

        printf("\n");

    }

}



void meet(int (*P)[3],int (*Q)[3])

{

    int i, j;

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

    {

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

        {

            if (0 == *(*(P + i) + j)) printf("%2d", *(*(P + i) + j));

            else

            {

                if (1 == *(*(Q + i) + j)) printf("%2d", *(*(Q + i) + j));

                else printf("%2d", *(*(Q + i) + j));

            }

        }

        printf("\n");

    }

}



void join(int (*P)[3],int (*Q)[3])

{

    int i, j;

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

    {

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

        {

            if (1 == *(*(P + i) + j)) printf("%2d", *(*(P + i) + j));

            else

            {

                if (0 == *(*(Q + i) + j)) printf("%2d", *(*(Q + i) + j));

                else printf("%2d", *(*(Q + i) + j));

            }

        }

        printf("\n");

    }

}

运行结果

总结

This is just an algorithm that I came up with on a whim, and it might be more efficient to use Pointers of Pointers to traverse a two-dimensional array.

参考文献

《C prime plus》《C程序设计---谭浩强》

相关推荐
蹉跎x12 分钟前
力扣1358. 包含所有三种字符的子字符串数目
数据结构·算法·leetcode·职场和发展
雨颜纸伞(hzs)21 分钟前
C语言介绍
c语言·开发语言·软件工程
巫师不要去魔法部乱说1 小时前
PyCharm专项训练4 最小生成树算法
算法·pycharm
IT猿手2 小时前
最新高性能多目标优化算法:多目标麋鹿优化算法(MOEHO)求解GLSMOP1-GLSMOP9及工程应用---盘式制动器设计,提供完整MATLAB代码
开发语言·算法·机器学习·matlab·强化学习
阿七想学习2 小时前
数据结构《排序》
java·数据结构·学习·算法·排序算法
a0023450012 小时前
判断矩阵是否为上三角矩阵
c语言
王老师青少年编程2 小时前
gesp(二级)(12)洛谷:B3955:[GESP202403 二级] 小杨的日字矩阵
c++·算法·矩阵·gesp·csp·信奥赛
Kenneth風车2 小时前
【机器学习(九)】分类和回归任务-多层感知机(Multilayer Perceptron,MLP)算法-Sentosa_DSML社区版 (1)111
算法·机器学习·分类
eternal__day2 小时前
数据结构(哈希表(中)纯概念版)
java·数据结构·算法·哈希算法·推荐算法
APP 肖提莫3 小时前
MyBatis-Plus分页拦截器,源码的重构(重构total总数的计算逻辑)
java·前端·算法