craft code to find a weekday name given a proleptic gregorian calendar date, without…
- User
craft code to find a weekday name given a proleptic gregorian calendar date, without using timestamps. *str weekday(in year,in month,int day) {
} test with str=weekday(2024,05,15); printf("%s", str); str=weekday(-2024,05,15); printf("%s", str); str=weekday(2021,05,15); printf("%s", str); str=weekday(-24,05,15); printf("%s", str); str = weekday(1985, 02, 12); printf("should be Tuesday:%s\n", str);
Assistant
#include <stdio.h> #include <stdlib.h>
// Helper function to calculate the day of the week for a given date int get_day_of_week(int year, int month, int day) { // January and February are counted as months 13 and 14 of the previous year if (month < 3) { month += 12; year -= 1; }
codeint h = (day + 13 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; return h;}
// Function to return the weekday name for a given date const char* weekday(int year, int month, int day) { static const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
codeint day_index = get_day_of_week(year, month, day); return days[day_index];}
int main() { char *str;
code// Test cases str = weekday(2024, 5, 15); printf("%s\n", str); str = weekday(-2024, 5, 15); printf("%s\n", str); str = weekday(2021, 5, 15); printf("%s\n", str); str = weekday(-24, 5, 15); printf("%s\n", str); str = weekday(1985, 2, 12); printf("should be Tuesday:%s\n", str); return 0;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.