+ 1

Help me find my mistake

I don't know why, but it doesn't show the final value of the mathematicial example. Help me, please: https://code.sololearn.com/co65U1LAMyc0/#cpp

7th May 2018, 1:38 PM
In Code We Trust
In Code We Trust - avatar
6 Respostas
+ 2
In your code, factorial(0) is not defined, so put if(q<=1) instead of if(q==1) inside the factorial function to define it. And also, the iterator in the for loop goes until n, not n-1, so change k<n to k<=n or k<n+1(i recommend the first) in the condition of the for loop.
7th May 2018, 2:09 PM
Bebida Roja
Bebida Roja - avatar
+ 6
if (q==0 || q==1) return 1; else return q*factorial(q-1); Factorial of 0 is also 1. Change your function as above to consider 0 (since k starts from 0)
7th May 2018, 2:04 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
Khidoyat Tashmatov The value held by uninitialised local variables will vary from compiler to compiler. It is highly encouraged to initialize z to 0, knowing what you are doing. VS for instance, will warn you about using an uninitialised local variable. https://stackoverflow.com/questions/6032638/default-variable-value
7th May 2018, 2:32 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Shamima Yasmin, thank you very much! you helped me again :)
7th May 2018, 2:08 PM
In Code We Trust
In Code We Trust - avatar
+ 1
Bebida Roja now it definitely works, thank you!
7th May 2018, 2:12 PM
In Code We Trust
In Code We Trust - avatar
0
Hatsy Rei, by default, z equals to zero
7th May 2018, 2:27 PM
In Code We Trust
In Code We Trust - avatar