C函数用于查找阵列中的模式打印出一个满0的数组

时间:2016-02-20 01:42:12

标签: c function visual-studio-2012

我正在尝试创建一个返回已排序数组模式的函数,它只是无法正常工作。当没有模式时它可以完美地运行,但是只要有模式我就可以了:

模式:< 0.000,0.000,0.000,0.000,0.000< p>

这是函数

void findMode(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
        }
    }

    count = 1; //sets count back to 1 for next loop

    if ((double)size / max != uniqueNum)
    {
        double mode[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;
            }
        }
        printf("\nMode: {");
        for (int i = 0; i <= (sizeof(mode) / sizeof(mode[0])); ++i)
        {
            printf(" %.3lf ", &mode[i]);
        }
        printf("}");
    }
    else
    {
        printf("\nNo mode");
    }
}

据我所知,整个功能可能是垃圾,我需要重新开始,否则可能只是一个小错误。

1 个答案:

答案 0 :(得分:1)

根据我的理解,模式值是出现次数最多的一组数字中的值。您的代码存在一些小问题,导致其无法按预期运行。

首先如评论中所述,模式数组总是定义为8,因为sizeof((double)maxCount)在大多数系统上总是评估为8字节(取决于平台)。它应该是:

double modes[maxCount];

注意:为避免编译警告和错误,如果您使用的是gcc,则可能需要使用-std = c99进行编译。

在第二个for循环之前,您忘记将变量编号重新分配给数组“a”的第一个元素。

因此,第二个for循环仅将变量“number”(由最后一个for循环的结果赋予“a”的值)与“a”中的每个其他数字进行比较,因此它不是实际上找到了模式。

现在在你的第二个for循环中,当你找到这个数字时,你忘了将变量“number”刷新到数组“a”中的非等元素!= a [i]。

基本上,函数的for循环应该是这样的:

for (unsigned int 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)
        {
            printf("inside. a = %.3lf\n", a[i]);
            ++maxCount; //Keeps track of how many modes are in the array
        }
        if (count > max)
        {
            printf("Reset. a = %.3lf\n", a[i]);
            //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("a = %.3lf count = %d and max = %d\n", a[i], count, max);
}

if (count == max){ // handle the case where the lat couple of numbers are the same e.g {1.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0};
    ++maxCount;
}

count = 1; //sets count back to 1 for next loop

// printf("max = %d\n", max);

if ((double) (size / max) != ((double)uniqueNum))
{
    double mode[maxCount]; //makes the mode array the right size to store all the modes
    number = a[0];
    for (unsigned int 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++] = number;
            }
            //Count is set back to 1 as we are counting a different number
            count = 1;
            number = a[i];
        }
    }

    if (count == max){
        mode[elementNum++] = number;
    }

    printf("\nMode: {");
    for (int i = 0; i < maxCount; i++)
    {
        printf(" %.3lf ", mode[i]);
    }
    printf("}\n");
}
else
{
    printf("\nNo mode");
}

注意:这可能不完全是您所追求的,但它应该引导您朝着正确的方向前进。