Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program named pa04.c that calculates and displays the weekday (day of

ID: 640392 • Letter: W

Question

Write a C program named pa04.c that calculates and displays the weekday (day of week) for any valid Gregorian date.1 To determine the weekday. you will first need to calculate the weekday for December 31 of the previous year. To calculate the weekday for December 31, use the following formula: (year-1) × 365 + pear-1]-[year-1. D'ear-1 100 The formula returns an integer value for the weekday, where 0 represents Sunday, 1 represents Monday, 2 represents Tuesday, and so on. Once you know the weekday for December 31, you simply calculate the days in the year before the month in question. Use a switch statement to make this calculation. (Hint: use case 12 first, and then fall into case 11,10... 2.) If the desired month is 12, add the number of days for November (30). If it is 11, add the number of days for October (31), and so on. If you do not use abreak between the months (i.e. an intentional fall-through case), the switch will add the days in each month before the current month. To this sum, add the day in the current month and then add the result to the weekday value for December 31. This number modulo seven is the weekday for the specified date The last thing to consider is whether or not the year is a leap year. If the desired date is after February you must add 1 to the weekday value. The following formula can be used to determine if the year is a leap year: (! (year % 4) &&(year % 100)) !! ! (year % 400) Notes: Awell-structured program design is required. A typical solution will use several functions besides main. Suggestions are functions to get data from the user, to calculate the day of the week, to print the result, etc. It is not unreasonable to have five or more functions for this assignment. . . Do not use any global constants or variables. . Whitespace should be ignored in user input, but the'/' separator character is required.

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int is_leap(int year){
   return ( !(year%4) &&(year%100)) || !(year%400);
}
int check_for_valid_date(int year, int month, int day){
   int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   days[1] = days[1] + is_leap(year);
   if(year < 1582 || (month < 1 || month > 12) || (day < 1 || day > days[month-1]))
   return 1;
   return 0;
}
int day_for_dec_31(int year, int month, int day){
   return ((year-1)*365+(year-1)/4 - (year-1)/100 + (year-1)/400)%7;
}
void get_date(char datestr[],int* month, int* year, int* day){
      char* token = strtok(datestr, "/");
      *month = atoi(token);
      token = strtok(NULL, "/ ");
      *day = atoi(token);
      token = strtok(NULL, "/ ");
      *year = atoi(token);
}
void display_day_of_week(char local[], int N)
{
   const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   printf("%s is a %s ", local,days[N]);
}
int main()
{
   char datestr[10];
   char local[10];
   int month;
   int day;
   int year;
   int weekday = 0;
   printf("This program calculates the weekday given a valid date.");
   printf(" Enter a date in the form M/D/YYYY: ");
   scanf("%s", datestr);
   strcpy(local,datestr);
   get_date(datestr, &month, &year, &day);
   if(check_for_valid_date(year, month, day)){
      printf("Invalid date. ");
   return 1;
   }
   else
   {
   weekday = day_for_dec_31(year, month, day);
   switch(month-1){
       case 12:weekday += 31;
       case 11:weekday += 30;
       case 10:weekday += 31;
       case 9: weekday += 30;
       case 8:weekday += 31;
       case 7:weekday += 31;
       case 6:weekday += 30;
       case 5:weekday += 31;
       case 4:weekday += 30;
       case 3:weekday += 31;
       case 2:weekday += 28+ is_leap(year);
       case 1:weekday += 31;
   }
   weekday += day;
   display_day_of_week(local, weekday%7);
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote