How to transform a mathematical expression into a C language

Asked

Viewed 844 times

-2

How can I turn it into C language?

Expressão matemática:

  • 3

    You just have to understand how the function works, your reasoning and play it in code. I can’t help you anymore because I don’t understand the function

  • 2

    You can tell us what you tried and what problems you are having?

2 answers

7

The M function, which you place, is a real function of two real variables, defined by glue (the expression used to compute M(MP,ML) depends on the MP and ML values).

For a real function, of two real variables, a C function that models such a mathematical function can have as a signature float nome_da_funcao(float arg1, float arg2), where arg1 and arg2 are the function variables (in your case, MP and ML).

Depending on the desired accuracy (number of decimal places after the comma or, in C, after the point), it might be necessary to use double, but in this particular case, it seems that float is of good size.

As it is defined by collage, the expression to return its calculated value in (MP, ML) (or, if you like matematiqués, the image of (MP, ML)) depends on a conditional expression applied to the MP and ML arguments. Well, if the expression used to calculate the function value, at the point (MP, ML), depends on whether MP and ML satisfy a certain condition, it is clear that you will need to use in your code a conditional, at least, or more than one, when applicable.

In the case of their function:

if (PM < 5 or ML < 5)

M = Min{MP, ML}<br>

but

M = (7*MP + 3*ML)/10

If you have a function Min(float x, float y) available, your code can be

float M(float MP, float ML)
{
    if((MP < 5.0)||(ML < 5.0))
        return Min(MP, ML)
    else
        return ((7*MP + 3*ML)/10);
}

However, if you don’t have Min(float x, float y), can use

float M(float MP, float ML)
{
    if((MP < 5.0)||(ML < 5.0))
    {
        if (MP <= ML)
            return MP;
        else
            return ML;
    }
    else
        return ((7*MP + 3*ML)/10);
}

or define

float Min(float x, float y)
{
    if (x <= y)
        return x;
    else
        return y;
}

and, together with this function, use the first code.

  • 2

    +1 The answer was very good! :)

  • 1

    Leonardo, I took the liberty of lightly editing your post to break the paragraphs a little, to make it easier to read. I only did it because you can reverse the editing if you think it doesn’t suit you. To reverse just click on [Edit] and the link "revert" above the version you want. To see the changes made, just this link. As for the content, already took my +1

-2

The whole program goes like this:

 #include <stdio.h>
 int main(){
    double mp, ml;
    printf("Digite o Mp: ");
    scanf("%d",&mp);
    printf("\nDigite o Ml: ");
    scanf("%d",&ml);
    while(mp<5 || ml<5){
        printf("\nMp ou Ml menor que 5,0. Digite o numero novamente");
        printf("Digite o Mp: ");
        scanf("%d",&mp);
        printf("\nDigite o Ml: ");
        scanf("%d",&ml);
    }
    int m;
    //função:
    m=(7*mp+3*ml)/10;
    printf("%d", m);
}
  • 2

    Not supposed to have that while. The function that is in the question allows values of MP and ML smaller than 5, in this case the value to be returned is the lowest of the two, i.e., min(MP, ML).

  • That I’m not very good at this part of mathematics, the only part I understood was this: (7mp+3ml)/10, the rest I assumed that mp and ml have to be less than 5, and looking at the code I think I did wrong still kkk

  • 4

    As you have already mentioned, that answer is wrong. If you don’t understand the math part, I suggest removing it (even to avoid getting silly negatives). :)

Browser other questions tagged

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