[Tutor] small program in Python and in C++
Rob
rob@uselesspython.com
Tue, 02 Jul 2002 17:27:00 -0500
This is a multi-part message in MIME format.
--------------030709000100070406090702
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Quite right. Attached is a C++ source file with the formula wrapped in a
function.
Another friend contributed this equivalent (but not with the function)
version in Perl:
#!/usr/bin/perl
print "What is your height in inches? ";
$inches=<STDIN>;
print "What is your weight in pounds? ";
$pounds=<STDIN>;
$bmi= 703 * ($pounds/($inches*$inches));
print "Your body mass index is $bmi\n";
if ($bmi>=30) { print "You are obese!\n"; }
else { print "You are not obese!\n"; }
Happy Hacking,
Rob
http://uselesspython.com
Danny Yoo wrote:
>
> On Tue, 2 Jul 2002, Rob wrote:
>
>
>>I thought I'd pass it all along to the Tutor list in case it makes a
>>good example for anyone (or if anyone cared to point out other ways to
>>do it, etc.). If you know a little C++ and are looking into Python, or
>>vice versa, you might like it on some level.
>>
>>
>># calculate body mass index
>>bodyMassIndex = 703 * ( weight / ( height * height ) )
>>
>
> It might be good to make this a function --- even though it's so simple,
> having it as a function makes it easy to reuse this calculation if we ever
> need it later. As an additional bonus, if we write it as a function, we
> can play with it very easily within the interactive interpreter:
>
>
> ###
>
>>>>def bodyMassIndex(weight, height):
>>>>
> ... return 703 * ( weight / ( height * height ) )
> ...
>
>>>>bodyMassIndex(175, 6.5)
>>>>
> 2911.834319526627
>
>>>>bodyMassIndex(175, 6)
>>>>
> 2812
> ###
>
>
> Oh! It also reveals that we need to be careful about the types that we
> use to do the calculation when division comes into play.
>
>
> C++'s variable typing forces weight and height to be floats, but we don't
> restrict the same in Python. If "from __future__ import true_division"
> isn't on, we should probably force floating point division:
>
> ###
>
>>>>def bodyMassIndex(weight, height):
>>>>
> ... return 703 * ( weight / float( height * height ) )
> ...
>
>>>>bodyMassIndex(175, 6)
>>>>
> 3417.3611111111109
> ###
>
>
> Hope this helps!
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
"Giving the Linus Torvalds Award to the Free Software Foundation is a
bit like giving the Han Solo Award to the Rebel Alliance."
--Richard Stallman at the 1999 LinuxWorld show
--------------030709000100070406090702
Content-Type: text/plain;
name="obesityFunc.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="obesityFunc.cpp"
#include <iostream>
using namespace std;
// function prototype for getBMI()
// A function prototype appearing before the function is called
// in a program tell the program how to talk to the function.
float getBMI(float, float);
int main()
{
// fetch data from the user
cout << endl << "What is your height in inches? ";
float height;
cin >> height;
cout << endl << "What is your weight in pounds? ";
float weight;
cin >> weight;
// calculate body mass index
float bodyMassIndex;
bodyMassIndex = getBMI(height, weight);
// display obesity judgement
cout << "\nYour Body Mass Index is " << bodyMassIndex << endl;
if (bodyMassIndex >= 30)
cout << "\nYou are obese." << endl;
else
cout << "\nYou are not obese." << endl;
return 0;
}
//---------------------------------------------------------------
// In this function, "height" could just as easily read "inches",
// and "weight" could just as easily read "pounds"
// without having to change anything outside the function.
// Just a handy fact for those who are new to C++.
float getBMI(float height, float weight)
{
return 703 * ( weight / ( height * height ) );
}
--------------030709000100070406090702--