Monday, March 6, 2023
HomeSoftware EngineeringMethods to Resolve Deodorant Evaporator in C

Methods to Resolve Deodorant Evaporator in C


The problem

This program exams 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 edge (threshold) in proportion past which the evaporator is now not helpful. All numbers are strictly optimistic.

This system experiences the nth day (as an integer) on which the evaporator can be out of use.

Instance:

evaporator(10, 10, 5) -> 29

Observe:

Content material is the truth is not crucial within the physique of the operate “evaporator”, you need to use it or not use it, as you want. Some folks may favor to cause with content material, another with percentages solely. It’s as much as you however it’s essential to maintain it as a parameter as a result of the exams have it as an argument.

The answer in C

Choice 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));
}

Choice 2:

int evaporator(double content material, double evap, double threshold) {
    unsigned brief 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;
}

Take a look at circumstances to validate our answer

#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 received %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);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments