The result comes out another

Asked

Viewed 58 times

-1

I have tried to solve this exercise that I picked up on the internet. But it never comes out the value I want. What’s the reason?

/ João Papo-de-Pescador, a good man, bought a microcomputer to control the daily income of your work. Every time it carries a weight of fish greater than that established by the regulation fishing in the state of São Paulo (50 kilos) must pay a fine of R$ 4,00 per kilo surplus. John needs you to make a program read the weight variable (weight of fish) and calculate the excess. Write the amount of kilos over the limit and variable to the excess variable fine the amount of the fine that John must pay. Print the program data with the appropriate messages. /

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

int main(){   

   //variaveis
        float peso = 0, excesso = 0; 

        // Recebe o peso
        printf("Digite quantos quilos você coletou:\n");
        scanf("%d", peso);
        printf("=================================================\n");
        // Recebe o excesso
        printf("Digite quantos quilos de excesso foram coletados:\n");
        scanf("%d", excesso);
        printf("=================================================\n");

        //Calcular
        float calculo = peso + excesso;

        if(calculo >= 50){
            float multa = calculo * 4;
            printf("Você coletou: %d Kg.\n", calculo);
            printf("A multa a ser paga, será de: R$%d\n", multa);
        }else{
            printf("Você coletou: %d Kg.\n", calculo);
            printf("Não será necessário pagar multa!\n");
        }




    return 0;
}    
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

2

There are numerous problems with that code. Some are more cosmetic or not so important, but this is not how you program in C. If you learn wrong will always do wrong.

The comments are obvious so they do not help in understanding the code, only pollute, so I took.

The initialization of variables was not necessary because it never uses these values before changing them, there is a waste of time initiating without use. It is not important for an exercise, but you get used to doing wrong.

The formatting to use type float in the input and output functions shall be f and not d how you used to.

To pass a variable to the scanf() should be through a reference always, as the variables there are not references, should explicitly pass as one through the operator &.

The if had something that runs equal in both cases, so it can stay out of the block and gets more canonical and simple.

I changed a variable name to be more semantic and readable, this is more important than comments.

In addition there is a logic problem. You should check if the weight is greater than 450 (not greater or equal), and then calculate the fine in this case. The fine should be calculated only on the excess (should not ask which is the excess) which is the weight minus 50 kilos.

Strictly the statement asks to "record" the values in variable. You have chosen to show, it makes no sense to do both without need. I think showing is much more important than putting in variables, I find the statement bad.

#include <stdio.h>

int main(){   
    float peso; 
    printf("Digite quantos quilos você coletou:\n");
    scanf("%f", &peso);
    printf("=================================================\n");
    printf("Você coletou: %f Kg.\n", peso);
    if (peso > 50) printf("A multa a ser paga, será de: R$%f\n", (peso - 50) * 4.0);
    else printf("Não será necessário pagar multa!\n");
}

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

If you want to put in variables:

#include <stdio.h>

int main(){   
    float peso; 
    printf("Digite quantos quilos você coletou:\n");
    scanf("%f", &peso);
    printf("=================================================\n");
    printf("Você coletou: %f Kg.\n", peso);
    if (peso > 50) {
        float excesso = peso - 50;
        float multa = excesso * 4.0;
        printf("A multa a ser paga, será de: R$%f\n", multa);
    } else printf("Não será necessário pagar multa!\n");
}

0

First, you are not printing the correct values as you are not passing the value correctly with the scanf() function, read this question:Why don’t you need the & in the scanf();?

Later, in fact, you misinterpreted the question. Because your code is 'catching' the whole value of the fish and multiplying by 4( value that will be paid if it exceeds 50 kg). To conform to the enunciation of the exercise he must take only the excess value and multiply by 4.

I made some modifications to your code:

1) Since the excess will only be used after 50 kg, assign 50 directly to the variable( You can easily change this in the future)

2) I have arranged the accuracy of your format specifiers %f for two decimal places %.2f

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

int main(){   

   //variaveis
        float peso = 0, limite = 50; 

        // Recebe o peso
        printf("Digite quantos quilos você coletou:\n");
        scanf("%f", &peso);
        printf("=================================================\n");
        // Recebe o excesso


        //Calcular
        float calculo = peso - limite;

        if(peso > 50){
            float multa = calculo * 4;
            printf("Você coletou: %.2f Kg.\n", calculo);
            printf("A multa a ser paga, será de: R$%.2f\n", multa);
        }else{
            printf("Você coletou excedeu os quilos\n");
            printf("Não será necessário pagar multa!\n");
        }

0

This variable calculation is not necessary, because the condition to calculate the excess is when the weight exceeds 50 kg.

    float peso, multa; 

    printf("Digite quantos quilos voce coletou: \n");
    scanf("%f",&peso);

    multa = 0;

    if(peso >= 50){
        multa = peso * 4;

        printf("Voce coletou: %.2f Kg \n", peso);
        printf("A multa a ser paga, sera de: R$%.2f\n", multa);
    }else{
        printf("Você coletou: %.2f Kg.\n", peso);
        printf("Não será necessário pagar multa!\n");
    }

Browser other questions tagged

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