How can I apply an "if or while" condition to a "struct" struct fix in C? [closed]

1

I have this problem and in the code that gives us example structures are handled but from what I understand a structure can not be compared, what I want to do is compare if register 1 is greater than 18 then increase a counter and so on for the rest.

    
asked by Fernando28R 18.09.2017 в 06:15
source

1 answer

1

You can compare the values of the elements of the structures by accessing the notation estructura.elemento or in the case that the structure is referenced with a pointer with estructura->elemento .

As an example if the structure and arrangement or vector are as follows:

struct persona{
    int sexo;
    int edad;
    int estado;
};

struct persona lista[15];

You can scan the list as follows:

// variables para almacenar los resultados
int hombressolteros = 0;
int hombrescasados = 0;
int mujeressolteras = 0;
int mujerescasadas = 0;
int totalsolteros = 0;
int totalcasados = 0;
int menoresedad = 0;

for (int i = 0; i < 15; i++) {

    // Comprobar si es mayor de edad
    if (lista[i].edad < 18) {
        menoresedad++;
    }
    // Comprobar si es hombre
    else if (lista[i].sexo == 1) {

        // Determinar si esta casado
        if (lista[i].estado == 1) {
            hombrescasados++;
        }
        else {
            hombressolteros++;
        }
    }
    // si es mujer
    else {
        // Determinar si es casada
        if (lista[i].estado == 1) {
            mujerescasadas++;
        }
        else {
            mujeressolteras++;
        }
    }
}

// Totales
totalcasados = hombrescasados + mujerescasadas;
totalsolteros = hombressolteros + mujeressolteras;
    
answered by 18.09.2017 / 08:20
source