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

时间:2013-10-25 01:58:49

标签: c arrays search

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

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

1 个答案:

答案 0 :(得分:0)

此代码是您的一个小修改,主要修复搜索循环中的逐个限制,并在找到匹配时使用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,因为它总是返回相同的值,所以测试值没有意义,所以没有必要返回它并让调用代码测试它。