I wanted to make a recursive function for the multiplication of the elements from an array
If my array has 1 element and v[1] is 1 ,the program wil show me 4703488
My question is why?
#include<iostream>
using namespace std;
int prod(int n,int v[100])
{
if(n)
{
return prod(--n,v)*v[n];
}
return 1;
}
int main()
{
int v[100],n,i=1;
cout<<"n=";
cin>>n;
for(i=1;i<=n;i++)
cin>>v[i];
cout<<prod(n,v);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire