Tuesday, December 6, 2016

Programming Challenge 5.4 - Calories Burned

/* Calories Burned - Running on a particular treadmill you burn 3.6 calories per
   minute. This program uses a loop to display the number of calories burned after
   5, 10, 15, 20, 25, and 30 minutes. */

#include "Utility.h"

int main()
{
    /* Constants: Calories burned, Time */
    const double CALORIES_BURNED = 3.6;
    const int TIME = 30;

    /* Display: Information */
    cout << "The data stored shows, that you burn 3.6 cal. per minute on this\n"
         << "treadmill. This table shows you, how many calories you will burn\n"
         << "if you train 5, 10, 15, 20 or 30 minutes every day.\n\n";

    cout << "\tMinutes " << "\t\tYou burn\n"
         << "-------------------------------------------\n";

    /* For loop: Calories burned is declared and initialized with a value of 1.
       As long as the value of calories burned is lower than or equal to TIME (30),
       the outer loop will iterate. */
    for (double caloriesBurned = 1; caloriesBurned <= TIME; caloriesBurned++)
    {
        /* Nested for loop: Timer is declared and initialized to 5. It increments
           6 times, or - as long as the value of timer is lower than or equal to
           TIME (30). */
        for (int timer = 5; timer <= TIME; timer += 5)
        {
            /* Calulation: In each iteration of the inner loop, CALORIES_BURNED
               is multiplied by the value of timer (5). The result is stored in
               the variable caloriesBurned. */
            caloriesBurned = CALORIES_BURNED * timer;
           
            /* Display: Formatted output, The number of calories burned after 5,
            10, 15 ... 30 minutes */
            cout << fixed << showpoint << setprecision(2);

            cout << "After:  " << setw(2) << right << timer << "\t\t\t"
                << setw(6) << caloriesBurned << " cal.\n" ;
        }
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment