Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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).

OpenStudy (anonymous):

when naming is always good to try camelCase naming style.. is pretty helpfu l and is better to understand.

OpenStudy (anonymous):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!