2013-07-06 38 views
-1

我使函數刪除陣列在C其中相同的數據++,從而使這樣的過程:誤差函數刪除陣列的數據,在C++中相同

input:4 
input: 
25.50 
64.25 
64.25 
25.50 
output: 
5.50 
64.25 

但像這樣的圖像的功能的錯誤 enter image description here

這裏的代碼

#include <iostream> 
using namespace std; 
void removeSame(double a[int x]){ 
    int index[x]; 
    for(int z=0; z<x; z++){ 
     jum[z]=0; 
     for(int c=0; c<x; c++){ 
      if(a[c]==a[z]){ 
       index[z]=c; 
      } 
     } 
    } 
    for(int z=0; z<x; z++){ 
     if(z==index[z]){ 
      cout<<a[z]<<endl; 
     } 
    }  
} 
int main(){ 
    int n,x; 
    cin>>n; 
    double a[n]; 
    for(x=0; x<n; x++){ 
     cin>>a[x]; 
    } 
    removeSame(a[x]); 
    return 0; 
} 

然後當我改變這樣的代碼,再次出現錯誤

void removeSame(double a[], int x){ 
... 
} 

錯誤是這樣的:

cannot convert 'double' to 'double*' for argument '1' to 'void hapusygsama(double*, int)' 

請幫我

更新。 THX爲大家誰是我的回覆線程

#include <iostream> 
using namespace std; 
void hapusygsama(double a[], int len){ 
    int index[len]; 
    for(int z=0; z<len; z++){ 
     for(int c=0; c<len; c++){ 
      if(a[c]==a[z]){ 
       index[z]=c; 
      } 
     } 
    } 
    cout<<endl; 
    for(int z=0; z<len; z++){ 
     if(z==index[z]){ 
      cout<<a[z]<<endl;; 
     } 
    } 
} 
int main(){ 
    int n,x; 
    cin>>n; 
    double a[n]; 
    for(x=0; x<n; x++){ 
     cin>>a[x]; 
    } 
    hapusygsama(a, n); 
    return 0; 
} 
+0

爲什麼你不把所有的項目插入地圖,然後打印? – aryjczyk

+1

我很困惑,爲什麼這個'double a [n];'編譯?由於'n'不是一個常量。 –

+0

您是否嘗試過在Google中通過查詢_「C++傳遞數組來傳遞函數」來搜索某些內容?這是一個非常受歡迎的問題。 – soon

回答

0

你不正確傳遞的參數功能:

#include <iostream> 
using namespace std; 
void removeSame(double a[], int x){ 
    //     ^^^ 
    int index[x]; 
    for(int z=0; z<x; z++){ 
     jum[z]=0; 
     for(int c=0; c<x; c++){ 
      if(a[c]==a[z]){ 
       index[z]=c; 
      } 
     } 
    } 
    for(int z=0; z<x; z++){ 
     if(z==index[z]){ 
      cout<<a[z]<<endl; 
     } 
    }  
} 
int main(){ 
    int n,x; 
    cin>>n; 
    double a[n]; 
    //  ^^^^ ERROR : not permitted, n is not a constant 
    //     it would not compile. 
    for(x=0; x<n; x++){ 
     cin>>a[x]; 
    } 
    removeSame(a, x); 
    //  ^^^ 
    return 0; 
} 

你想收到removeSame陣列,以去除誰是相同的值。在你的代碼中,你試着只傳遞一個double值。

正如我所說的,這將編譯,因爲n不是一個常數,當你在main函數聲明a

編輯:它似乎是gcc誰允許這種語法的擴展。但我建議使用一些更常見的...

你應該這樣做:

int main() { 
    int n, x; 
    cin >> n; 
    double* a = new double[n]; 
    ... 
    delete[] a; 
    return 0; 
} 

在這種情況下,removeSame原型應該是:

void removeSame(double* a, int x); 

而最後一件事,你應該驗證用戶輸入的值,開發中最重要的規則之一是:永遠不要相信用戶的輸入!

1

你的代碼是非法的C++。 C++目前不支持運行時數組。所有數組維度必須包含常量邊界。另外,C++不允許在double a[int x]等參數中聲明任何類型的聲明大小。您必須將大小作爲單獨的參數傳遞。

0

一個才達到所希望的behaivour更高雅的方式是使用std::uniquestd::resize

的代碼將是:

int main() { 
    std::vector<float> myvector; 
    int n; 
    cin >> n; 
    float b; 
    for (int i=0; i<n; i++){ 
     cin >> b; 
     myvector.push_back(b); 
    } 
    std::sort(myvector.begin(), myvector.end()); 
    myvector.resize(std::distance(myvector.begin(),std::unique (myvector.begin(), myvector.end()))); 

return 0; 
} 
在你的問題

你也需要輸入flaoting點數,而你使用的代碼int,我想你也應該改變它。