delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/02/26/20:25:09

Message-ID: <3A9AFEEB.637C@earthlink.net>
From: Joe Wright <joewwright AT earthlink DOT net>
X-Mailer: Mozilla 3.03Gold (Win95; I)
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: convert miles to kilometres
References: <XoBm6.206$GF DOT 50463 AT sapphire DOT mtt DOT net>
Lines: 91
Date: Tue, 27 Feb 2001 01:09:24 GMT
NNTP-Posting-Host: 209.244.208.124
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread2.prod.itd.earthlink.net 983236164 209.244.208.124 (Mon, 26 Feb 2001 17:09:24 PST)
NNTP-Posting-Date: Mon, 26 Feb 2001 17:09:24 PST
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019