Hello. I'm taking the C++ tutorials found at cprogramming.com, and one of the example codes for a lesson on arrays kind of confused me.
This is the code and resulting output: http://codepad.org/VIMFsa9v
I started fiddling around with it because I didn't understand the logic in it, and after a few tries I decided to give up and try to produce the same output from scratch.
Here's my version: http://codepad.org/GIGEnZgZ
Could anyone tell me *why* they did it that way? Mine seems a bit simpler, but I don't know if their version might have any advantages in a different situation.
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Programs which code you pasted do practically same thing, your program does assigning and printing out simultaneously and their program has this steps separated. In general, sometimes there are situations where you can assign values and perform operations on them simultaneously, sometimes processing part is to complex to merge it with assigning/preparing part.
I see. So in larger programs, separating the two reduces the chance of errors, then. From what you say I take it mine wasn't faulty in logic, though ( quite a relief! ). I was worried that mine might've been a fluke with some rather horrendous flaw that I narrowly escaped by sheer luck. I'm afraid it's happened before.
Thank you so much for replying.
Another quick question: is naming the array "array" okay, too? I was told to stay away from... 'reserved' key words like "int" and "char", and though I don't know how "array" might be used, Microsoft Visual Studio did highlight it, and the program ran flawlessly despite that.
The word "array" isn't a keyword in C/C++, if it had been, your program wouldn't have compiled. So, you can name array as "array" and this is not a fault. Of course you should often have better names for your arrays - such names that are better describing destination of particular ones.
Not the answer you are looking for? Search for more explanations.
By the way, the reason that there is no reserved word "array" is that we don't use keywords to declare arrays, we just declare pointer and this can be pointer to space allocated earlier - statically or dynamically, then, we only use offset from this pointer to get access to particular elements (this is the reason for numbering of elements starting from zero).
when naming is always good to try camelCase naming style.. is pretty helpfu
l and is better to understand.
Yes, that's why I didn't really understand why 'array' was highlighted. Thank you =)
Thanks kuro, I'll try camelCasing. I'm still trying to figure out a consistent naming scheme for myself since it seems to vary from person to person.