From dd75cd75e74217ba004af546bae987086cac2212 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Thu, 26 Oct 2023 20:24:04 +0100 Subject: [PATCH] complete c21 --- Exercises/C21/ex1.c | 20 ++++++++++++++++++++ Exercises/C21/ex2.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 Exercises/C21/ex1.c create mode 100644 Exercises/C21/ex2.c diff --git a/Exercises/C21/ex1.c b/Exercises/C21/ex1.c new file mode 100644 index 0000000..b64fe16 --- /dev/null +++ b/Exercises/C21/ex1.c @@ -0,0 +1,20 @@ +#include +#include + +int main(int argc, char *argv[]) { + int age; + char name[10]; + + if ( argc != 3 ) + { + printf ("USAGE: %s name age\n",argv[0]); + exit (0); + } + + sscanf(argv[1],"%s",name); + sscanf(argv[2],"%d",&age); + + printf ("age=%d name=%s\n", age, name); + + return 0; +} diff --git a/Exercises/C21/ex2.c b/Exercises/C21/ex2.c new file mode 100644 index 0000000..3aa50ba --- /dev/null +++ b/Exercises/C21/ex2.c @@ -0,0 +1,44 @@ +// Modified from C10 Ex5 + +#include +#include + +int date_to_day(int year, int month, int day) { + if (month < 3) { + month += 12; + year -= 1; + } + + return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7; +} + +void print_day_of_week(int day) { + switch (day) { + case 0: printf("Monday\n"); break; + case 1: printf("Tuesday\n"); break; + case 2: printf("Wednesday\n"); break; + case 3: printf("Thursday\n"); break; + case 4: printf("Friday\n"); break; + case 5: printf("Saturday\n"); break; + case 6: printf("Sunday\n"); break; + } +} + +int main(int argc, char *argv[]) { + int rc, year, month, day, nd; + + if ( argc != 4 ) + { + printf ("USAGE: %s year month day\n",argv[0]); + exit (0); + } + + sscanf(argv[1],"%d",&year); + sscanf(argv[2],"%d",&month); + sscanf(argv[3],"%d",&day); + + nd = date_to_day(year, month, day); + print_day_of_week(nd); + + return 0; +}