Mail Archives: djgpp/2001/02/26/20:25:09
Stewart wrote:
>
> Can you help me write a C++ program that uses include <iostream.h> which has
> a function to convert miles to kilometres. ( 1 km = 0.621371 miles I think)
> What const double can I use for the conversion factor?
> In more particular terms, I need help to write a program that prompts the
> user for a value in miles and outputs the equivalent value in km using a
> precision of 2 digits after the decimal. In the function definition need to
> take the miles variables as a formal parameter and returns the km value. I
> think I know how a prototype can be used but I don't know how to write a
> program where you don't need to use it. Can anyone help me?
I attach a C program I wrote some time ago which does various kinds of
conversions. It may help you or it may not. Alas, it is not C++. It
may give you some idea of simple calculations and formatted output. It
may also show you (and other readers of this group) that we can know
several things with exactitude, other things not.
HTH, Have fun.
/*
Weights and Measures, Metric, American and British.
Well defined is length. The Meter is king and the inch
is defined in terms of meters, specifically 0.0254 meters.
Less clear is weight. Metric is clear. One cubic centimeter
of water (at 4 degrees Celsius) weighs one gram. 1,000 grams
is One Kilogram.
The World Almanac declares 453.59237 grams per pound. I don't know
how
this figure is derived. The following is just an exercize.
*/
#include <stdio.h>
#define MPI 0.0254
#define GLB 453.59237
int main(void) {
double cm = MPI * 100;
double ccci = cm * cm * cm;
double cicf = 12 * 12 * 12;
double gpg = 231 * ccci;
double gpo = gpg / 128;
double ppk = 1000 / GLB;
double goz = GLB / 16;
double ifoot = MPI * 12; /* SI Foot */
double sfoot = (double)1200/3937; /* US Survey Foot */
double ipm = 1 / MPI;
putchar('\n');
printf("One survey foot = %.10f meters.\n", sfoot);
printf("One statute mile = %.10f kilometers.\n", sfoot * 5280 /
1000);
printf("One inch = %.4f meters (exactly).\n", MPI);
printf("One foot = %.4f meters (exactly).\n", ifoot);
printf("One yard = %.4f meters (exactly).\n", 3 *
ifoot);
printf("One cubic inch = %.6f cubic centimeters
(exactly).\n", ccci);
printf("One cubic foot = %.9f liters (exactly).\n", ccci *
cicf / 1000);
printf("One cubic foot = %.8f US gallons.\n", cicf / 231);
printf("One US gallon = %.9f liters (exactly).\n", gpg /
1000);
printf("One ounce (fluid) = %.9f milliliters.\n", gpo);
printf("One pound = %.5f grams (exactly).\n", GLB);
printf("One pound = %d grains (exactly).\n", 7000);
printf("One ounce (avdp) = %.9f grams (exactly).\n", goz);
printf("One ounce (avdp) = %.1f grains (exactly).\n",
(double)7000 / 16);
printf("One grain = %.5f milligrams (exactly).\n", GLB /
7);
printf("One carat = %.8f grains.\n", 1400 / GLB);
putchar('\n');
printf("One meter = %.8f inches.\n", ipm);
printf("One meter = %.9f feet.\n", ipm / 12);
printf("One meter = %.9f yards.\n", ipm / 36);
printf("One kilogram = %.9f pounds.\n", ppk);
printf("One gram = %.8f grains.\n", 7000 / GLB);
printf("One gram = %.9f ounces.\n", 1 / goz);
/*
printf("One US gallon (water) weighs %f pounds.\n", gpg / GLB);
printf("Ten pounds of water is %f US gallons.\n", GLB * 10 / gpg);
*/
return 0;
}
--
Joe Wright mailto:joewwright AT earthlink DOT net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
- Raw text -