BOOK: COMPUTER SCIENCE A STRUCTURED PROGRAMMING APPROACH USING C BY BEHROUZ A.FO
ID: 3650844 • Letter: B
Question
BOOK: COMPUTER SCIENCE A STRUCTURED PROGRAMMING APPROACH USING C BY BEHROUZ A.FOROUZAN &RICHARD F . GILBERT . PAGE 300 , EXERCICE #60
write a program that , given a person's birth date(or any other date in th e gregorian calendar), will display the day of the week the person was born. To determine the day of the week , you will first need to calculate the day of the week for december 31 , use the following formula"((year-1)x365+((year-1)/4)+((year-1)/400))%7.
The formula determines the day based on the values as shown below
day0:sunday
day1:monday
day2:tuesday
day3:wednesday
day4:thursday
day5:friday
day6:saturday
Once you know the day for december 31,you simply calculate the days in the year before the minth 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).if it is 3,add the number of days for february(28).if it is 2,add the number of days for january(31).if you do not use a break between the months,the switch will add the days in each month before the current month.
To this figure ,add the day in the current month and then add the result to the day code for december 31.this number modulo seven is the day of the week.
There is one more refinement.if the current year is a leap year,and if the desired date is after february , you need to 1 to the day code. The following formula can be used to determine if the year is a leap year
"!(year % 4) && (year % 100)) ll !(year % 400)
Your program should have a function to get datat from the user , another to calculate the day of the week, and a third to print the result.
to test your program, run it with the followings dates:
a. February 28,1900 and March 1,1900
b.February 28,1955, and March 1,1955
c.February 28,1996, and march 1,1996
d.February 28,2000, and March 1,2000
e. December 31, 1996
f. The first and last dates of the current week
PS: PLEASE SHOW ALL THE WORKS AND ANSWERS
Explanation / Answer
please rate - thanks
your formula for dec 31 is wrong.
using 2012
doing integer arithmetic
((year-1)x365+((year-1)/4)+((year-1)/400))%7
((2011)x365+((2011)/4)+((2011)/400))%7
(734015+(502)+(5))%7
734522%7=5, which is friday. in actuallity dec 31, 2012 is a monday
this should get you started
#include<stdio.h>
#include <conio.h>
void getDate(int*,int*,int*);
int getDOW(int,int,int);
void print(int,int,int,int);
int main()
{int m,d,y,dow;
getDate(&m,&d,&y);
dow=getDOW(m,d,y);
print(m,d,y,dow);
getch();
return 0;
}
void getDate(int* m,int* d,int* y)
{
printf("Enter date: ");
printf("Enter month: ");
scanf("%d",m);
printf("Enter day: ");
scanf("%d",d);
printf("Enter year: ");
scanf("%d",y);
}
int getDOW(int m,int d,int year)
{int dec31,days=0;
dec31=((year-1)*365+((year-1)/4)+((year-1)/400))%7;
//printf("*********%d %d %d %d ",m,d,year,dec31);
switch(m-1)
{
case 11: days+=30 ;
case 10: days+=31 ;
case 9: days+=30 ;
case 8: days+=31 ;
case 7: days+=31 ;
case 6: days+=30 ;
case 5: days+=31 ;
case 4: days+=30 ;
case 3: days+=31 ;
case 2: days+=31 ;
case 1: days+=31 ;
}
days=days+d+dec31;
if((!(year % 4) && (year % 100)) || !(year % 400) &&m>3)days++;
return days%7;
}
void print(int m,int d,int y,int day)
{printf("%d/%d/%d is on a ",m,d,y);
if(day==0)
printf("Sunday ");
else if(day==1)
printf("Monday ");
else if(day==2)
printf("Tuesday ");
else if(day==3)
printf("Wednesday ");
else if(day==4)
printf("Thursday ");
else if(day==5)
printf("Friday ");
else
printf("Saturday ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.