C++ Hello I need help with the following code. Analyzing the code, explain why i
ID: 3810476 • Letter: C
Question
C++
Hello I need help with the following code. Analyzing the code, explain why it failed, show the specific lines of code with an explanation of why. And how to fix it.
Relevant code is the one that is not in bold letters.
So what’s the purpose of the while loop? To put it simply, it’s designed to get the number of years from the number of days since 1980 as well as a remainder of days out of the current year. Keep in mind that you’re never supposed to retry the loop without committing some sort of action which will ultimately exit the loop:
1. year is set to ORIGINYEAR. Since ORIGINYEAR is 1980, any addition to year is added on top of 1980. This is fine because the hardware is passing the number of days since January 1, 1980.
2. Is the number of days greater than 365? If so, proceed. Otherwise, skip to number 6.
3. Is the current year a leap year? If so, proceed. Otherwise, subtract 365 from the number of days, add 1 to the number of years, and skip to number 5.
4. Is the number of days greater than 366? If so, subtract 366 from the number of days, add 1 to the number of years, and proceed.
5. retry number 2.
6. This is the loop exit point – success if you get here
#define ORIGINYEAR 1980
BOOL ConvertDays(UINT32 days, SYSTEMTIME* lpTime)
{
int dayofweek, month, year;
UINT8 *month_tab;
//Calculate current day of the week
dayofweek = GetDayOfWeek(days);
year = ORIGINYEAR;
while (days > 365)
{
if (IsLeapYear(year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
// Determine whether it is a leap year
month_tab = (UINT8 *)((IsLeapYear(year))? monthtable_leap : monthtable);
for (month=0; month<12; month++)
{
if (days <= month_tab[month])
break;
days -= month_tab[month];
}
month += 1;
lpTime->wDay = days;
lpTime->wDayOfWeek = dayofweek;
lpTime->wMonth = month;
lpTime->wYear = year;
return TRUE;
}
Thank you in advance
Explanation / Answer
Actually , the existing code will be fine for all the cases except exact case of complete days of an year .
Let me give with an example ,
If we give number of days input as '366' , then the system never come out from the while loop .Because , we started year from 1980 and this is a leep year . So, if we give 366 for number of days input then the problem at below statement from existing code .
while (days > 365) //enter into this
{
if (IsLeapYear(year)) //enter into this
{
if (days > 366) //but it will not enter into this, since the number if days is 366
{
days -= 366;
year += 1;
}
}
else //it entered into if condition . so, it will not come for else part
{
days -= 365;
year += 1;
}
} //so, finally there is no termination for while loop . some times , it will impact to the system hang also
So, please try with this below added conditions for this case, then it will take all the cases as you desire
while (days > 365)
{
if (IsLeapYear(year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
else if(days == 366) //added this condition with out increasing year , because it is closer of current year (leap year) days
days -= 366;
}
else
{
if(days == 365) //added this condition with out increasing year , because it is closer of current year (non leap year) days
days -= 365 ;
else
{
days -= 365;
year += 1;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.