473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

a temperature conversion programme

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;
}

Mar 4 '07 #1
4 2319

"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr 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;
}
Mar 4 '07 #2
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.
Mar 4 '07 #3
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
Mar 4 '07 #4
On Mar 4, 2:35 pm, John Harrison <john_androni...@hotmail.comwrote:
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

Mar 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Derek Ross | last post by:
Hello, Say I have a server that's saving the CPU temperature to 'temperature.js' once a second. The contents of the file is one single line: var temperature = "35.5"; And it changes as...
1
by: deanfamily11 | last post by:
I'm trying to have this program do a simple temperature conversion from Fahrenheit to Celsius. I have confirmed that the other variable is receiving and calculating the conversion, but it is just...
5
by: anthony | last post by:
One the computer I am programmig I could see the CPU temperature in the BIOS, is there a system DLL in VB.NET that I can call to display the temperature in my software? Thanks!
1
by: pollardw | last post by:
i have written a small program to convert Fahrenheit to Celsius and I have a minor problem. Here is the code what do i need to change to get it to work? /* Convert Fahrenheit to Celsius */ ...
11
by: arnuld | last post by:
i have created a new temperature conversion programme. it converts a temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit at user's will. tell em if i need some improvements: ...
21
by: AsheeG87 | last post by:
Hey Everyone~ I'm still a C++ Rookie so please bear with me on this. I'm doing a temperature conversion program with prototype functions. Basicly, I was wondering if some of you would take a look...
16
by: Brigitte Behrmann | last post by:
I am absolutely stuck with this one. I have to create a temperature conversion calculator that rounds the resulting temperature to the nearest whole number & vice versa. The result must be displayed...
25
by: kid joe | last post by:
Hi, Is it normal for the temperature of your CPU to increase as time goes on? I've got an Athlon XP 2100+ and I see that the temp is now at 51C where it used to be around 42 to 44. Anyone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.