0

Sum of an array : What is a exact behavior of the sum of an array, if sum is not defined?

https://code.sololearn.com/cg39UsZGbz4U/?ref=app Is sum defined inside the library? What is the default value of sum? Can someone explain to me what happened inside this situation, please?

5th Apr 2017, 5:22 AM
The413th
The413th - avatar
4 Réponses
+ 14
G-G-Garbage value!
5th Apr 2017, 5:41 AM
Hatsy Rei
Hatsy Rei - avatar
+ 10
AFAIK, Since you didn't initialize sum with 0. Garbage value will be initialized to sum.
5th Apr 2017, 5:37 AM
Mr.Robot
Mr.Robot - avatar
+ 7
It takes some random values left in computer's secondary memory and uses them. It is always useful or I'd even say necessary to initially assign 0 to sum counting variables and 1 to product counting ones. For the same reason, if you declare an empty array it is better to do //int arr[n] = {}; https://code.sololearn.com/cYy6nsSNLl59/?ref=app
5th Apr 2017, 5:42 AM
Dinmukhamed Mailibay
Dinmukhamed Mailibay - avatar
0
Thanks everyone for your fast answers! @Hatsy I heard you well ahah @Mr.Robot I took note @Dimmukhamed Thank you for your detailed answer and advice. I now know the right behavior of an undefined int.
5th Apr 2017, 10:15 AM
The413th
The413th - avatar