patterncppModerate
Print out table with start/end temperatures and step size
Viewed 0 times
temperatureswithsizeprintstepstartandendouttable
Problem
I am trying to learn C++ by myself. I looked up a sample question after going through some text. Though I would like someone to review my code. I'm basically asking you to break it to show some flaws or some thing I missed. As a beginner I tried exhaustively to improve it and now hit a wall to analyse robustness of the code.
Problem statement:
In this challenge, write a program that takes in three arguments, a
start temperature (in Celsius), an end temperature (in Celsius) and a
step size. Print out a table that goes from the start temperature to
the end temperature, in steps of the step size; you do not actually
need to print the final end temperature if the step size does not
exactly match. You should perform input validation: do not accept
start temperatures less than a lower limit (which your code should
specify as a constant) or higher than an upper limit (which your code
should also specify). You should not allow a step size greater than
the difference in temperatures.
```
#include
#include
#include
#define LOWER_LIMIT 23.3
#define UPPER_LIMIT 256.3
using namespace std;
bool isnum(string s)
{
//check if the string is a number
//48 & 57
int len = s.length();
for(int i = 0; i =48 && int(s[i]) "UPPER_LIMIT)
{
cout does not meet the limit requirement ("UPPER_LIMIT || end does not meet the requirement ("=(UPPER_LIMIT - LOWER_LIMIT))
{ //zero or negetive stepsize checking
cout<<"The step_size cannot be negetive, zero or greater than or equal to step_size"<<endl;
return -1;
}
if(end<start) //swapping variables if start is greater than end
{
cout <<"Swapped! end and start values for simplicity" <<endl;
double tmp = start;
start = end;
end = tmp;
}
cout << "start "<<start<<endl;
cout << "end "<<end<<endl;
Problem statement:
In this challenge, write a program that takes in three arguments, a
start temperature (in Celsius), an end temperature (in Celsius) and a
step size. Print out a table that goes from the start temperature to
the end temperature, in steps of the step size; you do not actually
need to print the final end temperature if the step size does not
exactly match. You should perform input validation: do not accept
start temperatures less than a lower limit (which your code should
specify as a constant) or higher than an upper limit (which your code
should also specify). You should not allow a step size greater than
the difference in temperatures.
```
#include
#include
#include
#define LOWER_LIMIT 23.3
#define UPPER_LIMIT 256.3
using namespace std;
bool isnum(string s)
{
//check if the string is a number
//48 & 57
int len = s.length();
for(int i = 0; i =48 && int(s[i]) "UPPER_LIMIT)
{
cout does not meet the limit requirement ("UPPER_LIMIT || end does not meet the requirement ("=(UPPER_LIMIT - LOWER_LIMIT))
{ //zero or negetive stepsize checking
cout<<"The step_size cannot be negetive, zero or greater than or equal to step_size"<<endl;
return -1;
}
if(end<start) //swapping variables if start is greater than end
{
cout <<"Swapped! end and start values for simplicity" <<endl;
double tmp = start;
start = end;
end = tmp;
}
cout << "start "<<start<<endl;
cout << "end "<<end<<endl;
Solution
Automatically detectable bugs
First, let the compiler do some reviewing:
User experience
I found your program to be infuriating to use. My first few attempts to run the program all failed; I would have expected all of them to produce reasonable output.
I finally succeeded in getting an incomplete response. What happened to 24.3°C?
My frustration appears to be partly a consequence of poor number parsing, and partly a consequence of artificial temperature limits. Reasonable limits, I think, are −273.15°C (absolute zero) for the lower bound, and 1.4e32°C (absolute hot) for the upper bound. (
Problem decomposition
There is plenty of action going on in
First, let the compiler do some reviewing:
$ clang++ -Wall -c cr44821.cpp
cr44821.cpp:26:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
1 warning generated.User experience
I found your program to be infuriating to use. My first few attempts to run the program all failed; I would have expected all of them to produce reasonable output.
$ ./cr44821 -40 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 0 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 0 40 0.5
0
40
40
The does not meet the limit requirement (23.3°C - 256.3°C)
$ ./cr44821 23.3 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 23.3 40 0.5
23.3
40
40
The step_size cannot be negetive, zero or greater than or equal to step_sizeI finally succeeded in getting an incomplete response. What happened to 24.3°C?
$ ./cr44821 23.3 25 1
23.3
25
25
start 23.3
end 25
step_size 1
1number of iterations
23.3°C = 55.3°FMy frustration appears to be partly a consequence of poor number parsing, and partly a consequence of artificial temperature limits. Reasonable limits, I think, are −273.15°C (absolute zero) for the lower bound, and 1.4e32°C (absolute hot) for the upper bound. (
std::numeric_limits::max() is 9.9e307, which is too large.)Problem decomposition
There is plenty of action going on in
main(). The temperature conversion calculation itself does not belong there. This problem cries out for a functiondouble c_to_f(double celsius) {
return celsius / 5 * 9 + 32;
}Code Snippets
$ clang++ -Wall -c cr44821.cpp
cr44821.cpp:26:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
1 warning generated.$ ./cr44821 -40 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 0 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 0 40 0.5
0
40
40
The <start_temprature> does not meet the limit requirement (23.3°C - 256.3°C)
$ ./cr44821 23.3 +40 0.5
All three input arguments must be positive numbers!
$ ./cr44821 23.3 40 0.5
23.3
40
40
The step_size cannot be negetive, zero or greater than or equal to step_size$ ./cr44821 23.3 25 1
23.3
25
25
start 23.3
end 25
step_size 1
1number of iterations
23.3°C = 55.3°Fdouble c_to_f(double celsius) {
return celsius / 5 * 9 + 32;
}Context
StackExchange Code Review Q#44821, answer score: 14
Revisions (0)
No revisions yet.