fork download
  1. //Zachary Abdollahi CS1A Chapter 6, P. 369, #2
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE RECTANGLE AREA
  6.  *
  7.  * This program calculates the area based on user-supplied length and width
  8.  * values. The program is implemented using functions for modular design.
  9.  *
  10.  * Computation is based on the formula:
  11.  * Area = Length x Width
  12.  *
  13.  * INPUT
  14.  * length : Length of the rectangle in units
  15.  * width : Width of the rectangle in units
  16.  *
  17.  * OUTPUT
  18.  * area : Calculated area of the rectangle
  19.  *
  20.  ******************************************************************************/
  21. #include <iostream>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. // Function Prototypes
  26. double getLength();
  27. double getWidth();
  28. double getArea(double length, double width);
  29. void displayData(double length, double width, double area);
  30.  
  31. int main()
  32. {
  33. double length; // To hold the rectangle's length
  34. double width; // To hold the rectangle's width
  35. double area; // To hold the rectangle's area
  36.  
  37. // Get the rectangle's length from the user
  38. length = getLength();
  39.  
  40. // Get the rectangle's width from the user
  41. width = getWidth();
  42.  
  43. // Calculate the rectangle's area
  44. area = getArea(length, width);
  45.  
  46. // Display the rectangle's data
  47. displayData(length, width, area);
  48.  
  49. return 0;
  50. }
  51. /******************************************************************************
  52.   * getLength
  53.   * Prompts the user to enter the rectangle's length and returns *
  54.   * that value as a double.
  55.   *
  56.   *****************************************************************************/
  57. double getLength()
  58. {
  59. double length;
  60.  
  61. cout << "Enter the length of the rectangle: ";
  62. cin >> length;
  63.  
  64. return length;
  65. }
  66.  
  67. /*****************************************************************************
  68.   * getWidth *
  69.   * Prompts the user to get the rectangle's width and returns *
  70.   * That value as a double. *
  71.   ****************************************************************************/
  72. double getWidth()
  73. {
  74. double width;
  75. cout << "Enter the width of the rectangle: ";
  76. cin >> width;
  77.  
  78. return width;
  79. }
  80.  
  81. /****************************************************************************
  82.   * getArea *
  83.   * Accepts the rectangle's length and width as arguments and returns the *
  84.   * calculated area (length x width). *
  85.   ***************************************************************************/
  86. double getArea(double length, double width)
  87. {
  88. return length * width;
  89. }
  90.  
  91. /****************************************************************************
  92.   * displayData *
  93.   * Accepts the rectangle's length, width, and area as arguments and *
  94.   * displays them formatted on the screen. *
  95.   ***************************************************************************/
  96. void displayData(double length, double width, double area)
  97. {
  98. cout << fixed << showpoint << setprecision(2);
  99. cout << "\n--- Rectangle Details ---" << endl;
  100. cout << "Length : " << length << endl;
  101. cout << "Width : " << width << endl;
  102. cout << "Area : " << area << endl;
  103. }
Success #stdin #stdout 0s 5320KB
stdin
10.5
5.2
stdout
Enter the length of the rectangle: Enter the width of the rectangle: 
--- Rectangle Details ---
Length : 10.50
Width  : 5.20
Area   : 54.60