Average age in a family - Core dump error

Asked

Viewed 51 times

0

the code below is intended to read a Qtd amount of people in a family, receive the sum of ages and present the average at the end:

#include <stdio.h>

int main()
{

    int qtd, idade, i;
    float soma=0 , media;

    printf("Informe quantos membros há na família: ");
    scanf("%d\n", qtd);

        do{

            printf("informe a idade: ");
            scanf("%d\n", idade);

            soma += idade;
            i++;

        }while(i<= qtd);

        media = soma / qtd;
        printf("a média de idade da família é: %f\n",media);

    return 0;
}

It occurs Segmentation fault (sometimes followed by core dump) so it makes the literacy of qtd

not intending what access violation I committed in the above code.

  • 2

    where is the "i" boot? but the error is here: "scanf("%d n", Qtd);" --> scanf("%d n", &Qtd);"

  • When you want to read a scalar value, the arguments after the format string should be pointers

  • @zentrunix the initialization of the variable in the global scope gave no difference (i=0). In fact, I forgot the '&' but still, after reading the Qtd it waits for the input of something else and only then enters the while loop. Finally it either generates an incorrect value or... Segmentation fault.

  • 1

    and where is the "&" in the "age" scanf" ?

3 answers

2


Some syntax fixes are missing from your code.
For example it is not necessary to put \n in his scanf, after reading it already gives a \n automatic, and another that you forgot to put the & before the variable that will receive the value, and following the logic of your do-while you would read one more age.
at the end the code with these small changes would look like this:

#include <stdio.h>

int main()
{

    int qtd, idade, i=0;
    float soma=0 , media;

    printf("Informe quantos membros há na família: ");
    scanf("%d", &qtd);

        do{

            printf("informe a idade: ");
            scanf("%d", &idade);

            soma += idade;
            i++;

        }while(i< qtd);

        media = soma / qtd;
        printf("a média de idade da família é: %f\n", media);

    return 0;
}

1

Failed to initialize the average variable and put & in the scanf

#include <stdio.h>

int main()
{

    int qtd, idade, i;
    float soma=0 , media=0;

    printf("Informe quantos membros há na família: ");
    scanf("%d\n",&qtd);

        do{

            printf("informe a idade: ");
            scanf("%d\n",&idade);

            soma += idade;
            i++;

        }while(i<= qtd);

        media = soma / qtd;
        printf("a média de idade da família é: %f\n",media);

    return 0;
}

0

Exactly personal. I made trivial mistakes like the absence of '&'...

It just wasn’t clear to me exactly why he was giving Segmentation fault when applying (unnecessarily. My error) "n" when reading Qtd.

Anyway, following the guidance of colleagues I arrived at the result I wanted.

OBS: while(i < qtd) is the correct logic. Thank you!

int main() {

    int qtd,  i=0;
    float soma=0 , media, idade;

    printf("Informe quantos membros há na família: ");
    scanf("%d", &qtd);

        do{

            printf("informe a idade: ");
            scanf("%f", &idade);

            soma += idade;
            i++;

        }while(i < qtd);

        media = soma / qtd;
        printf("a média de idade da família é: %f\n", media);

    return 0;
}
  • It gave error because the function scanf expects to receive as parameter the address of the variable that will receive the content read and you were passing as address the contents of the variable that, in this case, it was memory junk and probably indicating an address outside the addressable space of your program.

Browser other questions tagged

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