In: Computer Science
Complete the function ConvertToDecadesAndYears to convert totalYears to decades and years. Return decades and years using the ElapsedDecadesYears struct. Ex: 26 years is 2 decades and 6 years.
#include <iostream>
using namespace std;
struct ElapsedDecadesYears {
int decadesVal;
int yearsVal;
};
ElapsedDecadesYears ConvertToDecadesAndYears(int totalYears)
{
ElapsedDecadesYears tempVal;
/* Your code goes here */
}
int main() {
ElapsedDecadesYears elapsedYears;
int totalYears;
cin >> totalYears;
elapsedYears = ConvertToDecadesAndYears(totalYears);
cout << elapsedYears.decadesVal << " decades and " << elapsedYears.yearsVal << " years" << endl;
return 0;
}
Code screenshot:
Output:
Code to copy:
#include <iostream>
using namespace std;
struct ElapsedDecadesYears {
int decadesVal;
int yearsVal;
};
ElapsedDecadesYears ConvertToDecadesAndYears(int totalYears)
{
ElapsedDecadesYears tempVal;
// we can initialize the value of decadesVal with quotient of
totalYears / 10
tempVal.decadesVal = totalYears/10;
// we can initialize the value of yearsVal with remainder of
totalYears % 10
tempVal.yearsVal = totalYears%10;
// now we can return the structure object tempVal
return tempVal;
}
int main() {
ElapsedDecadesYears elapsedYears;
int totalYears;
cin >> totalYears;
elapsedYears = ConvertToDecadesAndYears(totalYears);
cout << elapsedYears.decadesVal << " decades and "
<< elapsedYears.yearsVal << " years" <<
endl;
return 0;
}