End loop when typing specific character

5

Hello! I need to resolve the following question, but I can not.

Write an algorithm that computes the mean arithmetic of the students' 3 grades (number indeterminate number of students) of a class. O algorithm should read, in addition to the notes, the student and should be terminated when the code is equal to zero

I wrote the code below, I do not know what is wrong, because when "code = 0" it does not close. Can anyone 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;
}
    
asked by anonymous 19.09.2015 / 23:53

1 answer

2

The line with for is wrong.

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

  

for i of 0, while i> = 0, incrementing i by one unit per cycle, execute ...

That is, since its variable i is initialized with 0 , and it is always incremented, it will never be less than 0 , so this for will never be finalized. p>

In fact, there is no motive 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 to note is that when you type 0 into the student code, we do not want the notes 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 have helped.

    
20.09.2015 / 00:06