fork download
  1. // Amari Mosley CSC5 Chapter 5, P. 294, #3
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE OCEAN LEVELS
  6. *______________________________________________________________
  7. * This program displays a table showing the number of millimeters
  8. * that the ocean will have risen each year for the next 25 years,
  9. * assuming a constant rise rate of 1.5 millimeters per year.
  10. *
  11. * INPUT
  12. * (None) : Program uses fixed constants for rate and years
  13. *
  14. * OUTPUT
  15. * Year & Rise : Table of years (1-25) and corresponding cumulative
  16. * ocean level rise in millimeters
  17. *
  18. **************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22.  
  23. using namespace std;
  24.  
  25. int main() {
  26. // CONSTANTS
  27. const double RISE_RATE = 1.5; // Millimeters per year
  28. const int NUM_YEARS = 25; // Number of years to project
  29.  
  30. // Display table header
  31. cout << setw(6) << "Year" << setw(20) << "Rise (millimeters)" << endl;
  32. cout << "-----------------------------------------" << endl;
  33.  
  34. // Loop through the next 25 years and calculate cumulative rise
  35. for (int year = 1; year <= NUM_YEARS; year++) {
  36. double cumulativeRise = year * RISE_RATE;
  37. cout << setw(6) << year << setw(20) << fixed << setprecision(1) << cumulativeRise << endl;
  38. }
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
  Year  Rise (millimeters)
-----------------------------------------
     1                 1.5
     2                 3.0
     3                 4.5
     4                 6.0
     5                 7.5
     6                 9.0
     7                10.5
     8                12.0
     9                13.5
    10                15.0
    11                16.5
    12                18.0
    13                19.5
    14                21.0
    15                22.5
    16                24.0
    17                25.5
    18                27.0
    19                28.5
    20                30.0
    21                31.5
    22                33.0
    23                34.5
    24                36.0
    25                37.5