Does everything look alright? (Cel and fah table)

Did I make any mistakes?
(Ignore the excessive use of comments it's required by my professor)
Should I make 0.0 a named constant?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Ashton Dreiling
//Celsius to Fahrenheit Table exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    //variables
    double celsiusTemp;
    double fahrenheitTemp;

    //constants for calculations
    const double MAX_VALUE = 20.0;//Max value for count-controlled loop calculation
    const double CONST = 9.0/5.0; // 9/5 constant
    const int FAHRENHEIT_FREEZE = 32; // water freezing temp in Fehrenheit


    //Display table headings
    cout << "We are going to display a table of the Celsius temperature zero through twenty and their Fahrenheight eqiuvalents." << endl;
    cout << "Celsius\tFahrenheit" << endl;
    cout << "-------------------------------" << endl;

    //Display celsius 0.0 through 20.0 and
    //their fahrenheit equivalent
    for (celsiusTemp = 0.0; celsiusTemp <= MAX_VALUE; ++celsiusTemp )
    {
        fahrenheitTemp = CONST * celsiusTemp + FAHRENHEIT_FREEZE;
        cout << celsiusTemp << "\t\t" << fahrenheitTemp << endl;
    }//end of count-controlled loop


    system("Pause");

    return 0;
}//end main 


Last edited on
closed account (48T7M4Gy)
Have you tried it with some test data and checked the results?

Just selecting a comprehensive set of test data can be a worthwhile challenge.
Hi,

Lines 11 and 12 : Always initialise your variables to something - it's one of the biggest source of error for young and old :+)

Line 27: Don't use doubles in a for loop, or any other end condition. They are not represented exactly, and could give you off by one errors. There are lots of numbers including whole numbers that don't convert exactly to their double counterparts. For example 20 might be 20.0 +/- 3e-16. If it is a shade less than 20, you will get an extra iteration in your for loop. Instead, calculate how many times you need to loop, as an int, then use that value in the for loop.

CONST is not a particularly good variable name IMO.

Good Luck !!
Thank you!
Topic archived. No new replies allowed.