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 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[] = 每列要搜索的值集...([col to search] = 要搜索的值)

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

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

1 回答 1

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,因为它总是返回相同的值,所以测试该值没有意义,所以返回它并让调用代码测试它没有意义。

于 2013-10-26T06:22:49.577 回答