a temperature conversion programme

Discussion in 'C++' started by arnuld, Mar 4, 2007.

  1. arnuld

    arnuld Guest

    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;
    }
     
    arnuld, Mar 4, 2007
    #1
    1. Advertising

  2. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:...
    > 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;
    }
     
    Jim Langston, Mar 4, 2007
    #2
    1. Advertising

  3. arnuld

    Ian Collins Guest

    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.
     
    Ian Collins, Mar 4, 2007
    #3
  4. 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
     
    John Harrison, Mar 4, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Mar 4, 2:35 pm, John Harrison <> 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
     
    arnuld, Mar 4, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,659
    Joe Knapka
    Jul 24, 2006
  2. Flemming Hansen

    CPU temperature reading with C

    Flemming Hansen, Mar 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    1,294
    Thomas Matthews
    Mar 17, 2005
  3. deanfamily11

    Temperature conversion

    deanfamily11, Sep 10, 2005, in forum: C++
    Replies:
    1
    Views:
    462
    Shezan Baig
    Sep 10, 2005
  4. arnuld
    Replies:
    11
    Views:
    597
    arnuld
    Mar 6, 2007
  5. Replies:
    21
    Views:
    1,685
Loading...

Share This Page