Zeroes only appear in the division

Asked

Viewed 92 times

-1

The result of division is only zero, which is wrong?

#include <stdio.h>
void main()
{   
    double Nota1, Nota2, MediaPond;
    printf("Escreva a nota 1: ");
    scanf("%f", &Nota1);
    printf("\nEscreva a nota 2: ");
    scanf( "%f", &Nota2 );
    MediaPond = ((Nota1 * 2) + (Nota2 * 3)) / 5;
    printf("\nA media ponderada e: ");
    printf("%.2f", &MediaPond);
    system("pause");
}

This is the return of the program

Write the note 1: 2

Write note 2: 5

The weighted average is: 0.00

  • Do you know you can only accept one answer? You can vote for all you want on the entire site, but only accept one answer to your question. It’s OK to accept any one you think is the right one, but many users end up changing the acceptance thinking they can accept more than one, so when they accept a new one and stop accepting the other one. If you did it because you wanted to, no problem, if you did it by mistake it would be good to review.

2 answers

1

There are other errors, but not what you are describing. You need to print in the format of lf for double and it makes no sense to have the address of a variable printed with &, nor need to create this variable, using the normal patterns of how to code:

#include <stdio.h>

int main() {   
    double nota1, nota2;
    printf("Escreva a nota 1: ");
    scanf("%lf", &nota1);
    printf("\nEscreva a nota 2: ");
    scanf("%lf", &nota2);
    printf("\nA media ponderada e: ");
    printf("%.2lf", (nota1 * 2 + nota2 * 3) / 5);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1


The main error is in reading the variables declared as double. When the function was used scanfyou have stored these variables using the %f,the correct in this case would be using %lf. Example :

#include <stdio.h>
#include <stdlib.h>

int main() {

double n1,n2;

printf("Nota 1: ");
scanf("%lf",&n1);

printf("Nota 2: ");
scanf("%lf",&n2);

printf("Media : %.2lf\n",(n1 + n2) / 2);

system("pause");
return 0;

}

Browser other questions tagged

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