IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Define command and index
- This topic is empty.
-
AuthorPosts
-
November 14, 2021 at 7:12 am #52
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
/* day_mon1.c -- prints the days for each month */ #include <stdio.h> #define MONTHS 12 int main(void) { int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31}; int index; for (index = 0; index < MONTHS; index++) printf("Month %d has %2d days.\n", index +1, days[index]); return 0; }
[/dm_code_snippet]
What I could not understand is how MONTHS values are assigned to index. No where in the code I could see index = values of MONTHS (or something similar).
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (index = 0; index < MONTHS; index++)
[/dm_code_snippet]
For loop above only seems to indicate index starts counting with 0 and continues till 11. But how it is taken granted that days [index] will be counter for the values of days[MONTHS].
On second look, it appears that days variable (or should I call array? if days is the name of array, what should I call MONTHS?) takes {31,28,31,30,31,30,31,31,30,31,30,31}; Next, these 12 values can be assigned to any other named array, which in this case index.
Reply
https://edstem.org/us/courses/176/discussion/849748?[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.