【发布时间】:2020-05-07 21:44:19
【问题描述】:
我正在编写一个函数,它将找到具有最大除数的数字,但该函数不返回任何内容。有人可以指出我的错误吗?
这是问题
编写一个 C++ 程序,创建一个包含 30 个元素的整数数组。在这个数组中获取输入(在 main 功能)。之后,使用引用指针将该数组传递给名为“Find_Max_Divisors”的函数。 函数“Find_Max_Divisors”应该在数组中找到(并返回)具有最高的数字 除数的数量。最后,主函数显示除数最多的数字。
#include <iostream>
using namespace std;
int main ()
{
int arr[30];
int* array = &arr[30];
cout << "Please enter values of the array" << endl;
for (int i=0; i<30; i++)
{
cin >> arr[i];
}
cout << "Number with most divisors in array is " << endl;
int Find_Max_Divisors (*array);
}
int Find_Max_Divisors (int p[])
{
int count=0, max_divisor, max_counter, prev=0, repeat=0, divisor;
for (int i=2; i<=30; i++)
{
if (p[i]%i==0)
{
count++;
}
if (count > prev)
{
prev = count;
divisor = p[i];
}
if (count==max_counter && max_counter!=0)
{
cout << p[i] <<" has maximum of "<< count <<" divisors.\n";
}
max_counter = prev;
max_divisor = divisor;
repeat++;
}
return count;
}
【问题讨论】:
-
你说函数没有返回任何东西,但你怎么知道函数被调用了?
-
@drescherjm 这对我来说看起来像是一个常规的旧变量声明,它应该编译得很好:)
-
C++ 肯定不好解析
-
i<=30可能是 1 错误。 -
我们不会在问题标题中添加“SOLVED”。您表示通过接受答案解决了问题。 (我修正了标题。)
标签: c++