我已经生成了一个包含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);
}
发布于 2022-04-23 01:59:04
注释在您的代码上
entropy_calculator
的返回类型是double
,但不返回任何内容。如果不希望返回任何内容,请将返回类型设置为void
.main
,您尝试将entropy_calculator
的返回值分配给名为countings
的int
。如果要返回一些double
.值,那么countings
应该是一个
bucket
是一个由10个整数组成的数组。具有N
元素的数组的有效索引通常是0, 1, 2, ..., N - 1
;换句话说,第一个元素被指定为索引0
,第二个元素被指定为索引1
,.,N
th元素被指定为索引N - 1
。因此,bucket
数组中的有效索引是封闭区间[0, 10 - 1] = [0, 9]
中的任意整数。在您的0, 1, ..., 9
.函数中,您试图使用索引10
访问bucket
的元素;唯一有效的索引是bucket
中的一个
entropy_calculator
,假设我们将所有的10s改为9,这样循环就从x = 0
,x = 1
,。。,x = 9
和检查表单([bucket[10] == j)
在[1, 6]
中的某些j
被替换为([bucket[9] == j)
。所有这六个检查都只是简单地检查bucket
的第10个元素是否是1、2、.或6中的一个。您忽略了bucket
中其他9个随机生成的数字,所以您从未将它们包含在计数中。您还忽略了其他可能随机生成的值,即7、8、9和10,因为您目前只比较bucket
的第10个元素与1、2、.和6.。
溶液
我想你的任务是
bucket
.
int
s数组(即指向int
的指针),并打印数组中1
s、2
s、.、10
s出现的次数。H 268f 269
为了使程序更加通用,我们定义了宏MAX_LEN并使其表示数字10。
在main中,首先通过将种子设置为当前时间来初始化随机数生成器。其次,我们定义了一个名为MAX_LEN
ints的bucket数组。第三,我们在bucket中用一个伪随机整数填充[1, MAX_LEN]的每个元素.最后,我们调用函数entropy_calculator,传递bucket作为唯一参数,然后调用return 0。
在函数entropy_calculator中,我们定义了一个名为counts的MAX_LEN
int数组,每个元素都初始化为零。我们创建自己的内部映射,这样counts的n第四元素表示在bucket中找到的n的数量,对于{1, 2, ..., MAX_LEN}中的每个n。等价地,对于{1, 2, ..., MAX_LEN}中的每个{1, 2, ..., MAX_LEN},存储桶中的ns的数量由带有索引n - 1的counts元素表示。然后循环遍历bucket数组的元素,并使用映射在counts数组中增加相应的元素。然后,我们打印出counts的所有元素。
例如,对于我们程序中数组的一组有效索引中的某些counts[bucket[i] - 1].,即{0, 1, ..., MAX_LEN - 1},如果我们发现bucket[i]是5,那么我们希望增加counts的5th元素(因为D 109的n第四元素计数生成的D 110s的数量),即D 111,或者更一般地说,是
的。
程序
#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("%d\n", 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
简化版
如果任务是简单地生成MAX_LEN (宏表示[1, MAX_LEN]中的值10)随机整数,并计算生成了多少1s, 2s, ..., (MAX_LEN - 1), MAX_LENs,那么就可以这样做了。
我们创建一个MAX_LEN整数数组,称为counts。与counts关联的有效索引是0, 1, ..., MAX_LEN - 1。我们形成自己的内部映射,使得counts的元素n - 1索引表示n在{1, 2, ..., MAX_LEN}中随机生成的ns的数量。
当我们在[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("%d\n", 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
发布于 2022-04-23 03:53:17
#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);
}
https://stackoverflow.com/questions/71973372
复制相似问题