I get the salary but not the name of the two employees

-2

The exercise consists of making an employee-type structure that contains:

  • namemployed
  • sex
  • salary

and take out on the screen the name of the two employees with the highest and lowest salary.

The output must be:

  

How many employees does the company have: 3

     

employee 0: Jose salary 0: 100 sex 0: masc

     

employee 1: Alex salary 1: 200 sex 1: masc

     

employee 2: Carlos salary 2: 500 sex: masc

     

Carlos is the most charged: 500.

     

José is the least charged: 100

My code is as follows:

#include<stdio.h>
#include<string.h>

#define N 50
typedef struct {

    char nombre[N];
    float sueldo;
    char sexo[N];

} empleado;

void cambio (char palabra [N]);

int main(){

    int n ; 
    printf("Cuantos empleados tiene la empresa:");
    scanf("%d",&n);
    int i ; 
    empleado emp[n];
    for (i=0;i<n;i++){
        printf("Nombre empleado[%d]:",i);
        fflush(stdin);          
        fgets(emp[i].nombre,N,stdin);           
        cambio( emp[i].nombre);

        printf("\nSuedo empleado[%d]:",i);
        scanf("%f",&emp[i].sueldo);
        printf("\nSexo del empleado[%d]:",i);
        fflush(stdin);
        fgets(emp[i].sexo,N,stdin);cambio(  emp[i].sexo);
        printf("\n");
    }

        int mayor , menor,j;
        mayor=0;  
        for (i=0;i<n;i++){
            if (mayor<emp[i].sueldo){
                mayor=emp[i].sueldo;
            }           
        }
        menor = mayor;
         for (j=0;j<n  ; j++){
            if (menor>emp[j].sueldo){
                menor=emp[j].sueldo;
                 }
             }

            printf("\nEl empleado con mayor sueldo es : %d",mayor);
            printf("\nEl empleado con menor sueldo es: %d",menor);  
}

