Beer and cookies for explanation... 😇 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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