Program has memory junk 2 in C

Asked

Viewed 119 times

3

That’s the question of the program:

Make a program, using the function below, that displays the highest salary of each department of a company and how many employees earn the department’s highest salary. For each department, the program should read the department code and the amount of employees, and for each employee, registration and salary. End of reading departments: department code = 0. Perform the function of a department to process the employees of a department. This function should receive as a parameter the amount of department employees, read the data of each employee, find out the department’s highest salary and how many employees earn this higher salary, storing them in the variables whose addresses are provided in the function call.

I performed 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 very much.

  • about memory junk search a little about the command fflush(stdin); but and basically put this command before reading data.

1 answer

2

Matheus, beauty? the/

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

i. Inappropriate use of the semicolon character

maior = func[0].salario;;

ii. Calling a function that has not been created, using non-valued argument

dep (functot);

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

scanf ("%f%f", &funz[i].salario, &funz[i].matricula);

Calm down. These types of mistakes happen and are common for those who are starting out in the programming. I saw that you tried very hard, but I think you did not fully understand what asked the question and ended up implementing a somewhat confusing logic. Come on:

  1. In function a department, you must carry out reading of employees' data and not only verify which is the highest department salary;
  2. Still for this function, you should check how many employees receive salary exactly equal to the department’s highest salary. Thus, its variable is cont is not being used well;
  3. In the main, you should perform the reading of the departments while the value provided for the department code is different from 0;
  4. For each valid department (cod_dep != 0), the readings of the information of the total of employees will be made. This information covers the enrollment and the salary. Thus, the name field in the working structure is unnecessary.

A little question, future programmer: 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 a sense of the exact total of elements needed - which is not our case, since we are working with an undefined amount of departments. So, what do you think about trading the use of vectors for chained lists? It will be less laborious and much more understandable, as it will allow you to define each element of the list as a department, with its code and an X number of employees defined. Ah, for employees, you can use a vector of structures. It will be easy to implement.

I will share the structures that will help you with the resolution. 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 do not succeed or feel difficulties, you can contact me who will be happy to help you.

A hug! =]

  • 1

    Speak, all right? So, I get what you mean. I’m pretty confused still in the matter of how to use pointer and structs. Because I am still walking in programming, it makes no sense to use pointers and structs in an exercise and I did it by even trying. The question is: how could I improve my C learning with respect to pointers and structs/vectors? Thank you very much right now.

  • Speak, Matheus. All in peace here, and with you? Forgiveness for the late reply... The use of pointers and structs is extremely common in programming and characterizes what actually comes to be data structures. As pointed out in the answer, it is possible to simplify the resolution of the problem by making use of chained lists. Summarizing and straightforward, chained lists are a sequence of structs that store two types of data: information and a pointer to the next element. Analyze the Listadepts structure and identify them. Note that the f pointer will point to the Developer employee array.

  • Next: the *Prox pointer will always point to the next element in the list, which will be a Listadepts-type struct with a pointer to the employee array and a pointer to the next element in the list. What is the advantage of working with lists? In the problem cited by you, we are unaware of the amount of departments to be provided - so the use of a nonflexible structure, which is the vector, makes the resolution complex. With the use of lists, resolution is simplified as each element will be created as a new department is provided. That is, we can have N.

  • I see that these concepts are new to you. At first, they may seem too complex, just as they were for me. So I’m going to recommend a workbook that demonstrates in a simplified and didactic way the main data structures used in programming. I think it will be of great value to you! Link: https://goo.gl/1rcmdY

Browser other questions tagged

You are not signed in. Login or sign up in order to post.