void cambio(char palabra[N]){       
    int i ; 
    for (i=0; i<N;i++){
        if (palabra [N] == '\n'){
            palabra[N]='
#include<stdio.h>
#include<string.h>

#define N 50
typedef struct {

    char nombre[N];
    float sueldo;
    char sexo[N];

} empleado;

void cambio (char palabra [N]);

int main(){

    int n ; 
    printf("Cuantos empleados tiene la empresa:");
    scanf("%d",&n);
    int i ; 
    empleado emp[n];
    for (i=0;i<n;i++){
        printf("Nombre empleado[%d]:",i);
        fflush(stdin);          
        fgets(emp[i].nombre,N,stdin);           
        cambio( emp[i].nombre);

        printf("\nSuedo empleado[%d]:",i);
        scanf("%f",&emp[i].sueldo);
        printf("\nSexo del empleado[%d]:",i);
        fflush(stdin);
        fgets(emp[i].sexo,N,stdin);cambio(  emp[i].sexo);
        printf("\n");
    }

        int mayor , menor,j;
        mayor=0;  
        for (i=0;i<n;i++){
            if (mayor<emp[i].sueldo){
                mayor=emp[i].sueldo;
            }           
        }
        menor = mayor;
         for (j=0;j<n  ; j++){
            if (menor>emp[j].sueldo){
                menor=emp[j].sueldo;
                 }
             }

            printf("\nEl empleado con mayor sueldo es : %d",mayor);
            printf("\nEl empleado con menor sueldo es: %d",menor);  
}

void cambio(char palabra[N]){       
    int i ; 
    for (i=0; i<N;i++){
        if (palabra [N] == '\n'){
            palabra[N]='%pre%';            
        }           
    }
}
'; } } }
    
asked by Alvaro 11.04.2018 в 12:57
source

2 answers

3

You have some errors in the code:

VLA

Variable Length Array are not something supported by the C99 standard. While they are supported by some compilers or their extensions is not a portable feature and, therefore, it is something to avoid.

What is a VLA? It is a fixed-size array whose size is determined by a variable, that is, its size is decided at run time:

int n ; 
printf("Cuantos empleados tiene la empresa:");
scanf("%d",&n);
empleado emp[n]; // <<--- VLA

The alternative is either to determine a maximum number of elements at compile time:

empleado emp[N]; // Por poner un ejemplo

Or use dynamic memory:

empleado *emp = (empleado*)malloc(n*sizeof(empleado));

// ...

free(emp); // Liberamos la memoria reservada

Beware of fflush

fflush ... that big misunderstood and strangely used even by those who teach programming. The documentation of the function clearly states that it should only be used with outflows:

  

In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).

So better avoid that from

fflush(stdin);

If you need to clean the input buffer you can create a function that eliminates the characters left over until the line break:

void limpiar_entrada()
{
  for( char c = getchar(); c != '\n'; c = getchar() );
}

// ...

printf("Nombre empleado[%d]:",i);
limpiar_entrada();
fgets(emp[i].nombre,N,stdin);

However, this can be saved if it is not a requirement that names can contain spaces. In this case you can replace fgets with scanf :

scanf("%s",emp[i].nombre);

Additionally, the function cambio has no use ... it is poorly implemented (iterates from 0 to N but in each iteration you use N instead of i ) but it is also unnecessary since both fgets and scanf end the text string with 'for' .

The explanation in this point is also applicable for sex.

And that's it, with these simple changes your code will fulfill its purpose, however it would be interesting to follow some additional tips:

Declare the variables within the for

There is no need to extend the life of the variables beyond what is strictly necessary ... from C99 it is possible to declare variables directly in float :

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

If it is not an express requirement, that salaries use integers

The types double and float allow you to store significantly large numbers ... at the cost of sacrificing precision. In the case of double the accuracy is 6 digits and about 12 in the case of float . The rest of the digits can be considered junk. This has its implications:

To give an example, in C it is possible to compare integers directly ... beware of doing it with int . Why? Because the non-representative digits are going to be taken into account in the comparison even if they are garbage digits. What you have to do in these cases is to understand that two numbers are the same if their difference is small enough:

int a, b;
if( a == b ) // ok

float c, d;
if( c == d ) // CUIDADO!!!!

if( abs(c-d) < 1e5 ) // mejor

So, use menor if possible.

The highest and lowest values can be calculated at the same time

You do not need to iterate the collection twice ... with just one loop is enough. You only need to assign a sufficiently large number to int ... this value can be set to pinion (not recommended) or let the standard library give you the largest possible value (so the worker's salary only may be equal or lower).

If you have also changed the type used for the salary to limits.h you can benefit from the library int to say what is the highest value that can be stored in the type float (there is no %code% , I'm sorry):

#include<limits.h>

int mayor = 0;
int menor = INT_MAX;
for (int i=0;i<n;i++){
    if (mayor<emp[i].sueldo){
        mayor=emp[i].sueldo;
    }
    if (menor>emp[i].sueldo){
        menor=emp[i].sueldo;
    }
}

printf("\nEl empleado con mayor sueldo es : %d",mayor);
printf("\nEl empleado con menor sueldo es: %d",menor);  

To get the employee's name next to their salary

In this case, it's worth going over to the pointers:

#include<limits.h>

empleado* mayor = emp; // por defecto el primer empleado
empleado* menor = emp; // por defecto el primer empleado
for (int i=1;i<n;i++){ // No empezamos en 0 porque ese es el que esta seleccionado por defecto
    if (mayor->sueldo<emp[i].sueldo){
        mayor = &emp[i];
    }
    if (menor->sueldo>emp[i].sueldo){
        menor = &emp[i];
    }
}

printf("\n%s tiene el mayor sueldo: %d",mayor->nombre,mayor->sueldo);
printf("\n%s tiene el menor sueldo: %d",menor->nombre,menor->sueldo);
    
answered by 11.04.2018 / 13:56
source
1

You are not making scanf or name or sex:

#include<stdio.h>
#include<string.h>

#define N 50
typedef struct {

    char nombre[N];
    float sueldo;
    char sexo[N];

} empleado;

void cambio (char palabra [N]);

int main(){

    int n ; 
    printf("Cuantos empleados tiene la empresa:");
    scanf("%d",&n);
    int i ; 
    empleado emp[n];
    for (i=0;i<n;i++){
        printf("Nombre empleado[%d]:",i);
        scanf("%f",&emp[i].nombre);
        fflush(stdin);          
        fgets(emp[i].nombre,N,stdin);           
        cambio( emp[i].nombre);

        printf("\nSueldo empleado[%d]:",i);
        scanf("%f",&emp[i].sueldo);
        printf("\nSexo del empleado[%d]:",i);
        scanf("%f",&emp[i].sexo);
        fflush(stdin);
        fgets(emp[i].sexo,N,stdin);cambio(  emp[i].sexo);
        printf("\n");
    }

        int mayor , menor,j;
        mayor=0;  
        for (i=0;i<n;i++){
            if (mayor<emp[i].sueldo){
                mayor=emp[i].sueldo;
            }           
        }
        menor = mayor;
         for (j=0;j<n  ; j++){
            if (menor>emp[j].sueldo){
                menor=emp[j].sueldo;
                 }
             }

            printf("\nEl empleado con mayor sueldo es : %d",mayor);
            printf("\nEl empleado con menor sueldo es: %d",menor);  
}

void cambio(char palabra[N]){       
    int i ; 
    for (i=0; i<N;i++){
        if (palabra [N] == '\n'){
            palabra[N]='
#include<stdio.h>
#include<string.h>

#define N 50
typedef struct {

    char nombre[N];
    float sueldo;
    char sexo[N];

} empleado;

void cambio (char palabra [N]);

int main(){

    int n ; 
    printf("Cuantos empleados tiene la empresa:");
    scanf("%d",&n);
    int i ; 
    empleado emp[n];
    for (i=0;i<n;i++){
        printf("Nombre empleado[%d]:",i);
        scanf("%f",&emp[i].nombre);
        fflush(stdin);          
        fgets(emp[i].nombre,N,stdin);           
        cambio( emp[i].nombre);

        printf("\nSueldo empleado[%d]:",i);
        scanf("%f",&emp[i].sueldo);
        printf("\nSexo del empleado[%d]:",i);
        scanf("%f",&emp[i].sexo);
        fflush(stdin);
        fgets(emp[i].sexo,N,stdin);cambio(  emp[i].sexo);
        printf("\n");
    }

        int mayor , menor,j;
        mayor=0;  
        for (i=0;i<n;i++){
            if (mayor<emp[i].sueldo){
                mayor=emp[i].sueldo;
            }           
        }
        menor = mayor;
         for (j=0;j<n  ; j++){
            if (menor>emp[j].sueldo){
                menor=emp[j].sueldo;
                 }
             }

            printf("\nEl empleado con mayor sueldo es : %d",mayor);
            printf("\nEl empleado con menor sueldo es: %d",menor);  
}

void cambio(char palabra[N]){       
    int i ; 
    for (i=0; i<N;i++){
        if (palabra [N] == '\n'){
            palabra[N]='%pre%';            
        }           
    }
}
'; } } }
    
answered by 11.04.2018 в 13:06