Struct and Direct Field Indexing Write a function called ConvertToDecadesAndYears to convert totalYears to decades and years. The decades and years should be returned using a struct that has two fields, decades for the number of decades, and years for the number of years.

Respuesta :

Answer:

Please refer to the attached image for the solution code.

Explanation:

To return a struct data type from a function, we need to define the struct data in advance (Line 1 - 4). As required by the question, the struct duration consists of two members, decade & year.

Next we can proceed to define the function ConvertToDecadesAndYears() that take one input parameter, totalYear (Line 6). Please note the function is also defined with a return data type, struct duration.

Next, we calculate the year, y,  by calculating modulus (remainder) of totalYear when divide it by 10 (Line 9) and calculate the decade, d,  by using floor function to round down the division result of totalYear by 10.

We can then assign the y & d to the member of the struct data, d1, (Line 12 & 13) and return it as output (Line 15).

Ver imagen teobguan2019