Program has memory garbage 2 in C

3

This is the program question:

Make a program, using the function below, that displays the highest salary of each department in a company and how many employees earn the highest salary in the department. For each department, the program should read the department code and the number of employees, and for each employee, enrollment and salary. Completion of departments reading: department code = 0. Make the function a_ department to process the employees of a department. This function should receive as a parameter the number of employees of the department, read the data of each find the department's highest salary and how many employees earn this salary, storing them in the variables whose addresses are given in the function call.

I've made the following code:

#include <stdio.h>


struct funcionario
{
    char nome[20];
    float salario;
    char matricula[10];
};

void um_departamento(struct funcionario *func, int *numfunc){
    int i, maior,cont=0;
    maior = func[0].salario;;

    for (i = 0; i < *numfunc; i++){

        if (func[i].salario > maior){
            maior = func[i].salario;
            cont++;
        }
    }
    printf("%d %d", maior,cont);

}

int main(){
    int departamento, i, functot, coddep, depzin,num=20;
    struct funcionario funz[num];

    printf ("Digite o departamento e seu codigo");
    scanf ("%d %d", &departamento, &coddep);

    for (i = 0; i < departamento; i++){
        dep (functot);
        print ("Digite a matricula e o salário");
        scanf ("%f%f", &funz[i].salario, &funz[i].matricula);




    }

    um_departamento(funz, &num);

    return 0;

}

How can I fix it? Thank you in advance.

    
asked by anonymous 15.05.2018 / 23:52

1 answer

2

Matheus, beauty? \ o /

So your code has some syntax errors that undermine the implemented logic. I'll list them:

i. Inappropriate use of the semicolon character

maior = func[0].salario;;

ii. Calling a function that was not created, using a valueless argument

dep (functot);

iii. Reading a float to a char variable (funz.matricula)

scanf ("%f%f", &funz[i].salario, &funz[i].matricula);
Calm down. These types of errors happen and are common to anyone who is starting programming. I saw that you struggled a lot, but I do not think you fully understood what you were asking for, and you came up with a somewhat confused logic. Come on:

  • In the a_department function, you should read the employee data and not only check the department's highest salary;
  • Still for this function, you should check how many employees are paid exactly the same as the highest salary in the department. So your variable cont is not being used well;
  • In main , you should read departments while the value provided for department code is different from 0;
  • For each valid department ( cod_dep ! = 0), the total employee information readings will be performed. This information covers enrollment and salary. Thus, the name field in the official structure is unnecessary.
  • A question, future programmer: Do you consider it feasible to use vector to solve the problem? Well, vectors are not flexible structures and are more recommended in contexts where we have notion of the exact total of necessary elements - which is not our case, since we are working with an undefined number of departments. So how do you change the use of vectors by threaded lists? It will be less cumbersome and much more understandable because it will allow you to define each element of the list as a department, with its code and an amount of defined employees X. Oh, for the employees, you can rather use a vector of structures. It will be easy to implement.

    I'll share the frameworks that will help you resolve. Take them as a basis for your implementation.

    struct listaDepts
    {
        int cod_dep;
        struct funcionario *f;
        struct listaDepts *prox;
    };
    typedef struct listaDepts ListaDepts;
    
    struct funcionario
    {
        char matricula[10];
        float salario;
    };
    typedef struct funcionario Funcionario;
    

    Anything, if you can not or feel difficult, you can contact me and it will be my pleasure to help you.

    A hug! =]

        
    17.05.2018 / 16:50