c++ - 在 C++ 中使用变量作为数组参数

标签 c++ arrays variables

我正在尝试编写代码,将 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;
}

最佳答案

代替

int count[relatives] = 0;

relatives 时作为标准 C++ 是无效的可以在运行时变化,使用

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

包括 <vector>标题。

关于c++ - 在 C++ 中使用变量作为数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805905/

相关文章:

c++ - boost 异步服务器缓冲区错误

javascript - 从数组 Ionic 2 Storage 中删除元素

perl - @$ 和 $$ 在 Perl 中是什么意思?

bash - 为什么 shell 会忽略通过变量传递给它的参数中的引号字符?

php - 在 HTML 回显中使用变量转义双引号

c++ - 这有多危险? (List<T> 被转换为 List<const T>)

c++ - 试图了解为什么我的链表仅显示最后添加的节点

c++ - 我无法运行将 allegro5 与 cmake 结合使用的程序

c++ - "int *(&arry)[10] = ptrs;"是什么意思?你怎么读这样的东西?

javascript - 在 ReactJS 上使用 Lodash 对映射列表进行排序