C++ 编写一个递归C++;函数,该函数计算并返回数组中整数的乘积 #包括 使用名称空间std; int产品(int anArray[],int n); int main(int argc,const char*argv[] { int myArray[3]={1,2,3}; cout
如果大小为C++ 编写一个递归C++;函数,该函数计算并返回数组中整数的乘积 #包括 使用名称空间std; int产品(int anArray[],int n); int main(int argc,const char*argv[] { int myArray[3]={1,2,3}; cout,c++,recursion,C++,Recursion,如果大小为1则只有一个元素,因此 #include<iostream> using namespace std; int theProduct(int anArray[], int n); int main(int argc, const char * argv[]) { int myArray[3] = {1, 2, 3}; cout << "The product of the array elements of myArray is "&l
1
则只有一个元素,因此
#include<iostream>
using namespace std;
int theProduct(int anArray[], int n);
int main(int argc, const char * argv[])
{
int myArray[3] = {1, 2, 3};
cout << "The product of the array elements of myArray is "<<theProduct(myArray,3)<<endl;
return 0;
}
int theProduct(int anArray[], int n)
{
if (n <= 0)
return 0;
else if (n == 1) //base case
return anArray[0] * anArray[1];
else
return anArray[n] * theProduct(anArray, n - 1);
}
访问anArray[1]
实际上是越界了
在:
如果大小为n
,则无法访问anArray[n]
,因为元素的计数范围为0
到n-1
。例如,在数组中,大小为3
,但元素具有索引:0、1、2
你的意思是:
else
return anArray[n] * theProduct(anArray, n - 1);
int产品(int数组[],int n){
如果(n)
您必须使用anArray[n-1]
而不是anArray[n]
,因为第三个元素的索引为3-1=2
不要返回anArray[0]*anArray[1]
而是anArray[0]
,因为您已经与anArray[1]
这是您的代码和更改(以及一些更小的更改):
#包括
使用名称空间std;
int产品(int数组[],int大小){
如果(size,我认为这应该可以正常工作。您应该使用数组的最大索引调用函数:
cout这里有一个替代版本,它使用一个helper函数和一个递归函数,递归地将数组分成两部分。它将使用log2(n)级递归,而不是n级递归,以减少堆栈开销
#include <iostream>
using namespace std;
int theProduct(int anArray[], int size) {
if (size <= 0)
return 0;
else if (size == 1) // base case
return anArray[0];
else
return anArray[size - 1] * theProduct(anArray, size - 1);
}
int main(int argc, const char * argv[])
{
int myArray[3] = {1, 2, 3};
cout << "The product of the array elements of myArray is "
<< theProduct(myArray,3) << endl;
return 0;
}
#包括
使用名称空间std;
int产品(INTA[],尺寸n);
int ProductR(int A[],尺寸低,尺寸端);
int main(int Argc,const char*argv[]
{
inta[]={1,2,3,4,5,6,7};
您的数组元素是否为myArray[0]、myArray[1]、myArray[2]。但不是myArray[3]。
int theProduct(int anArray[], int n) {
if (n <= 0)
return 0;
else if (n == 1)
return anArray[0];
else
return anArray[n-1] * theProduct(anArray, n - 1);
}
#include <iostream>
using namespace std;
int theProduct(int anArray[], int size) {
if (size <= 0)
return 0;
else if (size == 1) // base case
return anArray[0];
else
return anArray[size - 1] * theProduct(anArray, size - 1);
}
int main(int argc, const char * argv[])
{
int myArray[3] = {1, 2, 3};
cout << "The product of the array elements of myArray is "
<< theProduct(myArray,3) << endl;
return 0;
}
#include<iostream>
using namespace std;
int Product(int A[], size_t n);
int ProductR(int A[], size_t low, size_t end);
int main(int Argc, const char * argv[])
{
int A[] = {1, 2, 3, 4, 5, 6, 7};
cout << "The product of the array elements of myArray is " <<
Product(A, sizeof(A)/sizeof(A[0])) << endl;
return 0;
}
int Product(int A[], size_t n)
{
return ProductR(A, 0, n);
}
int ProductR(int A[], size_t low, size_t end)
{
if((end - low) == 0)
return 1;
if((end - low) == 1)
return A[low];
size_t mid = (low + end)/2;
return ProductR(A, low, mid) * ProductR(A, mid, end);
}