The problem
This program exams the lifetime of an evaporator containing a fuel.
We all know the content material of the evaporator (content material in ml), the share of froth or fuel misplaced day-after-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 use it or not use it, as you would like. Some individuals may want to motive with content material, another with percentages solely. It’s as much as you however you have to maintain it as a parameter as a result of the exams have it as an argument.
The answer in C
Choice 1:
#embody <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));
}
Choice 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;
}
Choice 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;
}
Check circumstances to validate our answer
#embody <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 obtained %d",
content material, evap_per_day, threshold,
anticipated, precise
);
}
Check(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);
}