How to show the number of employees by sex?

2

I have the following code which shows me the data of N number of employees admitted My question is this: How do I show the number of employees by sex?

int main(void){
               /*Declara las variables para los ciclo for*/
int i = 0, n = 0;

               /*Declara estructura person*/
struct person{
    char dni[14];
    char civil_status[15];
    char sex[15];
};
              /*Declara employee, arreglo de la estructura person*/
struct person employee[max];

/*Se pide cuantos registros de employees se guardaran*/
cout<<"Cuantos datos quieres ingresar? \n";
cin>> n;

              /*Ciclo for que va a recorrer según la cantidad escrita anteriormente*/
for (i = 0; i < n; i++){
    cout<<"\n Escriba la Cedula "<< i+1<<":";
    cin>> employee[i].dni;

    cout<<"\n Escriba el Estado Civil "<< i+1<<":";
    cin>> employee[i].civil_status;

    cout<<"\n Escriba el Sexo "<< i+1<<":";
    cin>> employee[i].sex;
}

cout<<"\n El registro de employes que se introdujeron son: \n\n";

/*Ciclo for que muestra el listado de registro ingresados*/
for (i = 0; i < n; i++){
    /*Se llama al arreglo employee seguido de la variable dni*/
    cout<< employee[i].dni;
    cout<<"\t"<<employee[i].civil_status;
    cout<<"\t"<<employee[i].sex<<"\n\n";
  }
   system("pause");
}

An example of data entry:

  

How many data do you want to enter? 2
  Write the Cedula 1: 012345
  Write the Civil Status 1: single
  Write Sex 1: male
  Write the Certificate 2: 67891
  Write Marital Status 2: married
  Write the Sex 2: female

     

The register of employes that were introduced are:

     

012345 male bachelor 67891 married female

    
asked by Adolfo Jose Suarez Rondon 29.03.2018 в 17:16
source

3 answers

4

Use std::count_if :

using std::literals::string_literals;

auto mujeres = std::count_if(std::begin(employee), std::end(employee),
               [](const person &p) {
                   return p.sex == "femenino"s;
               });

Accounts for women, men will result in subtracting the number of women from max , this allows you to avoid counting each sex separately, avoiding the need for if , which would negatively affect performance .

The std::count_if function is available by including the header <algorithm> , it will tell you all the elements between the start iterator (first parameter) and the end iterator (second parameter) that meet a condition (the third parameter).

The functions std::begin and std::end are available in the header <iterator> and are the ones that calculate the start and end of your training employee .

The using std::literals::string_literals; statement is the one that allows you to use the string-defined literals of characters, so in the lambda passed to std::count_if we can write

return p.sex == "femenino"s;

instead of

return p.sex == std::string{"femenino"};

You can not use the iterators and take advantage of the fact that employee is a formation and apply arithmetic of pointers:

auto mujeres = std::count_if(employee, employee + n,
               [](const person &p) {
                   return p.sex == "femenino"s;
               });

This also avoids the possible problem of counting wrongly when n and max do not contain the same value.

Other things to consider.

In C ++ struct is not part of the type, so you do not need to prefix it to declare structures.

In C ++ the functions that do not receive parameters are enough to write them with an empty parenthesis.

The function main must return a value .

I do not know what type has max , but if it is not a known type at compile time, you will have compatibility problems with your code, read this thread to know more about it.

The variables of the for can be declared in the loops themselves, it is not necessary to do it outside.

    
answered by 29.03.2018 в 19:01
2

Suppose you have two types of sexes, masculino and femenino .

Simply create a integer for each of them initialized to 0, go through the data with for and go increasing the cantidad in each of the corresponding integer :

int masculino = 0, femenino = 0;
for(int i = 0; i < n; i++){
    if(employee[i].sex == "masculino")
        masculino++;
    else if (employee[i].sex == "femenino")
        femenino++;
}
cout << "Número de empleados masculinos: " << masculinos;
cout << "Número de empleados femeninos: " << femeninos;
    
answered by 29.03.2018 в 18:10
-1

The data you already have, you only have to count how many records have in their attribute sex the value 'masculine' or 'feminine' and count them. You can define a variable for each one and then display them, for example:

int cantidadMasculinos = 0;
int cantidadFemeninos = 0;    
/*Ciclo for que muestra el listado de registro ingresados*/
for (i = 0; i < n; i++){
 /*Se llama al arreglo employee seguido de la variable dni*/
 cout<< employee[i].dni;
 cout<<"\t"<<employee[i].civil_status;
 cout<<"\t"<<employee[i].sex<<"\n\n";
  if(employee[i].sex = 'masculino'){
   cantidadMasculinos += 1;
   }else{cantidadFemeninos += 1;}
}
cout<<"Cantidad de empleados masculinos: "<<cantidadMasculinos;
cout<<"Cantidad de empleados femeninos: "<<cantidadFemeninos;
system("pause");
}
    
answered by 29.03.2018 в 18:14