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

Using a Variable as an Array Parameter in C++

本文关键字:数组 参数 变量 C++      更新时间:2023-10-16

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

作为标准无效C++当relatives在运行时可能发生变化时,请使用

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

包括<vector>标头。

如果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;
}

即使参数作为 const 传递,它也不会起作用。您可以尝试使用动态创建的数组

int *count = new int[relatives];