Create a structure called Volume that uses three variables of type Distance to
model the volume of a room. Initialize a variable of type Volume to specific
dimensions, then calculate the volume it represents, and print out the result. To
calculate the volume, convert each dimension from a Distance variable to a
variable of type float representing feet and fractions of a foot, and then multiply the
resulting three numbers.
#include <iostream>
using namespace std;
struct distance{
float feet;
float inches;
};
struct Volume{
struct distance length;
struct distance width;
struct distance height;
float volume() {
return (length.feet + length.inches / 12) * (width.feet + width.inches / 12) * (height.feet + height.inches / 12);
}
};
int main(){
struct distance length = {4, 6}, width = {3, 5}, height = {5, 3};
struct Volume Vol;
Vol.length = length;
Vol.width = width;
Vol.height = height;
cout<<"Volume in cubic feet: "<<Vol.volume();
return 0;
}
Comments
Leave a comment