Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Books
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Short Programs


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Books

  • C++ (The Book)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Figured
    2 weeks 1 day ago
  • I'm guessing at this stage
    2 weeks 1 day ago
  • WordPress MU?
    2 weeks 2 days ago
  • Thanks
    8 weeks 3 days ago
  • I'm running the conversion
    10 weeks 1 hour ago
  • Can't reproduce
    10 weeks 1 day ago
  • Strange one
    10 weeks 1 day ago
  • No customer support
    11 weeks 4 days ago
  • Came to the rescue
    13 weeks 2 days ago
  • Permalink - %postname%
    13 weeks 2 days ago

New forum topics

  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
  • index.php?
  • where do i download?
more

Who's online

There are currently 0 users and 0 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Calculate Pace (Running Program)

  • View
  • Revisions
Submitted by Steve on Sun, 23 Dec, 2007 - 21:39
  • C++
  • distance
  • pace
  • structure
  • time

During week 5 of study, learning functions and in particular references passed to and from functions. The following program, in a way, cemented some of the stuff studied. It wasn't a part of the book but since I'm interested in running, I thought I'd write a little program that calculates the average pace of a run given the distance (km) and duration (time) of the run.

// pace-2.cpp  --  To calculate pace of a run
// author : Steven Taylor
// 21 Dec, 2007.

#include <iostream>
using namespace std;
const int SECS_IN_MIN = 60;
const int MINS_IN_HOUR = 60;
const int SECS_IN_HOUR = 3600;

struct hms
{
    int hour;
    int min;
    int sec;
};
const hms & pace_is(double distance, hms  & duration, hms & rate);

int main()
{
    double distance;
    hms duration;
    hms perkm = {0,0,0};
   
    cout << "Enter distance (km's) run: ";
    while (!(cin >> distance))
    {
        cout << "\nMust enter a number - re-enter: ";
        cin.clear();
        while (cin.get() != '\n')
            continue;
    }
    cout << "\nEnter duration of run - firstly - hours: ";
    while (!(cin >> duration.hour))
    {
       cout << "\nMust be an integer number - re-enter - hours: ";
       cin.clear();
       while (cin.get() != '\n')
           ;
    }
    cout << "\nEnter minutes (0-59): ";
    while (!(cin >> duration.min) || duration.min >= MINS_IN_HOUR)
    {
        cout << "\nMinutes must be 0 - 59, re-enter - minutes: ";
        cin.clear();
        while (cin.get() != '\n')
            ;
    }
    cout << "\nEnter seconds (0 - 59): ";
    while (!(cin >> duration.sec) || duration.sec >= SECS_IN_MIN)
    {
        cout << "\nSeconds must be 0 - 59, re-enter - seconds: ";
        cin.clear();
        while (cin.get() != '\n')
            ;
    }

    perkm = pace_is(distance,duration,perkm);
   
    cout << "\nThe pace per km is : ";
    if (perkm.hour > 0)
        cout << perkm.hour << ":";
    cout << perkm.min << ":" << perkm.sec << endl;
   
    // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    cin.get();
    return 0;
}

And now the function pace_is()

const hms & pace_is(double distance, hms & d, hms & rate)
{
       
    int total_secs = (d.hour * SECS_IN_HOUR) + (d.min * SECS_IN_MIN) + d.sec;
    double secs_per_km = total_secs / distance;
       
 
    if (secs_per_km > SECS_IN_HOUR)
    {
        rate.hour = secs_per_km / SECS_IN_HOUR;
        secs_per_km = (int)secs_per_km % SECS_IN_HOUR;
    }
    if (secs_per_km > 0)
    {
        rate.min = secs_per_km / MINS_IN_HOUR;
        rate.sec = (int)secs_per_km % MINS_IN_HOUR;
    }
        return rate;
}

It works but I'm not overly happy with the fact of the function pace_is returns a reference to a structure and yet that same structure is passed as a reference to the function; what's the point, no real need to have the function return a reference, it might as well be void.

