Question 5 In many applications it is convenient to represent the days of a year
ID: 3909917 • Letter: Q
Question
Question 5 In many applications it is convenient to represent the days of a year using "day numbers". January 1st is day 1, January 2nd is day 2, and so on through December 31st which is either day 365 (if the year isn'ta leap year) or day 366 (if it is). Write a function that, given year and a day number, "returns" the corresponding month and day of the month (1 -first day of month, etc). Example: If your function is given 2007 as the year and 32 as the day number it should "return" 2 as the month and 1 as the day of month (the day number corresponds to February 1st). Assume that somebody else has provided you with a function that returns the number of days in a month. The prototype for this function is int daysInMonth (int month, int year)i // month 1 means January, etc. You may use this function in your function. You DO NOT have to write it. Your function must be consistent with the following sample call: cout year >> dayNumber; getMonthAndDay (year, dayNumber, month, dayofMonth) i coutExplanation / Answer
As the required values month and dayOfMonth are also passed as arguments we could safely assume they are Global variables. So in the function we have to just set the values.
When we read the values from the method that consumes this method we get the result on printing.
void getMonthAndDay (int year, int dayNumber, int month, int dayOfMonth){
int temp,i;
for(i=1;i<=12;i++) //iterating the month
{
if(dayNumber > daysInMonth(i,year)) // checking of the number of days are greater than the days in a month
{
temp=dayNumber-daysInMonth(i,year); //if yes , subtract the value
dayNumber=temp; // setting the new value after an iteration to No of days
}
else
{ // setting the values based on the iteration
month=i;
dayOfMonth=dayNumber;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.