Question

In: Computer Science

Complete the function ConvertToDecadesAndYears to convert totalYears to decades and years. Return decades and years using...

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

Solutions

Expert Solution

  • Number of decades that can be fit into a year is the quotient of that year when divided by 10.
  • Number of remaining years that after the decades in a year is the remainder of that year when divided by 10.

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


Related Solutions

PYTHON: Complete these functions in the module: f2c, a function to convert Fahrenheit temperatures to Celcius....
PYTHON: Complete these functions in the module: f2c, a function to convert Fahrenheit temperatures to Celcius. c2f, a function to convert Celcius termperatures to Fahrenheit. Write f2c() and c2f() as functions that return a true value or False. In other words, these functions do not print — they return. A "true value", in this case, would be a number that corresponds to a temperature. If the user enters an invalid temperature, the function should return False. Create _main(), a function...
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
Complete this return statement so that it could be used in the function body offind_percentageto fulfill...
Complete this return statement so that it could be used in the function body offind_percentageto fulfill the requirements for the last step's program. Fill in the Blanks — Fill in the blanks return ________ /___________
Complete the following program skeleton. When finished, the program will ask the user for a length (in inches), convert that value to centimeters, and display the result. You are to write the function convert.
Complete the following program skeleton. When finished, the program will ask the user for a length (in inches), convert that value to centimeters, and display the result. You are to write the function convert. (Note: 1 inch = 2.54 cm. Do not modify function main.) #include #include using namespace std;// Write your function prototype here.int main(){ double measurement; cout ≪ "Enter a length in inches, and I will convert\n"; cout ≪ "it to centimeters: "; cin ≫ measurement; convert(&measurement); cout...
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Write a function using MATLAB that will accept a structure as an argument and return two...
Write a function using MATLAB that will accept a structure as an argument and return two cell arrays containing the names of the fields of that structure and the data types of each field. Be sure to check that the input argument is a structure, and generate an error message if it is not.
The work function for nickel is 5.15 eV. (a) Convert the value of the work function...
The work function for nickel is 5.15 eV. (a) Convert the value of the work function from electron volts to joules. J (b) Find the cutoff frequency for nickel. Hz (c) What maximum wavelength of light incident on nickel releases photoelectrons from the nickel's surface? nm (d) If light of energy 7.12 eV is incident on nickel, what is the maximum kinetic energy of the ejected photoelectrons? Give the answer in electron volts. eV (e) For photons of energy 7.12...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Smaller index Complete the following function according to its docstring using a while loop. The lists...
Smaller index Complete the following function according to its docstring using a while loop. The lists we test will not all be shown; instead, some will be described in English. If you fail any test cases, you will need to read the description and make your own test. def smaller_index(items): """ (list of int) -> int    Return the index of the first integer in items that is less than its index, or -1 if no such integer exists in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT