簡體   English   中英

C查找模式的功能始終返回“無模式”

[英]C Function to find mode always returns “no mode”

對於學校,我必須創建一個打印排序數組模式的函數。 它必須考慮存在多種模式,也沒有模式。 由於某種原因,即使數組中存在模式,該函數也始終會打印“ no mode”

void findMode(double *mode, double a[], unsigned int size)
{
    double number = a[0]; //Used to to compare values in the array to see if they're similar
    int count = 1; //Keeps track of number of occurences for each number
    int max = 1; //Keeps track of max number of occurences found for each number
    int uniqueNum = 1; //Keeps track of how many unique values in the array
    int maxCount = 1; //Counts how many set's of numbers occur the max ammount of times
    int elementNum = 0; //Keeps track of element number in mode array

    for (unsigned i = 1; i < size; ++i)//loop to determine how many modes and unique numbers there are
    {
        if (number == a[i])
        {
            ++count; //if the numbers the two numbers compared are the same, count is increased by 1
        }
        else
        {
            if (count == max)
            {
                ++maxCount; //Keeps track of how many modes are in the array
            }
            if (count > max)
            {
                //If the two numbers compared are not the same and the count of the previous "set" of similar numbers is higher than the current max count, max is equal to the new count
                max = count;
                maxCount = 1; //Reset the max count if a new max is found
            }
            //Count is set back to 1 as we are counting a different number
            count = 1;
            number = a[i];
            ++uniqueNum; //Unique number variable gets incremented
        }
    }

    printf("\n%d, %d, %d", max, maxCount, uniqueNum);
    count = 1; //sets count back to 1 for next loop

    if ((double)size / max == uniqueNum)
    {
        mode = malloc(1);
        mode[0] = (a[size - 1] + 1); //Returns a value that can't exist in the array there is no mode
    }
    else
    {
        mode = malloc(sizeof(double) * maxCount); //makes the mode array the right size to store all the modes
        for (unsigned i = 1; i < size; ++i)//loop to determine what the modes are
        {
            if (number == a[i])
            {
                ++count; //if the numbers the two numbers compared are the same, count is increased by 1
            }
            else
            {
                if (count == max)
                {
                    mode[elementNum] = a[i];
                    ++elementNum;
                }
                //Count is set back to 1 as we are counting a different number
                count = 1;
            }
        }

        if (mode[0] = (a[size - 1] + 1))
        {
            printf("\nMode: no mode");
        }
        else
        {
            printf("\nMode: {");
            for (int i = 0; i <= (sizeof(mode) / sizeof(mode[0])); ++i)
            {
                printf(" %.3lf ", mode[i]);
            }
            printf("}");
        }
    }

}

我不知道我與正確的邏輯有多接近或相距遙遠,因此,如果我有一段距離,需要從頭開始,請告訴我。

謝謝!

您的問題是您的比較if (mode[0] = (a[size - 1] + 1))

單個=正在執行分配不等效測試。 您將mode[0]分配給a[size -1] + 1

在c中的賦值運算符返回左操作數的值,您可以在此問題的答案中看到。 在您的情況下,它將返回一個正數,該正數將轉換為布爾值,並且每次都將其評估為true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM