Why is their sum output negative? What would be their correct sum? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Why is their sum output negative? What would be their correct sum?

https://code.sololearn.com/cpmqBZc5LlL3/?ref=app

2nd Jan 2021, 2:56 PM
VIKAS
VIKAS - avatar
2 Respostas
+ 5
The reason being that the array consist of "long" type values and they do not fall into the "int" range. So there occurs an overflow and almost all values get "negative" values when they are considered as "int" types. So just change the "sum" and "x" types to long and it shall work fine.
2nd Jan 2021, 3:07 PM
Avinesh
Avinesh - avatar
2nd Jan 2021, 3:01 PM
The future is now thanks to science
The future is now thanks to science - avatar