![]() It is basic, but a common source of errors that may be hard to track down, especially for beginning programmers. However, if you start at zero, remember to be aware that the arrays still start at zero, and you will need an n + 1 size array to represent n elements if you use indexing starting at 1.Īlso be aware of your conditional in the for loop.įor (int i = 0 i < 10 i++) - Will loop 10 timesįor (int i = 1 i <= 10 i++) - Will loop 10 times In other words it may be beneficial to waste this first index if it adds clarity, however experienced programmers quickly learn to understand both styles. ![]() Experience and experimentation will teach you if this is worthwhile, in my experience, this can be helpful in things such as programming competitions (like the ICPC), where algorithms must be developed quickly with not much time for debugging, and the algorithms can be very complex, so the clarity is important. For example, if you are given a problem such as person 1 is x years old, person 2 is y years old, and so on, indexing using the given numbers may make it easier to give an answer if asked something such as who is the youngest person in the list. Starting at i = 1 is more natural for modeling many problems while designing algorithms. Our minds become use to working in this style. Personally I would say I use this style at least 90% of the time, as when modelling problems in the computer we mold them to use built in data structures, which usually start at 0. You should use i = 0 in situations where iterating starting at zero would be natural, as previously stated this could include array indexing or similar.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |