Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2: Modify the program above by adding a third structure named results w

ID: 656075 • Letter: E

Question

Exercise 2: Modify the program above by adding a third structure named
results which has two members area and perimeter. Adjust the rectan- gle structure so that both of its members are structure variables.

#include <iostream>

#include <iomanip>
using namespace std;


// This program uses a structure to hold data about a rectangle

struct dimensions
{
float length;
float width;

};

struct rectangle     // FILL IN CODE TO define a structure named rectangle which has
           // members length, width, area, and perimeter all of which are floats
{

float area;
float perimeter;
dimensions sizes;

};

int main()
{
    rectangle box;   // Fill IN CODE to declare a rectangle variable named box

    cout << "Enter the length of a rectangle: ";
  
  
    cin >> box.sizes.length;   // FILL IN CODE to read in the length member of box

    cout << "Enter the width of a rectangle: ";

    cin >> box.sizes.width;   // FILL IN CODE to read in the width member of box

   cout << endl << endl:

    box.area = (box.sizes.width) * (box.sizes.length);   // FILL IN CODE to compute the area member of box
  

    box.perimeter = 2 * ((box.sizes.length) + (box.sizes.width));   // FILL IN CODE to compute the perimeter member of box

   cout << fixed << showpoint << setprecision(2);

   cout << "The area of the rectangle is " << box.area << endl;   // FILL IN CODE to output the area with an appropriate message

   cout << "The perimeter of the recangle is " << box.perimeter << endl;   // FILL IN CODE to output the perimeter with an appropriate message
   
   return 0;
}

/*

Explanation / Answer

Please find the modified solution:(changes done are made to bold)

#include <iostream>
#include <iomanip> // setprecision

using namespace std;

// This program uses a structure to hold data about a rectangle
struct dimensions
{
float length;
float width;
};
struct Results
{
float value;
};

struct rectangle // FILL IN CODE TO define a structure named rectangle which has
// members length, width, area, and perimeter all of which are floats
{
Results area;
Results perimeter;

dimensions sizes;
};
int main()
{
rectangle box; // Fill IN CODE to declare a rectangle variable named box
cout << "Enter the length of a rectangle: ";
cin >> box.sizes.length; // FILL IN CODE to read in the length member of box
cout << "Enter the width of a rectangle: ";
cin >> box.sizes.width; // FILL IN CODE to read in the width member of box
cout << endl << endl;
box.area.value = (box.sizes.width) * (box.sizes.length); // FILL IN CODE to compute the area member of box
  
box.perimeter.value = 2 * ((box.sizes.length) + (box.sizes.width)); // FILL IN CODE to compute the perimeter member of box
cout << fixed << showpoint << setprecision(2);
cout << "The area of the rectangle is " << box.area.value << endl; // FILL IN CODE to output the area with an appropriate message

cout << "The perimeter of the recangle is " << box.perimeter.value << endl; // FILL IN CODE to output the perimeter with an appropriate message

return 0;
}