【问题标题】:Finding equal row and column between 2 arrays在 2 个数组之间找到相等的行和列
【发布时间】:2015-11-17 01:03:02
【问题描述】:

我正在尝试在 C 中编写获取两个二维数组的函数,然后它检查数组 2 中对于数组 1 中的每一行是否有相等的列。 每次有匹配时我都需要打印,以及数组 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();
}

【问题讨论】:

  • 请说明您的程序应该做什么,以及它正在做什么。
  • 看来你的数组声明有问题...cboard.cprogramming.com/c-programming/…
  • 程序需要在每次数组2中的列等于数组1中的行时打印。最后打印数组1中与数组2中的列匹配最多的行数。跨度>
  • 欢迎来到 Stack Overflow。请尽快阅读About 页面。您的代码几乎是 MCVE (How to create a Minimal, Complete, and Verifiable Example?),但缺少诸如 enum { M = 3 };(或者您是否使用 #define M 3)之类的详细信息。还缺少对出了什么问题以及您如何遇到问题的解释。你的while 循环应该是for 循环;将循环控件保持在循环的顶部会更干净。您可能应该在内部循环之前将flag 设置为 1,如果不匹配则将其设置为 0,并在循环之前将其设置为 break

标签: c arrays matrix


【解决方案1】:

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

不要使用:

int i,j;

这是合法的,但不如:

int i;
int j;

使用更多描述性名称:使用稍长的名称代替i(例如arridx,它是arr的索引变量的缩写)。或colno(列号)而不是k。这可能看起来微不足道,但它确实可以帮助您澄清事情。我知道这是许多课程/书籍所教的,但是,IMO,它会导致更多的悲伤

在使用索引变量时使用for 循环进行迭代。它简化了事情。

正确/干净的缩进让您可以更快地查看逻辑错误。

用空行垂直分隔代码段。

大量使用 cmets。好的 cmets 应该显示 intent 或“是什么”。代码是“如何”。 cmets 应该具有足够的可读性,即使外行人无法阅读代码本身,也可以大致了解正在发生的事情。例如:

// 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 2020-04-30
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2018-06-29
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多