C 在两个数组之间查找相等的行和列

C 在两个数组之间查找相等的行和列,c,arrays,matrix,C,Arrays,Matrix,我试着用C写一个函数,得到两个二维数组,然后它检查数组2中的每一行是否有一个相等的列。 我需要在每次有匹配项时打印,以及数组1中与数组2中的匹配项最多的行号 例如,它必须使用简单数组arr[][m]来完成。 我的代码不起作用: void main() { int i, j,k, flag = 0,count = 0,max_r,max = 0; int arr[M][M] = {1,2,3,4,5,6,7,8,9}, arr1[M][M]={4,4,6,5,5,4,8,6,9};

我试着用C写一个函数,得到两个二维数组,然后它检查数组2中的每一行是否有一个相等的列。 我需要在每次有匹配项时打印,以及数组1中与数组2中的匹配项最多的行号

例如,它必须使用简单数组arr[][m]来完成。 我的代码不起作用:

void main()
{
    int i, j,k, flag = 0,count = 0,max_r,max = 0;
    int arr[M][M] = {1,2,3,4,5,6,7,8,9}, arr1[M][M]={4,4,6,5,5,4,8,6,9};
    i = 0;
    while (i < M)
    {
        j = 0;
        k = 0;
        while (j < M)
        {
            if (arr[i][j] == arr1[j][k])
                flag = 1;
            else
                flag = 0;
            j++;                
            if (j == M)
            {
                if (flag == 1)
                {
                    printf("row %d in arr eqauls to calm %d in arr1\n", i+1, k+1);
                    count++;
                }
                k++;
                k = 0;
            }
        }
        if (count > max)
        {   
            max = count;
            max_r = i;
        }
        count = 0;
        i++;
    }
    printf("the max row is:%d\n", max_r+1);
    getche();
}

下面是一个工作版本。在我们开始之前,有一些提示:

不要使用:

int i,j;
它是合法的,但不如:

int i;
int j;
使用更具描述性的名称:而不是使用稍长的名称,例如arridx,它是:arr的索引变量的缩写。或colno列编号而不是k。这可能看起来很琐碎,但它确实可以帮助你澄清一些事情。我知道很多课程/书籍都是这么教的,但在我看来,这会导致更多的悲伤

使用索引变量时用于迭代循环。它简化了事情

正确/干净的缩进允许您更快地查看逻辑错误

用空行垂直分隔代码部分

自由地使用评论。好的评论应该显示意图或内容。代码是如何实现的。注释应该具有足够的可读性,以便外行人员能够了解正在发生的事情,即使他们无法阅读代码本身。例如:

// initialize the defense subsystem before use
flak_init();
这是工作版本。我为大量的免费风格清理道歉,但这是[对我来说]从代码中获得工作版本的唯一方法。错误太多,无法用文字或暗示来解释。此版本更简单,并修复了错误。与您的版本进行比较

#include <stdio.h>

#define WIDTH       9                   // number of elements in a line

// this was "arr"
#define ALINE       1                   // number of lines
int alist[ALINE][WIDTH] = {
    { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
    // add new lines here (and adjust ALINE)
};

// this was "arr1"
#define BLINE       1                   // number of lines
int blist[BLINE][WIDTH] = {
    { 4, 4, 6, 5, 5, 4, 8, 6, 9 }
    // add new lines here (and adjust BLINE)
};

int
main()
{
    int aline;
    int bline;
    int colno;
    int count;
    int max_aline = 0;
    int max_count = 0;

    // loop through all lines of alist
    for (aline = 0;  aline < ALINE;  ++aline) {
        // loop through all lines of blist
        for (bline = 0;  bline < BLINE;  ++bline) {
            count = 0;

            // loop through all columns of each line
            // if a given column should match, print a message and
            // increase the count of the number of column matches in the
            // current line
            for (colno = 0;  colno < WIDTH;  ++colno) {
                if (alist[aline][colno] == blist[bline][colno]) {
                    printf("row %d in alist equals column %d in blist\n",
                        aline + 1,colno + 1);
                    count++;
                }
            }

            // record a new maximum and remember the line number of alist
            if (count > max_count) {
                max_count = count;
                max_aline = aline;
            }
        }
    }

    printf("the max row is:%d\n",max_aline + 1);
    getchar();

    return 0;
}

请解释你的程序应该做什么,以及它正在做什么。看起来你的数组声明有一些问题。。。每当数组2中的列与数组1中的行相等时,程序都需要打印。最后,打印数组1中与数组2中的列最匹配的行数。欢迎使用堆栈溢出。请尽快阅读这一页。您的代码几乎是一个MCVE,但是有一些细节,比如enum{M=3};或者你用定义M3缺失。此外,还缺少一个解释,说明出了什么问题,以及你是如何遇到问题的。你的while循环应该是for循环;将循环控件保持在循环的顶部会更干净。您可能应该在内部循环之前将flag设置为1,如果存在不匹配,则将其设置为0,然后中断循环。