2016-04-23 38 views
2

我正在嘗試編寫代碼,將10個序列片段與一個親屬的10個序列DNA進行比較。用戶輸入他們的名字,他們想要比較的親屬以及他們的DNA。計算機輸出匹配的百分比。 ATTAGACGCA相比於ATAAGACGCA將匹配90%。在用戶說出多少親屬之後,親屬的數量是一個常數。我曾嘗試使用const,但它似乎並不想使用該數字。在C++中使用一個變量作爲數組參數

/********************************************************************** 
* Get DNA Sequence 
***********************************************************************/ 
void getMyDNA(char myDNA[]) 
{ 
    cout << "Enter your DNA sequence: "; 
    cin >> myDNA; 
} 

/********************************************************************** 
* Get Potential Relatives 
***********************************************************************/ 
int getRelatives() 
{ 
    int relatives = 0; 
    cout << "Enter the number of potential relatives: "; 
    cin >> relatives; 

    return relatives; 
} 

/********************************************************************** 
* Get Potential Relatives Names 
***********************************************************************/ 
void getRelativeName(string relativeNames[], int relatives) 
{ 
    string name; 
    for (int i = 0; i < relatives; i++) 
    { 
     cout << "Please enter the name of relative #" << i + 1 << ": "; 
     cin >> name; 
     relativeNames[i] = name; 
    } 
} 

/********************************************************************** 
* Get Potential Relatives DNA Sequence 
***********************************************************************/ 
void getRelativeDNA(char relativeDNA[][10], string relativeNames[], int relatives) 
{ 
    for (int i = 0; i < relatives; i++) 
    { 
     cout << "Please enter the DNA sequence for " << relativeNames[i] << ": "; 
     cin >> relativeDNA[i]; 
    } 
} 

/********************************************************************** 
* Display Potential Relatives Match 
***********************************************************************/ 
void displayMatch(string relativeNames, char relativeDNA[][10], int relatives, char myDNA[]) 
{ 
    const int family = relatives; 
    int count[family] = 0; 
    for (int r = 0; r < 3; r++) //relative number r 
    { 
     for (int d = 0; d < 10; d++) //dna piece number d 
     { 
     if (relativeDNA[r][d] == myDNA[d]) 
      count[r]++; 
     } 
    } 
} 

/********************************************************************** 
* Main 
***********************************************************************/ 
int main() 
{ 
    char myDNA[10]; 
    string relativeNames[50]; 
    char relativeDNA[50][10]; 

    // My DNA 
    getMyDNA(myDNA); 


    //# of relatives 
    int relatives = getRelatives(); 
    cout << endl; 

    //thier names 
    getRelativeName(relativeNames,relatives); 
    cout << endl; 


    //their dna 
    getRelativeDNA(relativeDNA,relativeNames,relatives); 
    cout << endl; 

    //display 
    displayMatch(relativeNames,relativeDNA,relatives,myDNA); 

    return 0; 
} 
+0

C++沒有[可變長度數組](https://en.wikipedia.org/wiki/Variable-length_array),因此在技術上這是無效的C++。如果你想要一個可變長度的數組,使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –

+0

另外,'count'應該做什麼?它是一個數組還是不是? –

+0

我更新了我的問題以包含我的整個代碼。伯爵應該計算我和我親戚之間的DNA序列中有多少個字母是相同的。 –

回答

2

如果count是一個新的數組動態創建一個新的數組如下...

int *count = new int[relatives]; 

我注意到你正在使用後下面...

count++; 

是你試圖增加一個整數或移動一個指針?此代碼可能有助於使其更清晰...

#include <assert.h> 
#include <iostream> 
#include <typeinfo> 

int main(void) { 
    const int x = 500; 
    int* a = new int[x]; 
    size_t i = 0; 
    for(i = 0; i < x;i++) { 
    a[i] = i; 
    } 

    for(i = 0; i < x;i++) { 
    //Print numbers without moving pointer 
    std::cout << a[i] << std::endl; 
    } 
    for(i = 0; i < x;i++) { 
    //Print numbers moving pointer 
    std::cout << a[0] << std::endl; 
    a++; 
    } 
    a = a - x; 
    delete[] a; 
    return 0; 
} 
+1

爲初學者提供建議,使用'new'作爲數組是一種魔法。 –

+0

並沒有提及使用'delete []'。 – PaulMcKenzie

+0

我會在剔號中修復它 – Harry

2

即使該參數作爲const傳遞,它也不起作用。可以用動態創建

int *count = new int[relatives]; 
+0

請不要使用術語「矢量」來描述您建議的OP。 – PaulMcKenzie

+0

我不確定''動態數組''是一個很好的術語。它的大小是可變的,但數組絕不是動態的。 –

+1

@MthethewCliatt你說得對,我的錯。 –

2

代替

int count[relatives] = 0; 

這是因爲標準的C++時relatives可以在運行時間改變無效陣列嘗試,使用

std::vector<int> count(relatives); 

附上<vector>頭。

+0

即使'親戚'不能改變,它不會仍然是無效的代碼?您不能將整個數組分配給整數。 –

+1

@MatthewCliatt:是的,初始化器必須是固定的,比如'{0}'(或者只是'{}')。 –

相關問題