【问题标题】:Searching 2D array for specific value in C在 C 中搜索二维数组以获取特定值
【发布时间】:2013-10-25 01:58:49
【问题描述】:

我需要一些帮助来完成作业的最后一部分。我需要在二维数组的特定列中搜索另一个数组传递的特定值。找到第一个匹配项后,将其保存到第三个数组中,然后继续在下一列中搜索新值。

myTEST mySearchArrayIntAll(const int theArray[][aTotItems2], int dataRows, int dataCols, int searchCols[], const int searchVals[])
{
    myTEST fncFlag = bPASS;
    int colCount=0;
    int rowCount=0;
    int index=0;
    int empty=-1;
    int searchVal=0;

        do
        {
            searchVal = searchVals[index];
            for(rowCount=0; rowCount<dataRows-1; rowCount++)
            {

            }

        } while (true);

        return(fncFlag);
}

theArray = 要搜索的值

dataRows = 数组中实际填充的行数

dataCols = 数组中实际填充的列数

searchCols[] = 设置为在列中首次找到值的位置显示列和行([col] = row)

searchVals[] = 每列要搜索的值集...([col to search] = value to 搜索)

我只是不确定如何构造代码以每次为每一列执行此操作,我只是有点筋疲力尽......这只是我需要包装到我的代码中的最后一个函数......

我用新的 for 循环更新了我的代码,我认为这可能就是我想要的......让我知道你在做什么......

myTEST mySearchArrayIntAll(const int theArray[][aTotItems2], int dataRows, int dataCols, int searchCols[], const int searchVals[])
{
    myTEST fncFlag = bPASS;
    int colCount=0;
    int rowCount=0;
    int searchVal=0;

        for(colCount=0; colCount<dataCols-1; colCount++)
            {
                searchVal=searchVals[colCount];

                for(rowCount=0; rowCount<dataRows-1; rowCount++)
                {
                    if(searchCols[colCount] == searchVal)
                        rowCount=dataRows;

                    if(theArray[rowCount][colCount] == searchVal)
                        searchCols[colCount] = rowCount;

                }

            }


    return(fncFlag);
}

3.0

myTEST mySearchArrayIntAll(const int theArray[][aTotItems2], int dataRows, int dataCols, int searchCols[], const int searchVals[])
{
    myTEST fncFlag = bPASS;
    int colCount=0;
    int rowCount=0;
    int searchVal=0;
    int empty=-1;       //searchCols is initalized to -1

        for(colCount=0; colCount<dataCols-1; colCount++)
            {
                searchVal=searchVals[colCount];

                for(rowCount=0; rowCount<dataRows-1; rowCount++)
                {
                    if(searchCols[colCount] != empty)
                        rowCount=dataRows;

                    if(theArray[rowCount][colCount] == searchVal)
                        searchCols[colCount] = rowCount;

                }

            }

    return(fncFlag);
}

【问题讨论】:

  • 欢迎来到 StackOverflow!感谢您发布您的代码,但请在您的问题中添加更多描述:您遇到什么问题,您期望的结果是什么,到目前为止what have you tried?通过question checklist 将帮助我们更好地回答您的问题。谢谢!
  • 很明显,您需要一个for 循环来依次搜索每一列,并需要另一个嵌套的for 循环来向下扫描行。剩下的就是一个简单的数据混洗问题,确保您测试正确的值,并将它们保存在正确的位置。一般来说,假设do ... while (...); 表示您做错了。这不是一个绝对规则,但 95% 的时间(我很想说 99%,但这可能夸大了),顶级检查循环(whilefor)是正确的。规范是不完整的:它没有涵盖如果找不到值该怎么办,也没有返回值。
  • 好的,sry 正在吃东西...是的,我开始了 2 个 for 循环,然后有点被某事困扰...我知道我会怎么做,但我想知道的是如何一旦找到值,我就可以退出 for 循环......说它们是一列中的 85 行,我在第 8 行找到我需要的值......我不需要为那个特定的行进一步......我可以在内部 for 循环中嵌套一个 if 语句来检查是否找到了数字吗?
  • break 语句是提前退出循环的显而易见的标准方法。
  • 好的;如果您不允许使用break,那么您可能也不允许使用gotocontinue。因此,在这种情况下,您可能会在进入循环之前将标志设置为 true,并将 &amp;&amp; flag 添加到循环条件中,并在找到值时将标志设置为 false。并且不幸被要求用 C 编写 Pascal。您的建议 — 在循环内嵌套 if — 也可以,但您真的希望循环停止而不是跳过正文。

标签: c arrays search


【解决方案1】:

此代码是对您的代码的小修改,主要修复了搜索循环中的逐一限制,并使用break 的替代方法在找到匹配项时终止内部循环。

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>

typedef bool myTEST;

enum { aTotItems2 = 50 };
enum { bPASS = true };

extern myTEST mySearchArrayIntAll(const int theArray[][aTotItems2], int dataRows,
                           int dataCols, int searchCols[], const int searchVals[]);
myTEST mySearchArrayIntAll(const int theArray[][aTotItems2], int dataRows,
                           int dataCols, int searchCols[], const int searchVals[])
{
    myTEST fncFlag = bPASS;

    for (int colCount = 0; colCount < dataCols; colCount++)
    {
        for (int rowCount = 0; rowCount < dataRows; rowCount++)
        {
            if (theArray[rowCount][colCount] == searchVals[colCount])
            {
                searchCols[colCount] = rowCount;
                rowCount = dataRows;
            }
        }
    }

    return(fncFlag);
}

int main(void)
{
    int const data[][50] =
    {
        { 9, 9, 9, 9, 9, },
        { 8, 8, 8, 8, 8, },
        { 7, 7, 7, 7, 7, },
        { 6, 6, 6, 6, 6, },
        { 5, 5, 5, 5, 5, },
        { 4, 4, 4, 4, 4, },
    };
    int rows = 6;
    int cols = 5;
    int search[] = { 6, 8, 7, 4, 9, };
    int found[5] = { -1, -1, -1, -1, -1, };
    int wanted[5] = { 3, 1, 2, 5, 0 };
    (void)mySearchArrayIntAll(data, rows, cols, found, search);
    int fail = 0;
    for (int i = 0; i < 5; i++)
    {
        const char *pass_fail = "PASS";
        if (found[i] != wanted[i])
        {
            pass_fail = "FAIL";
            fail++;
        }
        printf("%d: found %d, wanted %d (%s)\n", i, found[i], wanted[i],
                pass_fail);
    }
    if (fail == 0)
        printf("== PASS ==\n");
    else
        printf("!! FAIL !! (%d tests failed)\n", fail);
    return (fail == 0) ? 0 : 1;
}

测试结果:

0: found 3, wanted 3 (PASS)
1: found 1, wanted 1 (PASS)
2: found 2, wanted 2 (PASS)
3: found 5, wanted 5 (PASS)
4: found 0, wanted 0 (PASS)
== PASS ==

函数的界面是鸡眼的;有一个输出参数,它应该是最后一个 - 使用约定“输入参数,然后输出参数”。返回值也完全没有意义。我留下了,但是函数应该返回void,因为它总是返回相同的值,所以测试这个值没有意义,所以返回它并让调用代码测试它没有意义。

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2022-12-13
    • 2014-06-04
    • 1970-01-01
    相关资源
    最近更新 更多