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

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 whats the purpose of the while loop? To put it simply, its 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 youre 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!