我正在尝试编写将10个序列的DNA与相对的10个序列DNA进行比较的代码。用户输入他们的名字,他们想要比较多少亲戚,以及他们的DNA。计算机输出匹配的百分比。与ATAAGACGCA相比,ATTAGACGCA将达到90%。在用户说明亲戚的数量之后,亲属的数量是一个常数。我尝试过使用const,但它似乎并不想使用数字。
/**********************************************************************
* 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 :(得分: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 :(得分:2)
即使参数作为const传递,它也不会起作用。您可以尝试使用动态创建的数组
int *count = new int[relatives];
答案 2 :(得分:2)
而不是
int count[relatives] = 0;
当relatives
在运行时变化时,作为标准C ++无效,请使用
std::vector<int> count( relatives );
包含<vector>
标题。