The problem
This program assessments the lifetime of an evaporator containing a gasoline.
We all know the content material of the evaporator (content material in ml), the proportion of froth or gasoline misplaced every single day (evap_per_day) and the brink (threshold) in proportion past which the evaporator is now not helpful. All numbers are strictly constructive.
This system experiences the nth day (as an integer) on which the evaporator can be out of use.
Instance:
evaporator(10, 10, 5) -> 29
Be aware:
Content material is the truth is not obligatory within the physique of the operate “evaporator”, you should utilize it or not use it, as you would like. Some individuals may favor to motive with content material, another with percentages solely. It’s as much as you however you should maintain it as a parameter as a result of the assessments have it as an argument.
The answer in C
Possibility 1:
#embrace <math.h>
int evaporator(double content material, double evap_per_day, double threshold) {
return (int) ceil(log(threshold/100) / log(1 - evap_per_day/100));
}
Possibility 2:
int evaporator(double content material, double evap, double threshold) {
unsigned quick n = 0;
for(float misplaced = content material * (threshold/100); content material > misplaced; n++)
content material -= content material * (evap/100);
return n;
}
Possibility 3:
int evaporator(double content material, double evap_per_day, double threshold) {
int days;
double proportion = 100;
for(days = 0; proportion>=threshold; days++) {
proportion *= (1-(evap_per_day/100));
}
return days;
}
Take a look at instances to validate our resolution
#embrace <criterion/criterion.h>
extern int evaporator(double content material, double evap_per_day, double threshold);
static void do_test(double content material, double evap_per_day, double threshold, int anticipated)
{
int precise = evaporator(content material, evap_per_day, threshold);
cr_assert_eq(precise, anticipated,
"content material = %fn"
"evap_per_day = %fn"
"threshold = %fn"
"anticipated %d, however bought %d",
content material, evap_per_day, threshold,
anticipated, precise
);
}
Take a look at(tests_suite, sample_tests)
{
do_test(10, 10, 10, 22);
do_test(10, 10, 5, 29);
do_test(100, 5, 5, 59);
do_test(50, 12, 1, 37);
do_test(47.5, 8, 8, 31);
do_test(100, 1, 1, 459);
do_test(10, 1, 1, 459);
do_test(100, 1, 5, 299);
}