fork download
  1. //Amari Mosley CSC5 Chapter 6, P. 369, #2
  2. //
  3. /*****************************************************************
  4.  *
  5.  * Compute Rectangle Area
  6.  * _______________________________________________________________
  7.  * This program asks the user for the length and width of a
  8.  * rectangle, then computes and displays its area.
  9.  *
  10.  * Computation is based on the formula:
  11.  * area = length * width
  12.  * _______________________________________________________________
  13.  * INPUT
  14.  * length : the rectangle's length
  15.  * width : the rectangle's width
  16.  *
  17.  * OUTPUT
  18.  * area : the rectangle's area
  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; // length of the rectangle
  34. double width; // width of the rectangle
  35. double area; // area of the rectangle
  36.  
  37. // Get the rectangle's dimensions
  38. length = getLength();
  39. width = getWidth();
  40.  
  41. // Calculate the area
  42. area = getArea(length, width);
  43.  
  44. // Display the results
  45. displayData(length, width, area);
  46.  
  47. return 0;
  48. }
  49.  
  50.  
  51. //getLength
  52. // Asks the user for the rectangle's length and returns it.
  53.  
  54. double getLength()
  55. {
  56. double length;
  57.  
  58. cout << "Enter the rectangle's length: ";
  59. cin >> length;
  60.  
  61. return length;
  62. }
  63.  
  64.  
  65. // getWidth
  66. // Asks the user for the rectangle's width and returns it.
  67.  
  68. double getWidth()
  69. {
  70. double width;
  71.  
  72. cout << "Enter the rectangle's width: ";
  73. cin >> width;
  74.  
  75. return width;
  76. }
  77.  
  78.  
  79. // getArea
  80. // Receives the length and width and returns the area.
  81.  
  82. double getArea(double length, double width)
  83. {
  84. return length * width;
  85. }
  86.  
  87.  
  88. // displayData
  89. // Receives the length, width, and area and displays them.
  90.  
  91. void displayData(double length, double width, double area)
  92. {
  93. cout << fixed << setprecision(2);
  94. cout << "\nRectangle Data" << endl;
  95. cout << "Length: " << length << endl;
  96. cout << "Width: " << width << endl;
  97. cout << "Area: " << area << endl;
  98. }
Success #stdin #stdout 0s 5296KB
stdin
5
4
stdout
Enter the rectangle's length: Enter the rectangle's width: 
Rectangle Data
Length: 5.00
Width:  4.00
Area:   20.00