c语言 - 如何找到特定元素在数组中重复了多少次?



我生成了一个有 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的返回值分配给名为countingsint。如果要返回一些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函数中,您尝试使用索引访问bucket10元素;唯一有效的索引是0, 1, ..., 9之一。

  • entropy_calculator中,假设我们将所有的 10 都改为 9,以便循环从x = 0x = 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.
  • 创建一个函数,该函数接受 10ints 的数组(即指向int的指针)作为参数,并打印数组中1s、2s、...、10s 的出现次数。

为了使程序稍微通用一些,我们定义了宏MAX_LEN并使其表示数字 10。

main中,首先,我们通过将种子设置为当前时间来初始化随机数生成器。其次,我们定义一个名为bucketMAX_LENint数组。第三,我们在[1, MAX_LEN]中用伪随机整数填充bucket的每个元素。最后,我们调用函数entropy_calculator,传递bucket作为唯一的参数,然后return 0

在函数entropy_calculator中,我们定义了一个叫做countsMAX_LENint数组,每个元素初始化为零。我们创建自己的内部映射,使得counts的第n个元素表示在bucket中找到的n的数量,对于{1, 2, ..., MAX_LEN}中的每个n。等价地,对于{1, 2, ..., MAX_LEN}中的每个n,在 bucket 中找到的n的数量由索引为n - 1counts元素表示。然后,我们遍历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 - 1counts元素表示{1, 2, ..., MAX_LEN}中随机生成的nn的数量。

当我们在[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);

}

最新更新