Object

Write a program that demonstrates the process of a structure within a structure.

Algorithm

  1. Declare a structure with the tag coord.
  2. Declare integer variables x and y as the members of structure.
  3. Declare another structure with the tag rectangle that contains variables topleft and bottomrt of data type struct coord.
  4. Declare integer variables length and width.
  5. Declare a variable mybox of data type struct rectangle.
  6. Read structure members mybox.topleft.x, mybox.topleft.y and mybox.bottomrt.x, mybox.bottomrt.y.
  7. width = mybox.bottomrt.x – mybox.topleft.x.
  8. length = mybox.bottomrt.y – mybox.topleft.y.
  9. Write width and length.
  10. Exit.

Flowchart

C++ Source Code

// program 101
#include<iostream>
using namespace std;

struct coord
{
  int x;
  int y;
};

struct rectangle
{
  coord topleft;
  coord bottomrt;
};

int main()
{
  int length, width;
  rectangle mybox;

  cout << "Enter the top left x and y coordinates: ";
  cin >> mybox.topleft.x >> mybox.topleft.y;

  cout << "Enter the bottom right x and y coordinates: ";
  cin >> mybox.bottomrt.x >> mybox.bottomrt.y;

  width = mybox.bottomrt.x - mybox.topleft.x;
  length = mybox.bottomrt.y - mybox.topleft.y;

  cout << "\n\nThe top left x coordinate: " << mybox.topleft.x;
  cout << "\nThe top left y coordinate: " << mybox.topleft.y;
  cout << "\n\nThe bottom right x coordinate: " << mybox.bottomrt.x;
  cout << "\nThe bottom right y coordinate: " << mybox.bottomrt.y;

  cout << "\n\nThe width of the given rectangle: " << width << " units";
  cout << "\nThe length of the given rectangle: " << length << " units";
  cout << "\n\nThe area of the given rectangle: " << length * width << " sq.units";

  return 0;
}

C Source Code

/*program 101*/
#include<stdio.h>
int main()
{
  int length, width;
  struct coord
  {
   int x;
   int y;
  };
  struct rectangle
  {
   struct coord topleft;
   struct coord bottomrt;
  }mybox;
  printf("Enter the top left x and y coordinates: ");
  scanf("%d%d", &mybox.topleft.x, &mybox.topleft.y);
  printf("Enter the bottom right x and y coordinates: ");
  scanf("%d%d", &mybox.bottomrt.x, &mybox.bottomrt.y);

  width = mybox.bottomrt.x - mybox.topleft.x;
  length = mybox.bottomrt.y - mybox.topleft.y;

  printf("\n\nThe top left x coordinate: %d", mybox.topleft.x);
  printf("\nThe top left y coordinate: %d", mybox.topleft.y);
  printf("\n\nThe bottom right x coordinate: %d", mybox.bottomrt.x);
  printf("\nThe bottom right y coordinate: %d", mybox.bottomrt.y);

  printf("\n\nThe width of the given rectangle: %d units", width);
  printf("\nThe length of the given rectangle: %d units", length);
  printf("\n\nThe area of the given rectangle: %d sq.units", length*width);
  
  return 0;
}

Output

Enter the top left x and y coordinates: 8 20
Enter the bottom right x and y coordinates: 51 57

The top left x coordinate: 8
The top left y coordinate: 20

The bottom right x coordinate: 51
The bottom right y coordinate: 57

The width of the given rectangle: 43 units
The length of the given rectangle: 37 units

The area of the given rectangle: 1591 sq.units

Design a site like this with WordPress.com
Get started