c++ - 在 C++ 中使用数组调用方法

标签 c++ c arrays methods

我的问题是我在调用 int main() 中的 int computePattern 方法时遇到问题。我可以使用 insertNumber 方法。但我不知道如何使用computePattern方法。该方法是计算模式的数量并返回计数。模式是两个相邻的数字必须大于 7。因此,只有 5 对需要检查,因为有 10 个数字。目前输出只是有效的随机数组,但我也需要打印出模式的数量。我需要使用问题中所需的方法。

int insertNumber(int b )
{
 int a = rand()%10+1;
 return a;
}
int computePattern(int* inArray, int size )
{
int patterns=0;
 for(int z=0;z<size;z=z+2)
 {
    if(inArray[z]+inArray[z+1]>7)
    {
        patterns ++ ;
    }
}
 return patterns;
}
int main()
{
int arrays[10];
srand(time(0));
for ( int b=0; b<10 ; b++)
{
 arrays[b]  = insertNumber(b);
}
cout << "Arrays";
for ( int c= 0; c<10; c++)
{
    cout << " " ;
    cout << arrays[c];
}
int patterns =computePattern(arrays,10);
cout<<endl;
cout << "Patterns" <<patterns;
}

最佳答案

computePattern 中的数组仍未初始化。因此,它将在内存中包含随机变量,这会扰乱您的计算路径。

您也许应该将该函数修改为intcomputePattern(int z, int array[])。然后从主作用域传递数组

关于c++ - 在 C++ 中使用数组调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38636113/

相关文章:

c - 是否可以创建具有可变数量元素的数组?

c++ - 数组变量返回意外值

c++ - 将二叉树排序为排序数组

c++ - mingw "too many sections"错误,同时在 Qt 中编译巨大的头文件

C++ 指向函数的指针,该函数将模板化类的实例作为参数

c++ - C++ 中成员变量的关键字 mutable 的替代方法

c - GNU Makefile并行执行两个文件

将给定字符串与结果进行比较

c - 是否可以在 C 中将 char** 转换为 char*?

c - 指针数组外部问题