Write a function that will calculate the either the area of a square or the volu
ID: 3865217 • Letter: W
Question
Write a function that will calculate the either the area of a square or the volume of a cube based on the parameters passed to it. If only length and width are passed, the height should default to one using a default parameter. The function should use the same formula to calculate both quantities. The result should be returned in a tuple with the second part being the units(we will use feet to be consistent between everyone's code). Ex: calc(2, 5) would return (10, 'sq ft') calc(2, 5, 3) would return (30, 'cu ft')Explanation / Answer
#include <iostream>
using namespace std;
double calc(double l, double w, double h = 1); //default parameter
int main()
{
cout<<calc(2,5)<<endl;
cout<<calc(2,5,3)<<endl;
return 0;
}
double calc(double l, double w, double h){
return l * w * h;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
10
30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.