2014-11-23 88 views
-2

任何人都可以解釋我在以下程序中的count[array[i]]是什麼意思? 代碼的作用是打印數組中所有帶頻率的重複數字。此聲明計數的含義[array [i]]

#include <stdio.h> 
#include <malloc.h> 

void duplicate(int array[], int num) 
{ 
    int *count = (int *)calloc(sizeof(int), (num - 2)); 
    int i; 

    printf("duplicate elements present in the given array are "); 
    for (i = 0; i < num; i++) 
    { 
     if (count[array[i]] == 1) 
      printf(" %d ", array[i]); 
     else 
      count[array[i]]++; 
    } 
} 

int main() 
{ 
    int array[] = {5, 10, 10, 2, 1, 4, 2}; 
    int array_freq = sizeof(array)/sizeof(array[0]); 
    duplicate(array, array_freq); 
    getchar(); 
    return 0; 
} 
+1

這是什麼'數組[我]'怎麼辦? 'count [someIndex]'做什麼?把它們放在一起。這也不是一個聲明。 – chris 2014-11-23 12:40:47

+0

'count [array [i]] ++;'可以被重寫爲:'int t = array [i];算[T] ++;'。它有幫助嗎? – 2014-11-23 12:42:21

+0

它用於查找數組內的副本 – arahan567 2014-11-23 12:58:13

回答

1

這是用於發現在陣列重複越差方法。

count[array[i]]++; 

所以,array[i]將是指數,這將在轉作爲指數計數陣列在返回的數量。對於e.g: -

array[4] = {1,2,3,1}; 

遍歷數組,這將是這樣的: -

count[array[0]] = count[1] = 1; 
count[array[1]] = count[2] = 1; 
count[array[2]] = count[3] = 1; 
count[array[3]] = count[1] = 2; << Increment the count... 
1

如果我是正確的。您正在嘗試打印給定數組中的所有重複項。 首先,您創建一個數組數並用零填充。

int *count = (int *)calloc(sizeof(int), (num - 2)); 

如果count [x]等於0,則表示數字x未出現在數組中。 如果count [x]等於1,則表示數組中只有一個x實例。 如果count [x]大於1,則表示數組中有多個x實例。

所以你通過給定的數組和更新計數數組。同時更新您正在檢查是否有任何重複。這就是這些行:

if (count[array[i]] == 1) 
    printf(" %d ", array[i]); 
else 
    count[array[i]]++; 

從我的角度來看,這不是做這樣的例程的最佳方式。現在我可以看到的兩個問題是:您不能自由計數數組,計數數組的大小必須大於給定數組中的任何數。作爲另一種解決方案,您嘗試使用std :: set或std :: unique函數來完成此任務。

希望它有幫助。

0

您顯示的代碼沒有意義。例如,功能calloc的第一個參數是必須分配的元素的數量。但是在你的代碼中,相應的參數是sizeof(int),通常等於4.函數的第二個參數是元素的大小。但是在程序中指定了num - 2(?)。雖然calloc通過第二個參數分配一個等於第一個參數乘積的內存區域,但它完全不清楚爲什麼使用num - 2

至於這個表達count[array[i]]那麼這是一個明顯的錯誤。 array [i]的值可能比動態內存中分配元素的數量大得多。

因爲很明顯你可以將這個表達式邏輯地分成兩部分。例如

int j = array[i]; 
count[j]; 

例如,對於陣列

int array[] = {5, 10, 10, 2, 1, 4, 2}; 

array[1]等於10。因此count[array[i]]相當於count[10]但有被分配僅num - 2元件通過計數指向。由於num - 2在這種特殊情況下等於5,因此count [10]是數組中不存在的元素。

考慮到一般情況下您需要自由動態分配的內存。

我想你的意思是像下面

#include <stdio.h> 
#include <stdlib.h> 

void duplicate(const int a[], size_t n) 
{ 
    size_t *count; 
    int i; 

    if (n < 2) return; 

    count = (size_t *)calloc(n - 1, sizeof(size_t)); 

    for (i = 1; i < n; i++) 
    { 
     size_t j = 0; 
     while (j < i && a[i] != a[j]) j++; 

     if (j != i) ++count[j]; 
    } 

    printf("duplicate elements present in the given array are "); 
    for (i = 0; i < n - 1; i++) 
    { 
     if (count[i]) 
     { 
      printf(" %d ", a[i]); 
     } 
    } 

    free(count); 
} 

int main(void) 
{ 
    int a[] = { 5, 10, 10, 2, 1, 4, 2 }; 
    size_t n = sizeof(a)/sizeof(*a); 

    duplicate(a, n); 

    return 0; 
} 

輸出是

duplicate elements present in the given array are 10 2 

如果要替換語句

printf(" %d ", a[i]); 

printf(" %d(%zu) ", a[i], count[i] + 1); 

那麼輸出將是

duplicate elements present in the given array are 10(2) 2(2) 

如果你會用我的代碼,那麼請不要忘記,以紀念我的回答是最好的。:)