Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please explain why this code does not function the way is written and how can I

ID: 3807989 • Letter: P

Question

Please explain why this code does not function the way is written and how can I make it better to function.

Thank you

Code:

#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;

}

Explanation / Answer

#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;

}