The above file is listed in the attachment, feel free to download.

AttachmentSize
pace-2.zip902 bytes
‹ Array Solution - Half and half, 10 per line up Calculate Pace (Running Program) - # 2 ›
  • Printer-friendly version
  • Login or register to post comments
  • 366 reads
Fri, 28 Dec, 2007 - 22:57
#1
Steve
Joined: 27 Mar 2008
User offline. Last seen 15 hours 48 min ago.
Merely demonstration

I revisited the book, somewhere in chapter 8, and now skimming back I can't find the particular reference (page), I thought I highlighted it. In essence, in relation to references as used in the above code, it's merely for demonstration purposes or how they can be used. Further will unfold in later chapters.

For the purpose of the program, there is no need to return a reference from the function pace_is().

Changes required

Firstly, the prototype:

const hms & pace_is(double distance, hms  & duration, hms & rate);

to

void pace_is(double distance, hms  & duration, hms & rate);

Within the main() function:-

perkm = pace_is(distance,duration,perkm);

to

pace_is(distance,duration,perkm);

Lastly, the pace_is() function, change:

const hms & pace_is(double distance, hms & d, hms & rate)

to

void pace_is(double distance, hms & d, hms & rate)

and remove

return rate;

as we're not returning any values or references.

Revised Code

Here's the revised code, accounting for the above changes.

// pace-3.cpp  --  To calculate pace of a run
// author : Steven Taylor
// 28 Dec, 2007.

#include <iostream>
using namespace std;
const int SECS_IN_MIN = 60;
const int MINS_IN_HOUR = 60;
const int SECS_IN_HOUR = 3600;

struct hms
{
       int hour;
       int min;
       int sec;
};
void pace_is(double distance, hms  & duration, hms & rate);
int main()
{
    double distance;
    hms duration;
    hms perkm = {0,0,0};
   
    cout << "Enter distance (km's) run: ";
    while (!(cin >> distance))
    {
        cout << "\nMust enter a number - re-enter: ";
        cin.clear();
        while (cin.get() != '\n')
            continue;
    }
    cout << "\nEnter duration of run - firstly - hours: ";
    while (!(cin >> duration.hour))
    {
        cout << "\nMust be an integer number - re-enter - hours: ";
        cin.clear();
        while (cin.get() != '\n')
            ;
    }
    cout << "\nEnter minutes (0-59): ";
    while (!(cin >> duration.min) || duration.min >= MINS_IN_HOUR)
    {
        cout << "\nMinutes must be 0 - 59, re-enter - minutes: ";
        cin.clear();
        while (cin.get() != '\n')
            ;
    }
    cout << "\nEnter seconds (0 - 59): ";
    while (!(cin >> duration.sec) || duration.sec >= SECS_IN_MIN)
    {
        cout << "\nSeconds must be 0 - 59, re-enter - seconds: ";
        cin.clear();
        while (cin.get() != '\n')
            ;
    }
    pace_is(distance,duration,perkm);
    cout << "\nThe pace per km is : ";
    if (perkm.hour > 0)
       cout << perkm.hour << ":";
    cout << perkm.min << ":" << perkm.sec << endl;
   
    // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    cin.get();
    return 0;
}

void pace_is(double distance, hms & d, hms & rate)
{
       
    int total_secs = (d.hour * SECS_IN_HOUR) + (d.min * SECS_IN_MIN) + d.sec;
    double secs_per_km = total_secs / distance;
       
 
    if (secs_per_km > SECS_IN_HOUR)
    {
        rate.hour = secs_per_km / SECS_IN_HOUR;
        secs_per_km = (int)secs_per_km % SECS_IN_HOUR;
    }
    if (secs_per_km > 0)
    {
        rate.min = secs_per_km / MINS_IN_HOUR;
        rate.sec = (int)secs_per_km % MINS_IN_HOUR;
    }
}

n/a
  • Login or register to post comments

 Subscribe in a reader

free hit counter


RoopleTheme