-3

Once the user enters a valid input (two integers separated by a space), print out the calendar in a format be similar to the output of the UNIX cal command. For example, if the user enters 03 2014, the output should be:

https://i.stack.imgur.com/dXG8q.jpg

I realized i asked a similar question to this prior, however i could not understand the answer at all. I feel that i should start from the basics so that i can learn myself how to print out a monthly calendar from the basis when given an input of a month and year.

I have provided code below that can only print out the following month of march, as we incorporate the the fact that each different month of each different year starts on a different day the code becomes ever more complicated so i was wondering how i should even begin to do this code.

Please nothing to advanced as my professor would not like me using things that are far ahead of my level of knowledge.

#include <stdio.h>

int main()
{
 int k, rmd;

 printf("     March 2014\n");
 printf(" Su Mo Tu We Th Fr Sa\n");

 for(k=1;k<32;++k){
     if(k==1){
         printf("                   %2d\n", k);
     }
     else if(k%7 == 1){
         printf(" %2d\n",k);
     }

     else{
         printf(" %2d",k);
     }
}
return 0;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
user3436065
  • 15
  • 1
  • 3
  • 7

3 Answers3

0

The basic approach is simple:

Find a year for which you know what happens (e.g. 2014-3-1 is a Saturday). Then consider what happens in a year with 365 days (which consists of 7*52 + 1 days...) and a 366 day year. After that you only need to figure out when leap years happen.

You can either find your start date for the first year you will ever consider, or also incorporate backwards computation (e.g. what happened 365 days before this) - the first is simpler, but introduces an additional constraint.

danielschemmel
  • 10,885
  • 1
  • 36
  • 58
0
Step 1:  Given the month and year, determine the day of the week for the 1st of the month
Step 1a: You already know how to do that, since I saw one of your previous posts
Step 2:  Compute how many spaces you need to print so that 1 is in the correct column
Step 3:  Print additional numbers until you reach Saturday
Step 3a: Print a newline character after printing the number for Saturday
Step 4:  Keep outputting numbers and newlines till you reach the end of the month
Step 4a: Remember that February has 29 days for leap years
Step 5:  Print a newline if the month didn't end on a Saturday
Step 5a: Print one more newline just for good measure
user3386109
  • 34,287
  • 7
  • 49
  • 68
0

you can read this to know how to get the day of week. then you can use this code to print the calender of a month month of year year.

d=2+ ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5)+ (365 * (year + 4800 -((14-month) / 12)))+ ((year + 4800 - ((14 - month) / 12)) / 4)- ((year + 4800 - ((14 - month) /12)) / 100)+ ((year + 4800 - ((14 - month) / 12)) / 400)- 32045;
d=d%7;
i=0;
while(i<d)
{
 printf("  ");
 i++;
}
//let dd be the number of days in month `month-year`
for(j=1;j<=dd;j++)
{
  if(d<7)                    //to get the sunday date to next line
  {
   printf("%d ",j);
   d++;
  }
  else
  {
   printf("\n");
   printf("%d ",j)
   d=1;
  }

}

it will print output in

sun mon tue wed thu fri sat
    1    2  3   4   5   6
7

form.

Community
  • 1
  • 1
LearningC
  • 3,182
  • 1
  • 12
  • 19