#include <iostream> #include <iomanip> using namespace std; // This program uses
ID: 3604005 • Letter: #
Question
#include <iostream>
#include <iomanip>
using namespace std;
// This program uses a structure to hold data about a rectangle
// It calculates the area and perimeter of a box
// Erik Donovan
struct Dimension
{
float length;
float width;
};
// Fill in code to define a structure named dimensions that
// contains 2 float members, length and width
struct Rectangle
{
Results attributes;
Dimension sizes;
};
struct Results
{
float area;
float perimeter;
};
// Fill in code to define a structure named rectangle that contains
// 3 members, area, perimeter, and sizes. area and perimeter should be
// floats, whereas sizes should be a dimensions structure variable
int main()
{
Rectangle box;
// Fill in code to declare a rectangle structure variable named box.
cout << "Enter the length of a rectangle: ";
cin >> box.sizes.length;
// Fill in code to read in the length to the appropriate location
cout << "Enter the width of a rectangle: ";
cin >> box.sizes.width;
// Fill in code to read in the width to the appropriate location
cout << endl << endl;
box.attributes.area = (box.sizes.length*box.sizes.width);
// Fill in code to compute the area and store it in the appropriate
// location
box.attributes.perimeter = 2 * (box.sizes.length + box.sizes.width);
// Fill in code to compute the perimeter and store it in the
// appropriate location
cout << fixed << showpoint << setprecision(2);
cout << "The area of the rectangle is " << box.attributes.area << endl;
cout << "The perimeter of the recangle is " << box.attributes.perimeter
<< endl;
system("pause");
return 0;
}
Explanation / Answer
Hi,
I have fixed the issues and highlighted the code changes below.
#include <iostream>
#include <iomanip>
using namespace std;
// This program uses a structure to hold data about a rectangle
// It calculates the area and perimeter of a box
// Erik Donovan
struct Dimension
{
float length;
float width;
};
struct Results
{
float area;
float perimeter;
};
// Fill in code to define a structure named dimensions that
// contains 2 float members, length and width
struct Rectangle
{
Results attributes;
Dimension sizes;
};
// Fill in code to define a structure named rectangle that contains
// 3 members, area, perimeter, and sizes. area and perimeter should be
// floats, whereas sizes should be a dimensions structure variable
int main()
{
Rectangle box;
// Fill in code to declare a rectangle structure variable named box.
cout << "Enter the length of a rectangle: ";
cin >> box.sizes.length;
// Fill in code to read in the length to the appropriate location
cout << "Enter the width of a rectangle: ";
cin >> box.sizes.width;
// Fill in code to read in the width to the appropriate location
cout << endl << endl;
box.attributes.area = (box.sizes.length*box.sizes.width);
// Fill in code to compute the area and store it in the appropriate
// location
box.attributes.perimeter = 2 * (box.sizes.length + box.sizes.width);
// Fill in code to compute the perimeter and store it in the
// appropriate location
cout << fixed << showpoint << setprecision(2);
cout << "The area of the rectangle is " << box.attributes.area << endl;
cout << "The perimeter of the recangle is " << box.attributes.perimeter
<< endl;
system("pause");
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.