c - 在 C 中搜索二维数组中的特定值

标签 c arrays search

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

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 = theArray 中实际填充的行数

dataCols = theArray 中实际填充的列数

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

searchVals[] = 每列要搜索的值集...( [要搜索的列] = 要搜索的值 搜索)

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

我用新的 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);
}

最佳答案

此代码是您的代码的一个小修改,主要修复搜索循环中的离一限制,并使用 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 因为它总是返回相同的值,因此测试该值没有意义,因此返回它并让调用代码测试它也没有意义.

关于c - 在 C 中搜索二维数组中的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19579883/

相关文章:

c - 如何编辑节点?以及如何删除已修改的节点?

c - 如何在 Mac OS X 下用 C 设置文件的创建日期?

c - 当我声明一个字符串时保留多少内存?

android - 如何将搜索 View 添加到我的 Android 应用程序?

Django:如何过滤并获取包含所有搜索关键字的结果

mysql - 在大型 csv 文件中搜索

Contiki OS 循环太快无法打印结果?

javascript - 使用 angularjs forEach 循环

javascript - 从复杂数组中检索对象

ruby - 如何从可变长度数组中获取可变长度数组的指定顺序子集?