Arrays | Sololearn: Learn to code for FREE!

+2

Arrays

please I need more explanation about this code I could not understand it please someone should help me #include <iostream> using namespace std; int billy[]={16,2,77,40,12071}; int n,result=0; int main() { for (n=0;n<5;n++) { result +=billy[n]; } cout <<result; return 0; }

5/6/2020 1:43:46 PM

Werleed auta

9 Answers

New Answer

+3

so I know absolutely zip about this language, but I am curious about the comments and wondering if I understand with my limited programming knowledge: Ipang Martin Taylor so the global scope concern is because the variables are defined outside the scope of the for block? again without any understanding, I am curious why it would sum all integers in the array with the n<5 statement in the for loop. my assumption just looking at the code would make me believe the answer is 2? 😳 [edit]: OK retract that... I see the n<5 is defining the range of the interation. 👍 this is probably insanely basic, but my curiousity made me comment.

+3

Ipang thanks for that info! I am contemplating learning C. I will probably never use it before I retire, but I tinker with electronics and I figure learning C/Cpp might help me understand some of the open source I occassionally look at for projects. Something to keep my mind active?! 😉 I was actually really surprised I could follow it, because that was literally my first attempt to ever try to follow a piece of C. looks like SL may get me to go thru another tutorial. 👍

+3

Werleed auta I highly recommend finishing the SoloLearn tutorial. I have not taken it either, but if you work thru the tutorial and use the 'try it' buttons to experiment, I am 99% sure code like this will be completely obvious after half way thru the tutorial! 😉👍 Happy Coding!

+2

In short, that code sum all the elements of a global int array <billy> into variable <result>, which also is defined and initialized on the global space. Not sure what confused you here, so I'd appreciate it if you can come clearer on the problem.

+1

wow thank you @Ipang I just checked it and that's true it's sum all the elements

+1

Code Crasher As per my understanding, something is said to belong in global scope when their declaration/definition is not inside of a function. Here <billy> array is defined outside main function (the only function we see). You got it right though, the <n> in the for-loop refers to the index of array elements (a range), to be added into <result> variable. Curiosity is always welcome, no probs 👌

+1

Code Crasher Don't know that much about C/C++ myself, but a lot had said/written something around those languages and electronics or robotics. So I guess it makes just the sense to learn, even if it was just the basics. Keep crashing bruh 😁

0

No problem buddy 👌 Keep coding and stay safe 👍

0

Whoever wrote that code needs a clip around the ear. There is no excuse for those global variables. That's just lazy programming and sooner or later it's going to lead to problems. If the author of that code doesn't understand about variable scope they shouldn't be allowed near a keyboard.