2014-02-25 44 views
0

我有一個項目在c + +中接收數組,我需要建立一個程序,將只打印出現超過3倍+他們的索引的數字。 例如,對於陣列6,4,4,5,2,4,4,3,5,5 - 它將打印: 4:1,2,5,6 5:3,8,9排序數組 - 不斷收到錯誤。

和最重要的 - 它不應該超過O(n * log n)。

的sooo ....對這個問題...

這是我不斷收到錯誤:

1>HW - 2.obj : error LNK2019: unresolved external symbol "void __cdecl mergeSortP(int *   const,int)" ([email protected]@[email protected]) referenced in function "void __cdecl checkIfNumberIsMoreThenThreeTimes(int * const,int)" ([email protected]@[email protected]) 
1>C:\documents\visual studio 2012\Projects\HW - 2\Debug\HW - 2.exe : fatal error LNK1120: 1 unresolved externals 

,這是代碼:

void checkIfNumberIsMoreThenThreeTimes(int arr[], int n) 
{ 
    int **p; 
    p = copyToPointersArr(arr, n); 
    mergeSortP(*p, n); 
    output(arr, p, n); 
} 

//

int** copyToPointersArr(int *arr, int n) 
{ 
    int **pointers; 

    for (int i=0; i<n; i++) 
     *pointers[i]=arr[i]; 
    return pointers; 
} 

//

void merge(int **a1, int **a2, int size1, int size2, int **res) 
{ 
    int ind1, ind2, ind; 
    ind1=ind2=ind=0; 
    while (ind1<size1 && ind2<size2) 
    { 
     if (*a1[ind1]<=*a2[ind2]) 
     { 
      res[ind]=a1[ind1]; 
      ind1++; 
     } 
     else 
     { 
      res[ind]=a2[ind2]; 
      ind2++; 
     } 
     ind++; 
    } 
    while (ind1<size1) 
    { 
     res[ind]=a1[ind1]; 
     ind1++; 
     ind++; 
    } 
    while (ind2<size2) 
    { 
     res[ind]=a2[ind2]; 
     ind2++; 
     ind++; 
    } 
} 

//

void mergeSortP(int **a, int size) 
{ 
    int i; 
    int **temp=NULL; 
    if (size==1) 
     return; 
    else 
    { 
     mergeSortP(a, size/2); 
     mergeSortP(a+(size/2), size-(size/2)); 
     temp = new int* [size]; 
     merge(a, a+(size/2), size/2 , size-(size/2), temp); 
     for (i = 0; i < size; i++) 
      a[i] = temp[i]; 
     delete []temp; 
     temp=NULL; 
    } 
} 

//

void output(int arr[], int **ptr,int size) 
{ 

    int i, j, count=0; 
    for (i = 0; i < size-1; i++) 
    { 
     if(*ptr[i]==*ptr[i+1]) 
      count++; 
     else if (count>=2) 
     { 
      cout << *ptr[i] << ": "; 
      for (j = count; j >= 0; j--) 
       cout << (ptr[i-j]-arr) << ", "; 
      count=0; 
     } 
     else 
      count=0; 
    } 
} 

請幫助! 在此先感謝....

+0

mergeSortP想要一個雙指針。您正試圖傳遞單個指針。 –

+1

@ user3328918 - 請原諒我的無知,但是如何使用合併排序解決您的問題,即計算數組中出現的數字的次數並列出索引?如果你真的在學習C++,那麼顯而易見的解決方案是使用std :: map ,其中SomeStruct是一個包含count和一個包含找到的索引的向量的結構。大概5或6行,最多寫10個。 – PaulMcKenzie

+0

我同意@PaulMcKenzie。這些日子真的是C++教育的傳承嗎? _雙指針?學習C++的人不會對_regular_指針有足夠的麻煩嗎? – Chad

回答

0

void checkIfNumberIsMoreThenThreeTimes(int arr[], int n),你有

int **p; 

mergeSortP(*p, n); 

所以第一個參數是mergeSortP類型int*的。

但是,你只有一個功能

void mergeSortP(int **a, int size) 

因此鏈接抱怨,因爲它無法找到它。也許你打算打電話給

mergeSortP(p, n); 

取而代之?

+0

我試圖做到這一點,但後來我得到的錯誤: 'mergeSortP':無法將參數1從'int **'轉換爲'int []' 1>指向的類型是不相關的;轉換需要reinterpret_cast,C風格轉換或函數風格轉換 – user3328918

0

以下可能會有所幫助:https://ideone.com/oiAzTh

void displayFrequentNumber(const std::vector<int>& input) 
{ 
    std::vector<std::pair<int, std::size_t>> v; 

    for (std::size_t i = 0; i != input.size(); ++i) { 
     v.push_back({input[i], i}); 
    } 
    std::sort(begin(v), end(v)); 
    for (auto it = begin(v); it != end(v);) { 
     auto next = find_if(it, end(v), 
          [it](const std::pair<int, std::size_t>& rhs) 
           {return it->first != rhs.first;}); 
     if (next - it >= 3) { 
      std::cout << it->first << ":"; 
      for (; it != next; ++it) { 
       std::cout << " " << it->second; 
      } 
      std::cout << std::endl; 
     } 
     it = next; 
    } 
}