Pointer function?

3

statement:

  

A student's grade is calculated from three grades assigned   between the range of 0 to 10, respectively, to a work of   bi-monthly assessment and final examination. The average   three notes mentioned above obeys the pessos: work of   laboratory: 2; semester evaluation: 3; final exam: 5. According to   the result, show on the screen whether the student is in default (mean between 0 and   2.9), recovery (between 3 and 4.9) or if it was approved. Make all the   necessary checks.

Good. I had done with a float function, which returned the variable. For learning purposes, I decided to try to do pointer, but did not work very well kkk

In the part of the function that will validate the note

  

while (var 10)

If I type 5 for example, it is 0 and 1. But I do not quite understand why .. then 0 or 1 of 1, then it enters and stays in a loop, asking to enter a valid note. Why does this happen? Thank you in advance.

follow the code:

#include <stdio.h>
#include <locale.h>

void validar(float *var){
    scanf("%f", var);

    while (var < 0 || var > 10){
        printf("\nNota invalida, tente novamente: ");
        scanf("%f", var);
    }
}

int main(){
    setlocale(LC_ALL, "portuguese");

    float lab, avaliacao, exame;

    printf("Entre com a nota do trabalho de laboratorio: ");
    validar(&lab);

    printf("Entre com a nota da avaliação bimestral: ");
    validar(&avaliacao);

    printf("Entre com a nota do exame final: ");
    validar(&exame);

    float media = (lab * 2 + avaliacao * 3 + exame * 5) / (2+3+5);

    printf((media < 3) ? "\nAluno reprovado.\n" : (media < 5) ? "\nAluno em recuperação\n" : "\nAluno aprovado\n");
}
    
asked by anonymous 17.08.2018 / 17:00

2 answers

3
while (var < 0 || var > 10)

In this case you want to validate (*var) , putting var is using the

The correct answer would be while ((*var) < 0 || (*var) > 10)

When we do:

 printf("%d\n", var);
 printf("%f\n", *var);

OUTPUT with * var = 10:

6356744
10,000000
    
17.08.2018 / 17:05
5

When you are going to learn a new concept first study hard enough what it is before you start doing it, so you can understand what is happening and enjoy it more. Just making it work is not exactly what I would call learning.

You passed the variable as a pointer. Do you understand that you are passing a pointer to a memory address where the value is actually, not the value?

So in scanf() you do not have to go through the address operator & (as exemplified in Why you do not need of the '&' in 'scanf ();'? ), since it is an address that it expects, the variable var is already an address.

In other places it does not wait for an address, it expects a value, then it can use the variable directly, it has to do an operation that takes the value of the address. This is called dereference . The * operator is used to tell the compiler that it wants the value that is in that address. It works. Note that although it is the same character, in this context it is neither the multiplication nor the type indicator to be a pointer.

while (*var < 0 || *var > 10) {
    printf("\nNota invalida, tente novamente: ");
    scanf("%f", var);
}

That's better, less redundant code:

#include <stdio.h>

void validar(float *var){
    while (1) {
        scanf("%f", var);
        if (*var >= 0 && *var <= 10) return;
        printf("\nNota invalida, tente novamente: ");
    }
}

int main() {
    float lab, avaliacao, exame;
    printf("Entre com a nota do trabalho de laboratorio: ");
    validar(&lab);
    printf("Entre com a nota da avaliação bimestral: ");
    validar(&avaliacao);
    printf("Entre com a nota do exame final: ");
    validar(&exame);
    float media = (lab * 2 + avaliacao * 3 + exame * 5) / (2 + 3 + 5);
    printf((media < 3) ? "\nAluno reprovado.\n" : (media < 5) ? "\nAluno em recuperação\n" : "\nAluno aprovado\n");
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

But congratulations on having improved on what I already knew, many questions here we see the person continue to err on the same things.

    
17.08.2018 / 17:10