C 计算使用特定值调用函数的次数

C 计算使用特定值调用函数的次数,c,C,我想用数组中的键创建一个表。有没有一个简单的方法可以做到这一点 int array1[] = {1,5,3,8,9,11}; // table[1] // table[5] // table[3] int count(int a) { //a is one of the values in array. array1[] = {1,5,3,8,9,11}; // for ex 3. // I have to figure out how many times this func

我想用数组中的键创建一个表。有没有一个简单的方法可以做到这一点

int array1[] = {1,5,3,8,9,11};
// table[1]
// table[5]
// table[3]

int count(int a)
{

  //a is one of the values in array. array1[] = {1,5,3,8,9,11};
  // for ex 3. 
  // I have to figure out how many times this function was called with what values  1/5/3/8/9/11 
  table[3]++;
}
简单代码

  #include<stdio.h>
int n = 6; //number of elements in array1
int array1[] = {1,3,5,8,9,11};
int *funCount;//Count of elements in array1
int count(int a)
{
  int i;
  for(i = 0; i < n; i++)
    if(a == array1[i])
      break;
  funCount[i]++;
}

int main()
{
  funCount = (int*)calloc(n, sizeof(int));
  int i;
  count(1);
  count(3);
  count(5);
  count(8);
  count(9);
  count(11);
  for(i = 0; i < n; i++)
    printf("%d ",funCount[i]);
  return 0;
}
如果您的阵列1将变小,这种方法是可以的!
否则,我建议您使用哈希

在函数count之外创建一个全局arrayi.e。将其设置为zeroint。如果遇到数组1中的值,则增加索引值。该值甚至无法编译。n不是一个常量表达式。显然它不会编译!你期待什么?我的完整代码?他可以通过扫描设置“n”的值!我不希望有完整的代码,因为我不是OP。但是,这不是C,至少不是有效的C,而且因为它是错误的,所以一点帮助都没有。虽然他可以通过扫描来设置n的值,但这并没有任何好处——一个全局数组不能是可变长度的,它是C!这是C语言中的一个函数!有什么问题?你认为哪一行有问题?