Calculating average height of N people

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?
Hello Enna Ko,

I would first start with pen or pencil and paper and plan the program. A good plan makes the coding easier.

Also keep in mind the advice from http://www.cplusplus.com/forum/beginner/275001/

You may also have to go back to the begging of the books that you have used and reread them.

Posting the directions for the program is a good start and a big help.

Andy
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <valarray>
using namespace std;


template<typename T> ostream & operator << ( ostream &out, const valarray<T> V )
{
   for ( T e : V ) out << e << ' ';
   return out;
}


using VAL = valarray<double>;


int main()
{
   int N;
   double mn = 150.0, mx = 200.0;
   cout << "Input N: ";   cin >> N;
   VAL heights( N );
   for ( int i = 0; i < N; )
   {
      double ht;
      cout << "Enter height " << i + 1 << " in cm: ";   cin >> ht;
      if ( ht < mn || ht > mx ) cout << "Invalid height\n";
      else                      heights[i++] = ht;
   }

   double average = heights.sum() / N;
   VAL middle( heights[ heights >= average - 10 && heights <= average + 10 ] );
   VAL small ( heights[ heights <= 1.15 * heights.min() ] );
   VAL large ( heights[ heights >= 0.8  * heights.max() ] );

   cout << "Original heights: " << heights << '\n';
   cout << "Average height:   " << average << '\n';
   cout << "Middle heights:   " << middle << '\n';
   cout << "Short:            " << small  << '\n';
   cout << "Tall:             " << large  << '\n';
}
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.
Last edited on
Hi. Thank you very much for your replies. Could u please explain me this part of the code:
#include <valarray>

template<typename T> ostream & operator << ( ostream &out, const valarray<T> V )
{
for ( T e : V ) out << e << ' ';
return out;
}


using VAL = valarray<double>;

Last edited on
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 << ' ';
}
Assuming I've got my limits right as per the question, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <algorithm>

std::ostream& operator<<(std::ostream& os, const std::vector<double>& vd)
{
	for (const auto& d : vd)
		os << d << " ";

	return os;
}

int main() {
	const double mn {150.0}, mx {200.0};

	int N {};

	std::cout << "Input N: ";
	std::cin >> N;

	std::vector<double> heights(N);
	double sum {}, mnh {mx}, mxh {mn};

	for (int i = 0; i < N; ) {
		double ht {};

		std::cout << "Enter height " << i + 1 << " in cm: ";
		std::cin >> ht;

		if (ht < mn || ht > mx)
			std::cout << "Invalid height\n";
		else {
			heights[i++] = ht;
			sum += ht;

			if (ht > mxh)
				mxh = ht;

			if (ht < mnh)
				mnh = ht;
		}
	}

	const double average {sum / N};
	const double mn15 {mnh * 1.15};
	const double mx20(mxh * .8);
	const double avlow {average - 10};
	const double avhg {average + 10};
	const double low {std::max(avlow, mn15)};
	const double high {std::min(avhg, mx20)};

	std::cout << "Original heights: " << heights << '\n';
	std::cout << "Average: " << average << '\n';
	std::cout << "Minimum: " << mnh << '\n';
	std::cout << "Maximum: " << mxh << '\n';
	std::cout << "Minimum + 15%: " << mn15 << '\n';
	std::cout << "Maximum - 20%: " << mx20 << '\n';
	std::cout << "Average - 10: " << avlow << '\n';
	std::cout << "Average + 10: " << avhg << '\n';
	std::cout << "Low: " << low << '\n';
	std::cout << "High: " << high << '\n';

	// Remove those that are:
	// less than avlow or less than mn15 ie remove less max(avlow, mn15)
	// greater avhg or greater than mx20 ie remove greater min(avhg, mx20)

	if (high > low) {
		heights.erase(std::remove_if(heights.begin(), heights.end(), [low, high](auto num) {return num < low || num > high; }), heights.end());
		std::cout << "Remaining heights: " << heights << '\n';
	} else
		std::cout << "Overlapping ranges - no resultant\n";
}

Topic archived. No new replies allowed.