Equation of 1° degree in C

Asked

Viewed 4,344 times

1

How can I implement a algorithm calculating an equation of 1° grade in C?

I know that a first-degree equation to be solved must follow three steps:

1°: Group the numbers on one side of the = all terms that have the unknown (x) and gather on the other side all terms that do not have (x). To make this transposition, the signals ahead of each number must be changed. So, what is adding up on one side passes to the other subtracting and vice versa, and what is multiplying on one side passes to the other to divide.

Example: 4x + 1= 2x + 7

Transposing: 4x - 2x = 7 - 1

2°: Resolve operations separately on each side of the signal from equal. That is, to solve the first-degree equation must solve operations until a number on each side of the same.

Upshot: 2x = 6

3°: Finally, to solve the first-degree equation the number that this multiplying by x now divides to the other side of the sign of equal, in our case:

x = 6 / 2

Final result: x = 3

Implementation followed by @Rafael Carneiro de Moraes' reply:

#include <stdio.h>

int main(void)
{
    float x, a, b, c;

    b = 1 + 7;

    a = 4 + 2;

    c = a + b;

    x = (c - b) / a;

    printf("\nx = %f",x);

    return 0;
}

I don’t know if the result is correct.

  • What you’ve already done?

  • I am trying to implement the above example, but I am not able to define a logic of how to represent these steps in programming.

  • 1

    You don’t have to declare c as a + b because the result will go wrong. I will use your example (4x + 1= 2x + 7): joining the equal sides would be 2x = 6 and the value of its x should be 3. That is, in that case you should do: b = 1; &#xA;a = 4 - 2;&#xA;c = 7;&#xA;x = (c - b) / a;

  • I implemented here, the result was 3.

1 answer

2


Taking into account an equation of the type Ax + b = c, being:

  • Ax: the sum of all x of the equation
  • b: the sum of all numbers in the equation
  • c: the result

And assuming that your program will receive the values of a, b and c, the equation you need for is x = (c - b) / a.

If you have more than one x you need to do tests if/else to calculate a, because if it is 0 there is no equation but a possible equality of b and c.

Browser other questions tagged

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