在c中计算哈希集中的簇

发布于 2024-10-19 20:43:47 字数 1379 浏览 1 评论 0原文

我正在用 c 语言制作一个哈希集 ADT 以用于家庭作业。我一生都无法弄清楚为什么我的逻辑不适用于计算哈希集中簇的函数。

 void printClusterStats (hashset_ref hashset) {
   int **clusters = (int**)calloc (hashset->length, sizeof(int));
   assert (clusters);
   int ct = 0;
   // i traverses hashset->array
   // ct adds up words in each cluster
   // this loop screws up vvv
   for ( int i = 0; i < hashset->length; ++i) {
      if (hashset->array[i] == NULL) {
         clusters[ct] += 1;
         ct = 0;
      }else {
        ct += 1; 
      }
   }
   clusters[ct] +=1;  //catch an ending cluster

   printf("%10d words in the hash set\n", hashset->load);
   printf("%10d length of the hash array\n", hashset->length);
   for ( int i = 0; i < hashset->length; i++){
      if (clusters[i] == 0) continue;
      else{
         printf("%10d clusters of size %3d\n", clusters[i], i);
      }
   }
   free(clusters);
}

该函数的输出如下所示:

        26 words in the hash set
        63 length of the hash array
        96 clusters of size   0
        32 clusters of size   1
        16 clusters of size   2
         4 clusters of size   4
         4 clusters of size   6
       305 clusters of size  33
-703256008 clusters of size  34
-703256008 clusters of size  35

对于我的输入哈希集,63 长的数组中有 26 个单词。然而,计数不知何故搞砸了。

编辑:我手动计算了簇数,发现每个计数都是应有的 4 倍。这意味着什么?

I'm making a hashset ADT in c for a homework assignment. I cannot figure out for the life of me why my logic isn't working for a function that counts clusters in a hashset.

 void printClusterStats (hashset_ref hashset) {
   int **clusters = (int**)calloc (hashset->length, sizeof(int));
   assert (clusters);
   int ct = 0;
   // i traverses hashset->array
   // ct adds up words in each cluster
   // this loop screws up vvv
   for ( int i = 0; i < hashset->length; ++i) {
      if (hashset->array[i] == NULL) {
         clusters[ct] += 1;
         ct = 0;
      }else {
        ct += 1; 
      }
   }
   clusters[ct] +=1;  //catch an ending cluster

   printf("%10d words in the hash set\n", hashset->load);
   printf("%10d length of the hash array\n", hashset->length);
   for ( int i = 0; i < hashset->length; i++){
      if (clusters[i] == 0) continue;
      else{
         printf("%10d clusters of size %3d\n", clusters[i], i);
      }
   }
   free(clusters);
}

The output of this function looks like:

        26 words in the hash set
        63 length of the hash array
        96 clusters of size   0
        32 clusters of size   1
        16 clusters of size   2
         4 clusters of size   4
         4 clusters of size   6
       305 clusters of size  33
-703256008 clusters of size  34
-703256008 clusters of size  35

For my input hashset, there are 26 words in an array 63 long. However the counting screws up somehow.

EDIT: I've counted the clusters manually and discovered every count is 4 times what it should be. What does that mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-10-26 20:43:47

此行创建一个指向 int 的指针数组,

int **clusters = (int**)calloc (hashset->length, sizeof(int));

而不是您在存储簇计数时实际需要的 int 数组。

int *clusters = (int*)calloc (hashset->length, sizeof(int));   

因此,当您执行 clusters[ct] += 1; 时,它将被视为指针算术,并且每次将簇计数加 4,因为您所在的系统具有 4 字节指针。

This line creates an array of pointers to int

int **clusters = (int**)calloc (hashset->length, sizeof(int));

rather than an array of int that you actually want if you are storing cluster counts

int *clusters = (int*)calloc (hashset->length, sizeof(int));   

Consequently, when you do clusters[ct] += 1; it will be treated as pointer arithmetic, and add 4 to the cluster count each time, since you are on a system with 4-byte pointers.

夜唯美灬不弃 2024-10-26 20:43:47
int **clusters = (int**)calloc (hashset->length, sizeof(int));

应该是

int *clusters = (int*)calloc (hashset->length, sizeof(int));

我在 c 方面还不太称职,所以我无法解释为什么这解决了我的问题。但如果你好奇的话就去吧!

这是正确的输出

26 words in the hash set
63 length of the hash array
24 clusters of size   0
 8 clusters of size   1
 4 clusters of size   2
 1 clusters of size   4
 1 clusters of size   6
int **clusters = (int**)calloc (hashset->length, sizeof(int));

Should be

int *clusters = (int*)calloc (hashset->length, sizeof(int));

I'm not very competent at c yet, so I can't explain why this solved my problem. But there you go if you were curious!

Here's the correct output

26 words in the hash set
63 length of the hash array
24 clusters of size   0
 8 clusters of size   1
 4 clusters of size   2
 1 clusters of size   4
 1 clusters of size   6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文