查找模式的 C 函数始终返回 "no mode"

标签 c arrays function visual-studio-2012 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

的值

如您在 the answer to this question 中所读,c 中的赋值运算符返回左操作数的值。在您的情况下,它将返回一个正数,该数将被转换为 bool 值并每次都被评估为 true。

关于查找模式的 C 函数始终返回 "no mode",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35513504/

相关文章:

java - 数组的 boolean 方法,编码问题

c - PAPI(性能应用程序编程)无法正常工作?

采用 void * 函数参数的 C 函数

javascript - 在表中显示数组中的日期

javascript - d3 Enter().append() 不附加数组的所有元素

javascript - 带有变量的viewer.loadImageFromJSON 不起作用

c - 获取关系运算符和按位右移作为命令行参数

c - 具有多个语句的三元运算符

arrays - Shell Bash - 将多行字符串拆分为数组

php - 动态获取/设置多维数组中的值的函数