Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a temperature conversion programme

Reply
Thread Tools

a temperature conversion programme

 
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
this is my final code. Can you folks advise some improvements for
making this programme better?

BTW, i aways compile my programme with this cmmand on Linux:

g++ -ansi -pedantic -Wall -Wextra file.cpp

// a programme that converts Fahrenheit
temperature
// to Celcius (in range 0 -
300)

#include <iostream>

int main() {
int fahr, cel;
const int lower = 0;
const int upper = 300;
const int stepper = 20;

fahr = lower;

while (fahr <= upper)
{
cel = (5 * (fahr - 32)) / 9;
std::cout << fahr
<< "F \t"
<< cel
<< "C"
<< "\n";
fahr = fahr + stepper;
}

return 0;
}

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-04-2007

"arnuld" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> this is my final code. Can you folks advise some improvements for
> making this programme better?
>
> BTW, i aways compile my programme with this cmmand on Linux:
>
> g++ -ansi -pedantic -Wall -Wextra file.cpp
>
> // a programme that converts Fahrenheit
> temperature
> // to Celcius (in range 0 -
> 300)
>
> #include <iostream>
>
> int main() {
> int fahr, cel;
> const int lower = 0;
> const int upper = 300;
> const int stepper = 20;
>
> fahr = lower;
>
> while (fahr <= upper)
> {
> cel = (5 * (fahr - 32)) / 9;
> std::cout << fahr
> << "F \t"
> << cel
> << "C"
> << "\n";
> fahr = fahr + stepper;
> }
>
> return 0;
> }


#include <iostream>

int main()
{
const int lower = 0;
const int upper = 300;
const int stepper = 20;
for ( int fahr = lower; fahr <= upper; fahr += stepper )
{
int cel = ( 5 * ( fahr - 32 ) ) / 2;
std::cout << fahr << "F \t "
<< cel << "C\n";
}

return 0;
}


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-04-2007
Jim Langston wrote:

> #include <iostream>
>
> int main()
> {
> const int lower = 0;
> const int upper = 300;
> const int stepper = 20;
> for ( int fahr = lower; fahr <= upper; fahr += stepper )
> {
> int cel = ( 5 * ( fahr - 32 ) ) / 2;


int cel = ( 5 * ( fahr - 32 ) ) / 9;

> std::cout << fahr << "F \t "
> << cel << "C\n";
> }
>
> return 0;
> }
>
>



--
Ian Collins.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-04-2007
arnuld wrote:
> this is my final code. Can you folks advise some improvements for
> making this programme better?
>
> BTW, i aways compile my programme with this cmmand on Linux:
>
> g++ -ansi -pedantic -Wall -Wextra file.cpp
>
> // a programme that converts Fahrenheit
> temperature
> // to Celcius (in range 0 -
> 300)
>
> #include <iostream>
>
> int main() {
> int fahr, cel;
> const int lower = 0;
> const int upper = 300;
> const int stepper = 20;
>
> fahr = lower;
>
> while (fahr <= upper)
> {
> cel = (5 * (fahr - 32)) / 9;
> std::cout << fahr
> << "F \t"
> << cel
> << "C"
> << "\n";
> fahr = fahr + stepper;
> }
>
> return 0;
> }
>


My only real concern is the use of int to represent temperature. This is
unnatural (temperature is a continuously varying quantity so is
naturally represented as a double) and also means you get a rounding
error in your calculation. For instance

40 deg F is not 4 deg C, its more like 4.4444 deg C
60 deg F is not 15 deg C, its more like 15.5555 deg C

even if you only what to print out the converted temperature to the
nearest degree, your still getting the wrong answer in the second
example above (nearest being 16 deg C).

I would change fahr and cel to doubles, and then make a decision on how
you want to print the results.

Apart from that I think it's very good.

john
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
> On Mar 4, 2:35 pm, John Harrison <(E-Mail Removed)> wrote:

> My only real concern is the use of int to represent temperature. This is
> unnatural (temperature is a continuously varying quantity so is
> naturally represented as a double) and also means you get a rounding
> error in your calculation. For instance
>
> 40 deg F is not 4 deg C, its more like 4.4444 deg C
> 60 deg F is not 15 deg C, its more like 15.5555 deg C
>
> even if you only what to print out the converted temperature to the
> nearest degree, your still getting the wrong answer in the second
> example above (nearest being 16 deg C).
>
> I would change fahr and cel to doubles, and then make a decision on how
> you want to print the results.



ok, i will do this. that is really logical.

> Apart from that I think it's very good.


thanks
> john



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Temperature Conversion Program - Still a Rookie AsheeG87@msn.com C++ 21 04-03-2007 12:43 AM
a "temp conversion" programme arnuld C++ 11 03-06-2007 03:33 AM
Temperature conversion deanfamily11 C++ 1 09-10-2005 12:24 AM



Advertisments