Program error of average in C, in result view

Asked

Viewed 43 times

0

I’m a beginner in C programming, and in college we had to develop a program to make the average of 4 notes, and after typed, make the display, the program works without syntax error, but there’s something wrong in the final result, which displays the average result, for me, any value I put it returns the result as 0. Can anyone tell me where the error is? This is the program:

int main()
{
    float media,nota1,nota2,nota3,nota4;
    printf("\nEntre com as 4 notas: \n");
        scanf("%f" ,&nota1);
        scanf("%f" ,&nota2);
        scanf("%f" ,&nota3);
        scanf("%f" ,&nota4);
    media=(nota1+nota2+nota3+nota4)/4;
    printf("\nA media e': %f", &media);
}
  • 1

    Here: printf("\nA media e': %f", &media); doesn’t have this & before the media. Use: printf("\nA media e': %f", media);. Note that it is in the scanf function that we must provide the variable address.

  • Now that you’ve shown that I even remembered you can’t have the & on the printf

1 answer

0


The corrected program is this:

#include<stdio.h>
#include<conio.h>

int main()
{
    float media,nota1,nota2,nota3,nota4;
    printf("\nEntre com as 4 notas: \n");
        scanf("%f", &nota1);
        scanf("%f", &nota2);
        scanf("%f", &nota3);
        scanf("%f", &nota4);
    media=(nota1+nota2+nota3+nota4)/4;
    printf("\nA media e': %f", media);
}

Browser other questions tagged

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