Language C - Summation problem within a function

Asked

Viewed 58 times

1

Good afternoon.

The purpose of the programme is as follows. I have to have the user type in how many students the classroom has, so that it sends this number to a function (in this case, each subject is a function), and within these functions I have another generic function that I use for the user to type in the notes and make the average.

/*Fazer um programa para calcular as médias de cada disciplina utilizando função,
onde cada disciplina é uma função diferente e na função principal mostre todas as médias;*/
#include<stdio.h>
#include<stdlib.h>
#include<locale.h>

float matematica(int);
float portugues(int);
float biologia(int);
float geografia(int);
float fisica(int);
float digitarNotasFor(int);
int i;

int main(void) {

    setlocale(LC_ALL,"Portuguese");
    int qtd;

    printf("Digite a quantidade de alunos em sala = ");
    scanf("%d",&qtd);

    matematica(qtd);

    system("PAUSE");
    return 0;
}

float matematica(int n) {
    printf("\nMatemática\n");
    float media = digitarNotasFor(n);
    return printf("\nA média em matemática é = %.2f\n",media);
}

float digitarNotasFor(int n) {

    float notas[n],contaMedia = 0;
    printf("\nDigite as notas\n");
    for(i = 0; i < n; i++) {
        printf("Aluno[%d] = ",i);
        scanf("%d",&notas[i]);
        contaMedia = contaMedia + notas[i];
    }

    return (contaMedia/n);
}

Summarizing the problem, it enters the two functions, but when returns me the average, always appears 0. From what I noticed, the value of the sum of the variable contaMedia continues with the same constant value that I put in to not bring 'garbage' from my system. That is, is 0 in case, returns me 0 the average, if I put 1, returns me 1.

NOTE: I summarized the code, removing the other functions/ matters to get better.

1 answer

2

I discovered my mistake, when using the type float, inside the scanf is "%f" and not %d, which would be for the type int.

At the time, it was

scanf("%d",&notas[i]);

And in reality, it should stay that way

scanf("%f",&notas[i]);

Browser other questions tagged

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