Language C - Summation problem within a function [closed]

1

Good afternoon.

The program has the following purpose. I have to send the user to enter how many students the room has, so that he sends that 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 the notes and 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 goes into both functions, but when it returns the mean, it always shows 0. From what I've noticed, the value of the sum of the variable contaMedia continues with the same constant value that I set to not bring 'junk' out of my system. That is, it's 0 in the case, it returns me 0 to the average, if I put 1, it returns me 1.

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

    
asked by anonymous 29.10.2017 / 21:17

1 answer

2

I found out what was my error, when using the float type, inside the scanf is "% f" and not% d, which would be for type int.

At the time, it was

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

And in fact, it should stay that way

scanf("%f",&notas[i]);
    
29.10.2017 / 21:30