我生成了一个有 10 个整数的数组。我必须找到一个元素重复了多少次,但我总是得到0
作为输出。在此代码中,countcolor
变量是我的计数值。例如countcolor1
是整数1
的计数值。这是我到目前为止所拥有的:
#include <stdio.h>
#include <time.h>
int i;
double entropy_calculator(int bucket[10]);
int main() {
srand(time(NULL));
int countings;
int bucket[10];
for (i = 0; i < 10; ++i) {
bucket[i] = 1 + rand() % 10;
printf("%d n", bucket[i]);
}
countings = entropy_calculator(bucket);
return 0;
}
double entropy_calculator(int bucket[10]) {
int x;
int countcolor1 = 0, countcolor2 = 0, countcolor3 = 0,
countcolor4 = 0, countcolor5 = 0, countcolor6 = 0;
for (x = 0; x <= 10; ++x) {
if (bucket[10] == 1)
countcolor1++;
if (bucket[10] == 2)
countcolor2++;
if (bucket[10] == 3)
countcolor3++;
if (bucket[10] == 4)
countcolor4++;
if (bucket[10] == 5)
countcolor5++;
if (bucket[10] == 6)
countcolor6++;
}
printf("%d,%d,%d,%d,%d,%d",
countcolor1, countcolor2, countcolor3,
countcolor4, countcolor5, countcolor6);
}
关于代码的说明
-
entropy_calculator
的返回类型是double
,但您不返回任何内容。如果不希望返回任何内容,请将返回类型设置为void
。 -
在
main
中,您尝试将entropy_calculator
的返回值分配给名为countings
的int
。如果要返回一些double
值,countings
应该是一个double
。 -
未定义的行为。根据 C 标准,如果数组下标超出范围,则未定义程序的行为。
bucket
是一个由 10 个整数组成的数组。具有N
元素的数组的有效索引通常为0, 1, 2, ..., N - 1
;换句话说,第一个元素被分配索引0
,第二个元素被分配索引1
,...,第N
个元素被分配索引N - 1
。因此,bucket
数组中的有效索引是闭区间[0, 10 - 1] = [0, 9]
中的任何整数。在entropy_calculator
函数中,您尝试使用索引访问bucket
10
元素;唯一有效的索引是0, 1, ..., 9
之一。 -
在
entropy_calculator
中,假设我们将所有的 10 都改为 9,以便循环从x = 0
、x = 1
、.. 、x = 9
运行,并且[1, 6]
中某些j
的形式([bucket[10] == j)
的检查被替换为([bucket[9] == j)
。所有这六个检查都只是检查bucket
的第 10 个元素是 1、2、... 还是 6 之一。您忽略了bucket
中其他 9 个随机生成的数字,因此您永远不会将它们纳入计数。您还忽略了其他可能的随机生成的值,即 7、8、9 和 10,因为您目前仅将bucket
的第 10 个元素与 1、2、... 和 6 进行比较。
溶液
我假设你的任务是
- 在
[1, 10]
中生成 10 个随机整数,并将它们存储在一个int
数组中,例如bucket
. - 创建一个函数,该函数接受 10
int
s 的数组(即指向int
的指针)作为参数,并打印数组中1
s、2
s、...、10
s 的出现次数。
为了使程序稍微通用一些,我们定义了宏MAX_LEN
并使其表示数字 10。
在main
中,首先,我们通过将种子设置为当前时间来初始化随机数生成器。其次,我们定义一个名为bucket
的MAX_LEN
int
数组。第三,我们在[1, MAX_LEN]
中用伪随机整数填充bucket
的每个元素。最后,我们调用函数entropy_calculator
,传递bucket
作为唯一的参数,然后return 0
。
在函数entropy_calculator
中,我们定义了一个叫做counts
的MAX_LEN
int
数组,每个元素初始化为零。我们创建自己的内部映射,使得counts
的第n
个元素表示在bucket
中找到的n
的数量,对于{1, 2, ..., MAX_LEN}
中的每个n
。等价地,对于{1, 2, ..., MAX_LEN}
中的每个n
,在 bucket 中找到的n
的数量由索引为n - 1
的counts
元素表示。然后,我们遍历bucket
数组的元素,并使用我们的映射递增counts
数组中的相应元素。然后我们打印出counts
的所有元素。
- 例如,对于我们程序中数组的有效索引集中的某些
i
,即{0, 1, ..., MAX_LEN - 1}
,如果我们发现bucket[i]
是5
,那么我们要增加counts
的第5
个元素(因为counts
的第n
个元素计算生成的n
的数量),这是counts[5 - 1]
的,或者更一般地说,counts[bucket[i] - 1]
。
程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEN 10
void entropy_calculator(int bucket[]);
int main(void) {
/* initialize random number generator */
srand((unsigned) time(NULL));
/* will contain MAX_LEN random ints in [1, MAX_LEN] */
int bucket[MAX_LEN];
/* generate pseudo-random ints in [1, MAX_LEN], storing them in bucket */
for (int i = 0; i < MAX_LEN; i++) {
bucket[i] = 1 + rand() % MAX_LEN;
printf("%dn", bucket[i]);
}
entropy_calculator(bucket);
return 0;
}
/****************************************************************************
* entropy_calculator: given an array of MAX_LEN integers in [1, MAX_LEN], * *
* prints the number of occurrences of each integer in *
* [1, MAX_LEN] in the supplied array *
****************************************************************************/
void entropy_calculator(int bucket[]) {
int counts[MAX_LEN] = {0}; /* initialize counts to all 0s */
int i; /* loop variable */
for (i = 0; i < MAX_LEN; i++)
counts[bucket[i] - 1]++;
/* printing all elements of counts */
for (i = 0; i < MAX_LEN; i++) {
if (i % 4 == 0) printf("n");
printf(" %2d*: %d", i + 1, counts[i]);
}
printf("n");
}
示例会话
3
9
2
6
10
9
3
8
3
1
1*: 1 2*: 1 3*: 3 4*: 0
5*: 0 6*: 1 7*: 0 8*: 1
9*: 2 10*: 1
简体版
如果任务只是在[1, MAX_LEN]
中生成MAX_LEN
(表示值 10 的宏)随机整数并计算生成了多少个1s, 2s, ..., (MAX_LEN - 1), MAX_LENs
,那么可以简单地按如下方式完成。
我们创建一个MAX_LEN
整数数组,称为counts
.与counts
关联的有效索引0, 1, ..., MAX_LEN - 1
。我们形成自己的内部映射,使得具有索引n - 1
的counts
元素表示{1, 2, ..., MAX_LEN}
中随机生成的n
n
的数量。
当我们在[1, MAX_LEN]
中生成一个随机整数时,我们将其分配给cur
,并且我们用索引cur - 1
递增counts
元素,因为该元素表示cur
数的出现次数。
程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEN 10
int main(void) {
srand((unsigned) time(NULL)); /* initialize random number generator */
int counts[MAX_LEN] = {0}; /* initialize counts to all 0s */
int i, cur;
for (i = 0; i < MAX_LEN; i++) {
cur = 1 + rand() % MAX_LEN; /* pseudo-random int in [1, MAX_LEN] */
printf("%dn", cur);
counts[cur - 1]++;
}
/* printing all elements of counts */
for (i = 0; i < MAX_LEN; i++) {
if (i % 4 == 0) printf("n");
printf(" %2d*: %d", i + 1, counts[i]);
}
printf("n");
return 0;
}
示例会话
8
4
6
2
4
1
10
9
2
10
1*: 1 2*: 2 3*: 0 4*: 2
5*: 0 6*: 1 7*: 0 8*: 1
9*: 1 10*: 2
#include<stdio.h>
#include<time.h>
int i;
double entropy_calculator(int bucket[10]);
int main()
{
srand(time(NULL));
int countings;
int bucket[10];
for(i=0; i<10; ++i)
{
bucket[i] = 1 + rand() % 10;
printf("%d ", bucket[i]);
}
printf("n");
countings = entropy_calculator(bucket);
return 0;
}
double entropy_calculator(int bucket[10])
{
int x;
int countcolor1=0, countcolor2=0, countcolor3=0, countcolor4=0, countcolor5=0, countcolor6=0;
for(x=0; x<10; ++x)
{
if (bucket[9]==1)
countcolor1++;
if (bucket[9]==2)
countcolor2++;
if (bucket[9]==3)
countcolor3++;
if (bucket[9]==4)
countcolor4++;
if (bucket[9]==5)
countcolor5++;
if (bucket[9]==6)
countcolor6++;
}
printf("%d,%d,%d,%d,%d,%d",countcolor1,countcolor2,countcolor3,countcolor4,countcolor5,countcolor6);
}