gpt4 book ai didi

C 函数查找数组中的模式,打印出一个全为 0 的数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:08 24 4
gpt4 key购买 nike

我正在尝试创建一个返回排序数组模式的函数,但它无法正常工作。当没有模式时它工作得很好但是只要有模式我得到:

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

这是函数

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 个小错误。

最佳答案

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

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

double modes[maxCount];

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

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

因此,第二个 for 循环仅将变量“number”(作为最后一个 for 循环的结果,从“a”中分配了一个值)与“a”中的所有其他数字进行比较,因此它不是实际找到模式。

现在,在你的第二个 for 循环中,当你发现该数字 != a[i] 时,你忘记将变量“数字”刷新为数组“a”中的不相等元素。

所以基本上,函数的 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");
}

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

关于C 函数查找数组中的模式,打印出一个全为 0 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com