Task:

N people live in the village. On each known surname, age, gender. Enter information about the inhabitants of this item and count the number of women and men, display information about the average age of men, print the names of those men whose age is below average.

How to implement its condition?

  • you need a structure with a surname, age and gender, and a list (list) or an array with N number of elements of such structures, and then the approach is different. There are algorithms and lambdas, you can write functions or functors to output or calculate what you need for each element - AR Hovsepyan

2 answers 2

Example:

#include <string> #include <iostream> #include <list> struct Human{ enum class Gender{ Male = 1, Female = 2 }; Human(int age = 0, Gender gender = Gender::Male, const std::string& lastName = {}): age{age}, gender{gender}, lastName{lastName} {} Human(const Human& other): age{other.age}, gender{other.gender}, lastName{other.lastName} {} Human& operator=(const Human& other){ if (this != &other){ age = other.age; gender = other.gender; lastName = other.lastName; } return *this; } int age; Gender gender; std::string lastName; }; Human readFromUser(){ int age; int gender; std::string lastName; std::cout << "Enter gender of human:\n" << "1 - Male\n" << "2 - Female" << std::endl; std::cin >> gender; std::cout << "Enter age of human:\n"; std::cin >> age; std::cout << "Enter lastName of human:"; std::cin >> lastName; return Human(age, static_cast<Human::Gender>(gender), lastName); } int main() { std::cout << "Enter number of humans:"; int n; std::cin >> n; std::list<Human> humans; for (int i = 0; i < n; i++){ humans.push_back(readFromUser()); } double ageSum = 0.0; int maleCount = 0; int femaleCount = 0; //считаСм сумму возрастов ΠΌΡƒΠΆΡ‡ΠΈΠ½, //Π° Π·Π°ΠΎΠ΄Π½ΠΎ ΠΈ количСство ΠΆΠ΅Π½Ρ‰ΠΈΠ½ ΠΈ ΠΌΡƒΠΆΡ‡ΠΈΠ½ for (const auto& h: humans){ switch (h.gender) { case Human::Gender::Female: //ΠΆΠ΅Π½Ρ‰ΠΈΠ½Ρ‹ femaleCount++; break; case Human::Gender::Male: ageSum += static_cast<double>(h.age); maleCount++; break; default: break; } } double ageAvg = 0.0; if (maleCount != 0){ ageAvg = ageSum / static_cast<double>(maleCount); } std::cout << "male count: " << maleCount << std::endl; std::cout << "female count: " << femaleCount << std::endl; std::cout << "ageAvg:" << ageAvg << std::endl; std::cout << "list of lastnames:" << std::endl; for (const auto& h: humans){ if (h.gender == Human::Gender::Male){ if (static_cast<double>(h.age) < ageAvg){ std::cout << "age: " << h.age << " lastname:" << h.lastName << std::endl; } } } } 

Example of use:

 Enter number of humans:10 Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 45 Enter lastName of human:ivanov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 25 Enter lastName of human:Egorov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 19 Enter lastName of human:Smirnova Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 38 Enter lastName of human:Sidorov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 25 Enter lastName of human:Petrov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 36 Enter lastName of human:Barabash Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 26 Enter lastName of human:Skladanov Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 27 Enter lastName of human:Karlov Enter gender of human: 1 - Male 2 - Female 2 Enter age of human: 59 Enter lastName of human:Poroshina Enter gender of human: 1 - Male 2 - Female 1 Enter age of human: 45 Enter lastName of human:Petrov male count: 7 female count: 3 ageAvg:33 list of lastnames: age: 25 lastname:Egorov age: 25 lastname:Petrov age: 26 lastname:Skladanov age: 27 lastname:Karlov 

    Create a structure with last name, age and gender. Create an array where you will store structure objects, read and fill structures. In parallel, sum up the age of men, then divide the sum of ages by the number of men, and walk again through the array, displaying the names of those who are below average