简体   繁体   English

C查找模式的功能始终返回“无模式”

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

For school I have to create a function that prints the mode of a sorted array. 对于学校,我必须创建一个打印排序数组模式的函数。 It has to account for there being multiple modes and there being no modes as well. 它必须考虑存在多种模式,也没有模式。 For some reason even if there is a mode in the array the function always prints "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("}");
        }
    }

}

I have no clue how close or far away I am from the correct logic in this, so if I'm way way off and need to start from scratch let me know. 我不知道我与正确的逻辑有多接近或相距遥远,因此,如果我有一段距离,需要从头开始,请告诉我。

Thanks! 谢谢!

Your problem is your comparison if (mode[0] = (a[size - 1] + 1)) 您的问题是您的比较if (mode[0] = (a[size - 1] + 1))

The single = is doing assignment not equivalence testing. 单个=正在执行分配不等效测试。 You assign mode[0] to the value of a[size -1] + 1 您将mode[0]分配给a[size -1] + 1

The assignment operator in c returns the value of the left operand as you can read in the answer to this question . 在c中的赋值运算符返回左操作数的值,您可以在此问题的答案中看到。 In your case it will return a positive number which will be cast to a boolean and be evaluated as true every time. 在您的情况下,它将返回一个正数,该正数将转换为布尔值,并且每次都将其评估为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM