Close loop when typing specific character

Asked

Viewed 605 times

5

Hello! I need to resolve the following question, but I’m not getting it.

Write an algorithm that calculates the average arithmetic of the students' 3 grades (number undetermined of pupils) of a class. The algorithm should read, in addition to the notes, the code of the student and should be terminated when the code is zero

I wrote the code below, I do not know what is wrong, because when "code = 0" it does not close. Someone can help me?

main()
{

int codigo, n1, n2, n3, i;
float media;


while(codigo != 0){
    for(i= 0; i >= 0; i++){
            printf("Digite o código do aluno:\n");
            scanf("%d", &codigo);
            printf("Digite as 3 notas do aluno:\n");
            scanf("%d %d %d", &n1, &n2, &n3);
            media = ((n1+n2+n3)/3);
            printf("Media = %.2f\n", media);
    }
}
return 0;
}
  • You don’t need the block for, note that you declare i and the variable is not used inside the block.

1 answer

2


The line with the for this wrong.

Note that it is written for(i= 0; i >= 0; i++), which means:

for i of 0, while i > = 0, incrementing i in one unit per cycle, run...

That is, as your variable i is initialized with 0, and she’s always augmented, she’ll never be less than 0, so this for will never be finalized.

In fact, there is, if you like, no reason for it (at least according to the specification of the exercise cited), so it can be removed.

main() {
    int codigo, n1, n2, n3;
    float media;

    while(codigo != 0){
        printf("Digite o código do aluno:\n");
        scanf("%d", &codigo);
        printf("Digite as 3 notas do aluno:\n");
        scanf("%d %d %d", &n1, &n2, &n3);
        media = ((n1+n2+n3)/3);
        printf("Media = %.2f\n", media);
    }
    return 0;
}

Another factor that should be observed is that when typing 0 for the student code, we do not want the grades to be read (since we are not associating them with a student). We then read the notes only if the code is different from 0 (codigo != 0).

main() {
    int codigo, n1, n2, n3;
    float media;

    while(codigo != 0){
        printf("Digite o código do aluno:\n");
        scanf("%d", &codigo);
        if ( codigo != 0 ) {
            printf("Digite as 3 notas do aluno:\n");
            scanf("%d %d %d", &n1, &n2, &n3);
            media = ((n1+n2+n3)/3);
            printf("Media = %.2f\n", media);
        }
    }
    return 0;
}

I hope I’ve helped.

  • 1

    In fact, I’ve reviewed my code and if it’s done the way it was first mentioned, it won’t even start, because it doesn’t have any input before the while. It looks like this: #include <stdio. h> #include <stdlib. h> main() { int code, n1, N2, N3; float media; printf("Enter student code: n"); scanf("%d", &code); while(code !=0), #Xa; printf("Type the 3 student notes: n"); scanf("%d %d %d", &n1, &N2, &N3); media = ((n1+N2+N3)/3); printf("Media = %.2f n", media); printf("Enter student code: n"); scanf("%d", &code); } Return 0; }

  • @Andressaoliveira well observed! I had noticed this detail! Another option, if you want, is to initialize codigo with any non-zero value, so it will enter the loop, and you will not need to duplicate code. But your solution is equally valid.

Browser other questions tagged

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