0

課題の最後の部分を終わらせるために助けが必要です。2D 配列の特定の列で、別の配列から渡された特定の値を検索する必要があります。最初の値を見つけたら、それを 3 番目の配列に保存し、次の列で新しい値を検索し続けます。

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] = 行 )

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

関数へのインターフェイスは目を見張るものがあります。1 つの出力パラメーターがあり、それを最後にする必要があります — 「入力パラメーター、次に出力パラメーター」という規則を使用します。戻り値もまったく無意味です。私はそれを残しましたが、関数はvoid常に同じ値を返すため、値をテストしても意味がないため、値を返して呼び出し元のコードにテストさせる意味がありません。

于 2013-10-26T06:22:49.577 に答える