我正在嘗試編寫代碼,將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;
}
C++沒有[可變長度數組](https://en.wikipedia.org/wiki/Variable-length_array),因此在技術上這是無效的C++。如果你想要一個可變長度的數組,使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –
另外,'count'應該做什麼?它是一個數組還是不是? –
我更新了我的問題以包含我的整個代碼。伯爵應該計算我和我親戚之間的DNA序列中有多少個字母是相同的。 –