Hi all.
On May 20, 2012, at 8:14 AM, Shashank wrote:
> //This program converts celsius to kelvin.
> #include"../../../std_lib_facilities.h"
> //pre condition: argument cel must be a floating point value
Allow me to be a little fussy here.
I would put "none" for a precondition. Looking at the argument list, it's obvious that cel must be a floating point value. By way of contrast, suppose you wanted to compute a letter grade as a function of a numerical grade; you might well require the numerical grade to be in the range [0, 100], and so this would be a precondition for the function.
> //post condition: function should return the floating point value
A post-condition usually refers to an effect of a function call upon the operating environment. For instance, if you were to write your own function to sort the elements of a vector, the postconditon would say that the elements are sorted, e.g., you might write something like
// sort a vector using the foosort algorithm
// precondition: none
// postcondition: a[0] <= a[1] <= ...<= a[n-1], where n = a.size()
void mysort(vector<double> a) { ... }
> const double temp=273.15;
Choose a better name. Moreover, the number 273.15 doesn't mean all that much, but its negative is absolute zero (in degrees Celsius), which is meaningful. So I'd do
const double absolute_zero = -273.15; // degrees Celsius
> double celsiusTokelvin(double cel)
Be consistent in how you write compound words used for names of things. Either write celsiusToKelvin or celsius_to_kelvin. The choice between the latter is a religious argument (akin to Mac vs. Windows vs. Linux, or emacs vs. vi). Bjarne prefers the latter. The book I use for teaching data structures prefers the former. I've tended to use the former, but I don't really have any dog in this fight. To minimize confusion, I tell my students to follow the standard of whatever text we're using.
Also, use "celsius" rather than "cel". It's not that much more typing, since it only appears a few times, and it improves readability.
> {
> if(cel<=-temp)
> error("please enter the valid temperature:\n");
You're telling the user to enter a temperature, but you're not giving him a chance to do so. That's somewhat misleading, and may mystify your user.
> double Kelv;
Why the capital "K" here? You don't put a capital "C" when you talk about Celsius temperatures. Moreover, you're running afoul of the author's coding standard, which more or less states that names of data types should begin with a capital letter and that names of other things start with a lower case letter. (In that way, you can instantly tell whether a name refers to a data type or to anything else.)
Also, I'd use "kelvin", rather than "kelv". It doesn't take that much effort to type the extra two characters, and it certainly improves the readability. Bjarne talks about this in the text.
> Kelv=cel+temp;
> return Kelv;
> }
>
> int main()
> {
> double input_celsius;
> cout<<"Enter the float value in celsius:\n";
> cin>>input_celsius;
> /*if(input_celsius<=-temp)
> error("please enter the valid temperature:\n");*/
Although your conversion function checks for a invalid parameter, it's still a good idea for the calling environment to check the validity of any actual arguments, whenever this is practical. (Also, it's good practice for situations in which different programmers wrote the conversion function and main.) Call it "belt-and-suspenders", if you like.
You might wonder when it might be impractical to check for a valid argument. The answer: when doing so would be overly complex or time-consuming. The classical case? If a vector is in proper order, then binary search can be used instead of linear search, thereby gaining an exponential speedup; however, if the binary search routine checks the the vector is in order, then you lose the speedup.
> double result_kelvin=celsiusTokelvin(input_celsius);
> cout<<input_celsius<<" celsius is "<<result_kelvin<<" kelvin:\n";
> keep_window_open();
> return 0;
> }
Personal preference: A lot of people think that putting a space on either side of an operator improves readability. The only exception would be for multiplicative operators, based on traditional mathematical typesetting. So I'd tend to write "a + b", but "c*d".
I'm attaching my solution to this msg. For fun, I added a loop in main() that rejects invalid Celsius temperatures. The program is not bullet-proof; if you give it "asdf" as the Celsius temperature, it simply uses 0. That kind of thing is for later on.
Art Werschulz (8-{)} "Metaphors be with you." -- bumper sticker
GCS/M (GAT): d? -p+ c++ l u+(-) e--- m* s n+ h f g+ w+ t++ r- y?
Internet: agw STRUDEL
comcast.net