Beginner009
Beginner009

Reputation: 1

How is it possible to count, how many elements in an array satisfy certain conditions (C++)?

I am a beginner in C++, and I need help in a basic question. I have a dataset (array) and the task is to count how many elements satisfy the given conditions.

A company stores the age and salary of its employees. We need to write a program that tells you how many people over the age of L have a salary of less than M.

Input

Number of workers in the first line of standard input (0≤N≤100), age limit (1≤L≤100) and the salary limit (1≤M≤2,000,000) and below it is one person's age per line (1≤K≤100) and salary (1≤F≤2,000,000).

Output

In a single line of standard output, those over the age of L with a salary of less than M number of workers must be written.

#include <iostream>
using namespace std;
int main()
{
    int N;
    int K;
    int L;
    int F;
    int M;
    cin >> N >> K >> L >> F >> M;
    int arr[N];
    for (int i=0; i<N; ++i)
    {
        cin >> arr[i];
    }
    int DB=0;
    for (int i=0; i<N; ++i)
 {
                 for (int DB; K>L && F<M; DB=DB+1)
                    {


                    }
    }
    cout << DB << endl;
    return 0;
}

I tried to solve the problem using for-loops. It is obvious, that there are basic mistakes in the code. Could you help me solving the problem? Is the code above a good method or is there a much better solution?

Thank you for the help in advance.

Upvotes: 0

Views: 1968

Answers (2)

AndersK
AndersK

Reputation: 36082

Here is a way to do it, mind you this is just an example based on the comments to your post.

#include <iostream>
#include <vector>
#include <algorithm>

// you need a way to capture the information of age and salary

class Employee
{
public:
  Employee(int age, int salary) : m_age(age), m_salary(salary) 
  {}
  int Salary() const { return m_salary; }
  int Age() const { return m_age; }
private:
  int m_age{0};
  int m_salary{0}; 
};


int main()
{
  // an array of the employees with age and salary, avoid using native arrays
  std::vector<Employee> employees{{21,10000},{22,12000},
                                  {54,54500},{62,60000}, 
                                  {32,32000}};
  // some salary limit or use cin to read in it
  auto salaryLimit = 33000;

  // use count_if to count the number of employees under salary limit
  auto nrOfEmployees = std::count_if(employees.begin(), employees.end(), 
                       [=](const Employee& e){return e.Salary() < salaryLimit;});
  std::cout << nrOfEmployees << std::endl;

  return 0;
}

If you want to try the code

https://onlinegdb.com/Sy-qCXN8v

Upvotes: 0

lawruble13
lawruble13

Reputation: 535

That's certainly a creative way of approaching the problem! A more straightforward way to approach this is to go over each element, and check if it matches, as follows:

#include <iostream>
using namespace std;
int main(){
  int numWorkers, ageLimit, salaryLimit, matchCount=0;
  cin >> numWorkers >> ageLimit >> salaryLimit;
  for (int i = 0; i < numWorkers; i++){
    int age, salary;
    cin >> age >> salary;
    if (age > ageLimit && salary < salaryLimit){
      matchCount++;
    }
  }
  cout << matchCount << endl;
  return 0;
}

Upvotes: 2

Related Questions