在 C++ 中使用变量作为数组参数
Using a Variable as an Array Parameter in C++
我正在尝试编写代码,将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];
相关文章:
- C++函数模板需要 &for 数组参数
- C++ 数组参数不起作用
- 使用数组参数进行函数专用化
- 为什么我的数组参数的方法无法正常工作?(C )
- SWIG - Java 代理类数组参数
- 在 gcc 中使用数组参数编译外部"c"代码
- 从 VB6 调用的 C++ DLL 函数中的输出数组参数
- 如何根据数组参数项类型重载 IDL 中的函数
- 引用谷歌测试/模拟框架中的数组参数
- C++将数组参数传递给函数
- 字符星形数组参数未正确终止
- 如何将德尔菲的"字符串数组"参数翻译成C++?
- 为什么 gcc 无法推断数组参数的模板化大小?(C++11)
- 使用 const char 数组参数分析 constexpr 显示运行时执行
- C++指针数组参数
- 固定大小的多维数组参数
- 为什么数组参数被视为常量数组
- 如何在pgsql中格式化数组参数
- C++字符数组参数在x86编译中包含奇怪的字符
- 对象数组参数 - 错误:字段“字母”的类型不完整