Structured Programming
Lab Handouts
Lab 5:
More on Functions
2012
Faculty of Computer & Information sciences
Ain Shams University
Lab5
Example 1:
Write a program that converts from 24-hour notation to 12-hour notation. For example, it should
convert 14:25 to 2:25 P.M. The input is given as two integers. There should be at least three
functions: one for input, one to do the conversion, and one for output. Record the A.M./P.M.
information as a value of type char, ’A’ for A.M. and ’P’ for P.M. Thus, the function for doing
the conversions will have a call-by-reference formal parameter of type char to record whether it
is A.M. or P.M. (The function will have other parameters as well.) Include a loop that lets the
user repeat this computation for new input values again and again until the user says he or she
wants to end the program.
//Task: Convert 24-hour time notation to 12 hour AM/PM notation. //Input: 24 hour time
//Output: corresponding 12-hour time, with AM/PM indication //Required: 3 functions: input, conversion, and output.
#include <iostream>
using namespace std;
void input(int& hours24, int& minutes); void convert(int& hours, char& AMPM);
void output(int hours, int minutes, char AMPM);
int main()
{
int hours, minutes;
char AMPM, ans;
do
{
input(hours, minutes); convert(hours, AMPM);
output(hours, minutes, AMPM);
cout << "Enter Y or y to continue, anything else quits."
<< endl; cin >> ans;
} while('Y'== ans || 'y' == ans);
return 0;
}
void input(int& hours24, int& minutes) {
char colon;
cout << "Enter 24 hour time in the format HH:MM "
<< endl;
cin >> hours24 >> colon >> minutes;
Lab5
void convert(int& hours, char& AMPM) {
if(hours > 12) // definitely in the afternoon
{
hours = hours - 12; AMPM = 'P';
}
else if (12 == hours) // boundary afternoon hour
AMPM = 'P'; // but hours is not changed.
else if (0 == hours) // boundary morning hour
{
hours = hours + 12; AMPM = 'A';
}
else // (hours < 12) // definitely morning hour
AMPM = 'A'; // hours is unchanged
}
void output(int hours, int minutes, char AMPM)
{
cout << "Time in 12-hour format: " << endl << hours << ":" << minutes << " "
<< AMPM << 'M' << endl;
}
Example 2:
Write a program that will read in a length in feet and inches and output the equivalent length in
meters and centimeters. Use at least three functions: one for input, one or more for calculating,
and one for output. Include a loop that lets the user repeat this computation for new input values
until the user says he or she wants to end the program. There are 0.3048 meters in a foot, 100
centimeters in a meter, and 12 inches in a foot.
//Task: Convert feet/inches to meters
//Input: a length in feet and inches, with possible decimal //part of inches
//Output: a length in meters, with 2 decimal places, which //are the 'centimeters' specified in the problem.
#include <iostream>
using namespace std;
void input(int& feet, double& inches);
Lab5
int main()
{
int feet;
double inches, meters;
char ans;
do
{
input(feet, inches);
convert(feet, inches, meters); output(feet, inches, meters);
cout << "Y or y continues, any other character quits "
<< endl; cin >> ans;
} while('Y' == ans || 'y' == ans);
return 0;
}
void input(int& feet, double& inches) {
cout << "Enter feet as an integer: " << flush; cin >> feet;
cout << "Enter inches as a double: " << flush; cin >> inches;
}
const double METERS_PER_FOOT = 0.3048;
const double INCHES_PER_FOOT = 12.0;
void convert(int feet, double inches, double& meters) {
meters = METERS_PER_FOOT * (feet + inches/INCHES_PER_FOOT);
}
void output(int feet, double inches, double meters)
{
//inches, meters displayed as a number with two decimal places
cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2);
cout << "the value of feet, inches" << feet << ","
<< inches << endl
<< " converted to meters, centimeters is "
<< meters << endl;
Lab5
Example 3
You would like to know how fast you can run in Miles Per Hour. Your treadmill will tell you your
speed in terms of a pace (Minutes and Seconds per mile, e.g. a "5:30 mile") or in terms of
kilometers per hour (KPH).
Write an overloaded function called "ConvertToMPH". The first definition should take as input
two integers that represent the pace in Minutes and Seconds per mile and return the speed in
MPH as a double. The second definition should take as input one double that represents the
speed in KPH and return the speed in MPH as a double. One mile is approximately 1.61
kilometers. Write a driver program to test your function.
//mph.cpp
//This program tests an overloaded function to convert a pace // in minutes and seconds to miles per hour, and also to convert // kilometers per hour to miles per hour.
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
// Function prototypes
double ConvertToMPH(int paceMin, int paceSec);
double ConvertToMPH(double kph);
// ====================== // ConvertToMPH
// Converts a pace in minutes/seconds per mile into // miles per hour.
// ======================
double ConvertToMPH(int paceMin, int paceSec)
{
int secspermile;
double mph;
// Convert pace to seconds per mile
secspermile = paceMin * 60 + paceSec;
// Convert to miles per hour. 1/secspermile is miles/second and then // scale to one hour by multiplying by 3600 seconds/hour
mph = (1 / static_cast<double>(secspermile)) * 3600; return mph;
}
// ====================== // ConvertToMPH
// Converts a pace in kilometers per mile into // miles per hour.
// ======================
double ConvertToMPH(double kph)
{
double mph;
mph = kph / 1.61;
Lab5
int main()
{
cout << "5:30 pace is " << ConvertToMPH(5,30) << " MPH." << endl; cout << "7:30 pace is " << ConvertToMPH(7,30) << " MPH." << endl; cout << "8:00 pace is " << ConvertToMPH(8,0) << " MPH." << endl; cout << "10 kph is " << ConvertToMPH(10) << " MPH." << endl; cout << "20 kph is " << ConvertToMPH(20) << " MPH." << endl;
cout << "120 kph is " << ConvertToMPH(120) << " MPH." << endl << endl;
}
Example 4
Write a function named
ConvertToLowestTerms
that inputs two integer parameters by
reference named numerator and denominator. The function should treat these variables as a
fraction and reduce them to lowest terms. For example, if numerator is 20 and denominator is 60
then the function should change the variables to 1 and 3 respectively. This will require finding
the greatest common divisor for the numerator and denominator, then dividing both variables by
that number. If the denominator is zero the function should return false, otherwise the function
should return true. Write a test program that uses
ConvertToLowestTerms
to reduce and
output several fractions.
#include <iostream>
using namespace std;
// Function prototypes
bool ConvertToLowestTerms(int &numerator, int &denominator);
bool ConvertToLowestTerms(int &numerator, int &denominator)
{
if (denominator == 0)
return false;
// First, find the greatest common divisor // by starting with the smaller value and // subtracting 1 until we get a number that
// evenly divides both numerator and denominator. // There are faster algorithms (e.g. Euclid's)
int gcd = denominator;
if (gcd > numerator) gcd = numerator;
while (((numerator % gcd) != 0) || ((denominator % gcd) != 0)) {
gcd--; }
numerator /= gcd; denominator /= gcd;
Lab5
intmain( )
{
int numerator, denominator; numerator = 10;
denominator = 5;
cout << numerator << "/" << denominator << " = ";
ConvertToLowestTerms(numerator, denominator); cout << numerator << "/" << denominator << endl; numerator = 43;
denominator = 13;
cout << numerator << "/" << denominator << " = ";
ConvertToLowestTerms(numerator, denominator); cout << numerator << "/" << denominator << endl; numerator = 60;
denominator = 40;
cout << numerator << "/" << denominator << " = ";
ConvertToLowestTerms(numerator, denominator); cout << numerator << "/" << denominator << endl; numerator = 100;
denominator = 55;
cout << numerator << "/" << denominator << " = ";
ConvertToLowestTerms(numerator, denominator); cout << numerator << "/" << denominator << endl; cout << "Enter a character to exit." << endl;
char wait; cin >> wait;
return 0;