Write a function to determine the day of the week a person was born given his or
ID: 3677316 • Letter: W
Question
Write a function to determine the day of the week a person was born given his or her birthday. The following steps should be used to find the day of the week corresponding to any date from 1901 through the present. In the following explanation, the following terms will be helpful. Assuming I type in my birthday as "6 10 1981": The month is 6. The day of the month is 10. The year is 1981. Find the number of years since 1900, and put it into a variable called yy. Simply subtract 1900 from the year to get this. Divide the number of years since 1900 by 4. Put the quotient in a variable called total. For example, if the person was born in 1983, divide 83 by 4 and store 20 in total. Also add the number of years since 1900 to total. Add the day of the month to total. Using the function month_offset() described in the following, find the "month offset" and add it to total. If the year is a leap year and if the month you are working with is either January or February, then subtract 1 from the total. You can use the function is_leap() provided to determine if the year is a leap year. Find the remainder when total is divided by 7. Pass this remainder to the function weekday_name() you wrote to determine the day of the week the person was born. Finally, build up a single String value containing the whole date (day of week, month, day, year). You'll need to use the function month_name() you wrote to show the month name rather than its number. Return that String value. Whew. Here's some code to get you started. Welcome to MCIS5103's fantastic birth-o-meter! All you have to do is enter your birthday, and it will tell you the day of the week on which you were born. Some automatic tests.... 12 10 2003 => Wednesday, December 10, 2003 2 13 1976 => Friday, February 13, 1976 2 13 1977 => Sunday, February 13, 1977 7 2 1974 => Tuesday, July 2, 1974 1 15 2003 => Wednesday, January 15, 2003 10 13 2000 => Friday, October 13, 2000 Now it's your turn! What's your birthday? Birthday (mm dd yyyy): 11 11 2010 You were born on Thursday, November 11, 2010! The function that gives you the "month offset" should be following the table below
Month
Month offset
1
1
2
4
3
4
4
0
5
2
6
5
7
0
8
3
9
6
10
1
11
4
12
6
anything else
-1
Month
Month offset
1
1
2
4
3
4
4
0
5
2
6
5
7
0
8
3
9
6
10
1
11
4
12
6
anything else
-1
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int month_offset(int m){ //Return the value of the offset for a given month
if(m==4 || m==7)
return 0;
else if(m==1 || m==10)
return 1;
else if(m==5)
return 2;
else if(m==8)
return 3;
else if(m==2 || m==3 || m==11)
return 4;
else if(m==6)
return 5;
else if(m==9 || m==12)
return 6;
else
return -1;
}
int is_leap(int y){ //return 1 if given year is a leap year otherwise return 0
if(y%4==0 || y%400==0 || y%100==0)
return 1;
else
return 0;
}
string weekday_name(int rem){ //Return the name of the day of the week depending on remainder given as input
if(rem==0)
return "Saturday";
else if(rem==1)
return "Sunday";
else if(rem==2)
return "Monday";
else if(rem==3)
return "Tuesday";
else if(rem==4)
return "Wednesday";
else if(rem==5)
return "Thursday";
else if(rem==6)
return "Friday";
}
string month_name(int m){ //return the name of the month of the year
if(m==1)
return "January";
else if(m==2)
return "February";
else if(m==3)
return "March";
else if(m==4)
return "April";
else if(m==5)
return "May";
else if(m==6)
return "June";
else if(m==7)
return "July";
else if(m==8)
return "August";
else if(m==9)
return "September";
else if(m==10)
return "October";
else if(m==11)
return "November";
else if(m==12)
return "December";
}
string to_str(int x){ //function to convert an integer to a string
ostringstream ss;
ss << x;
return ss.str();
}
string function(string input){
int month,day,year;
int i=0,flag=0,num=0;
int yy,total,rem;
char c;
while( i<input.size() ){ // parses the given string input to correspong month, day and year
c=input[i];
if(c==' '){
if(flag==0){
month=num;
num=0;
flag=1;
}
else{
day=num;
num=0;
}
}
else{
num=num*10+(c-'0');
}
i++;
}
year=num;
//Following steps are done based on steps given in question
yy=year-1900;
total=yy/4;
total =total+yy;
total=total+day;
total=total+month_offset(month);
if(is_leap(year) && (month==1 || month==2))
total=total-1;
rem=total%7;
string day_name=weekday_name(rem);
string ans=day_name+", "+month_name(month)+" "+to_str(day)+", "+to_str(year);
return ans;
}
int main(){
string s="10 13 2000";
cout<<function(s)<<endl;;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.