Percentage calculation

Asked

Viewed 45 times

-1

Can anyone tell me if I’m calculating the right percentage?

13. Um dado é lançado 50 vezes, sendo anotado o valor correspondente a cada
jogada. Faça um programa para:
(a) anotar os valores de todas as jogas em um vetor;
(b) determinar e imprimir os números de lançamentos nos quais o resultado obtido é
maior do que a média aritmética dos 50 lançamentos;
(c) determinar a percentagem de ocorrência da face seis do dado.

What I did:

#include <stdio.h>
#define MAX 50
int main() {
    int dado[MAX];
    int vetLancamentos[MAX];
    int p=0;
    float per = 0;
    float media_aritmetica = 0;
    for (int i = 0; i < MAX; ++i) {
        scanf("%d", &dado[i]);
        vetLancamentos[i] = dado[i];
        if (dado[i]==6) {
            p++;
        } else {
            printf("");
        }
    }

    for (int i = 0; i < MAX; ++i) {
        printf("%d ", vetLancamentos[i]);
        media_aritmetica+=(float)vetLancamentos[i]/MAX;
    }
    printf("\nLancamentos (maiores que a media):\n");
    for (int i = 0; i < MAX; ++i) {
        if (vetLancamentos[i]>media_aritmetica) {
            printf("%d ", vetLancamentos[i]);
        }
    }
    printf("\n6: %d ", p);
    per = (((float)p/MAX));
    printf("\nPercentagem: %f", per);

    return 0;
}

sorry if it sounds a useless question, is that I am not sure if it is calculated in the correct way, I thank the patience.

SO WAS THE CODE AFTER EDITING, THANK YOU FOR BOTH YOUR HELP.

#include <stdio.h>
#define MAX 6
int main() {

    int vetLancamentos[MAX];
    int p = 0; float soma = 0;
    float per = 0;
    for (int i = 0; i < MAX; ++i) {
        scanf("%d", &vetLancamentos[i]);
        soma += (float)vetLancamentos[i];
        if (vetLancamentos[i]==6) {
            p++;
        } else {
            printf("");
        }
    }

    printf("Media aritmetica: %f ", soma/(float)MAX);
    printf("\nLancamentos (maiores que a media):\n");
    for (int i = 0; i < MAX; ++i) {
        if (vetLancamentos[i]>(soma/(float)MAX)) {
            printf("%d ", vetLancamentos[i]);
        }
    }

    printf("\n6: %d ", p);
    per = (((float)p/MAX)*100);
    printf("\nPercentagem: %f", per);

    return 0;
}
  • 1

    It’s not just multiplying by 100? per = ((float) p / MAX) * 100 - anyway, you can simplify a little: the variable dado is a little redundant, you can only use the array of launches, you can add the values in the same loop you read (so you use the sum to calculate the average later), etc: https://ideone.com/XRvopH

  • thanks for the tip, I really realized that given[i] becomes redundant, now the code is cleaner, multiplied by 100 and got the acceptable percentage, thanks.

1 answer

0


The best way to know, is to create 3 or 4 sequences of data made by you in which you previously know the average and the percentage of 6. Run your code with that sequence and check if the result is the same...

To make it easier, you can temporarily decrease the number of launches to 10 or 5... which at the start works the same way.

My analysis of your code without testing indicates it’s fine. I noticed that the "given" array is useless (it can be a simple variable), but otherwise it should be fine.

Browser other questions tagged

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