- This topic is empty.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
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
/* 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;
}
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).
for (index = 0; index < MONTHS; index++)
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]