Beer and cookies for explanation... šŸ˜‡ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 5

Beer and cookies for explanation... šŸ˜‡

Hello fellow sololearners! ā˜ŗ I was wandering if someone is willing to explain to me the logic behind this code that I got in a challenge. The best explanation gets free cookies and a beer from me šŸ˜‰ Thank you in advance. int[] pixels = new int[10000]; for (int y = 0; y < 100; y++) { Ā Ā  for (int x = 0; x < 100; x++) { index = x + y * 100; pixels[index] = x + y; Ā Ā  } } System.out.print(pixels[150]); Correct answer by Sololearn is 51.

10th Dec 2017, 6:28 PM
Vladan Stojanovic
Vladan Stojanovic - avatar
4 Respostas
+ 3
The only way to get pixels[150] is when y=1 and x=50. So y*100=100 + x=50 you get the array index of 150 and you store there x+y=50+1=51.. I hope it is clear
10th Dec 2017, 6:41 PM
Kostas Batz
Kostas Batz - avatar
+ 2
sure, beer's on me! here - šŸŗ pm me when you come to Belgrade, I'll buy you a beer ;) let me try to understand: the array location of 150 holds the value of 51, correct? .... right, now i feel stupid šŸ˜„ I just saw it liks this: 150 = x + y*100 pixels = x + y in our language this is called system of equations with two variables. real easy. hidden in code. damn it. :) thanks for the effort.
11th Dec 2017, 4:50 PM
Vladan Stojanovic
Vladan Stojanovic - avatar
+ 2
didn't look that easy during a challenge... šŸ˜ 
11th Dec 2017, 4:51 PM
Vladan Stojanovic
Vladan Stojanovic - avatar
+ 1
do I get the beer?
10th Dec 2017, 6:42 PM
Kostas Batz
Kostas Batz - avatar