Hello! How do I make a program in C++ in which the heights of N number of people will be input in centimeters? Also, all of the following should be included:
The entered values can be repeated. The interval of entered heights should be between 150 and 200 centimeters. In case of height input outside this interval, it should not be accepted.
-Calculate the average height
-Set aside those values of heights that vary by +/- 10 centimeters of the average height.
-Set aside those values for the heights that vary from 0 to + 15% in relation to the minimum height and from 0 to -20% in relation to the maximum height. It is recommended to work with functions.
The results should be tested in 3 rows, that will have the same number of elements.
Any ideas of how do I make this program?
As you need to 'set aside (whatever that means?)' entered values based upon a calculated average, you will need to store the entered numbers. Have you covered vectors yet - as that would be the container of choice.
As the values are entered, you can maintain the number entered, their sum, the minimum and maximum values. Once you have these and all the data has been entered, then you can iterate the entered data and 'set aside' the required values.
you can do it with less work but the easy way is to collect it (as noted, get the average while you do this) and shove it into a container. Then sort it to simplify your work later (the two functions I suggest can just iterate from the front or back end to gather the values you need).
ok, now lets play :)
say you had a function that gave you half the answer:
container pluses (double d, double average); //returns a container of all the values that are more than d above the average.
and the same for minuses (returns a container of all values less than d from average).
then the % question is the SAME as the +- 10 question, do you see it?
so you run it with 10 and 10 for each, then run it again with 15% and 20%, reusing that code.
The using VAL = valarray<double>; defines an alias for type.
After that definition we can write in code VAL (which the program does four times) instead of writing valarray<double>. That both saves typing time and it is possible to change the type in one place (the alias definition), rather than in every place it occurs in code.
Older syntax for defining similar alias: typedef valarray<double> VAL;
1 2 3 4 5 6
template<typename T>
ostream & operator << ( ostream &out, const valarray<T> V )
{
for ( T e : V ) out << e << ' ';
return out;
}
Is an example of operator overloading. An operator is a function.
One could have written such function for concrete type: ostream & operator << ( ostream &out, const valarray<double> V );
but instead here we have a generic template from which compiler can generate for different valarrays. (Valarray is a template for class. valarray<double> is a class.)
C++11 did introduce ranged for loop syntax. for ( T e : V ) out << e << ' ';
is about the same as:
1 2 3 4
for ( auto it = std::begin(V); it != std::end(V); ++it ) {
T e = *it;
out << e << ' ';
}