【问题标题】:Linear equation of two vaariable两个变量的线性方程
【发布时间】:2021-12-21 06:24:36
【问题描述】:

我想找到两个变量的线性方程的解(我知道有无穷多个解,但我必须打印其中一些!)

所以我尝试使用 C 语言 我所做的只是简单地输入 X、Y 和 c 的系数。然后计算你在代码中看到的值然后计算结果是否满足方程(意味着它是= 0或不)然后检查并打印。之后我增加了 x,y 的值,以便可以用不同的数字计算它(在第一个我默认为 0)..

这里是代码,请查看代码并告诉我有什么问题,我在选择变量类型等方面做错了什么。 如果可能,请给我正确的代码(请求)。

C中的代码:-

#include <stdio.h>

int main()
{
    float a, b, c, Rx, Ry;
    int sol;

    float x, y;
    printf("The Linear equation in two variable\n");
    printf("Formate is   aX+bY+c=0\n");
    printf("Enter the coefficient of 'X'\n");
    scanf("%f", &a);
    printf("Enter the coefficient of 'Y'\n");
    scanf("%f", &b);
    printf("Enter the coefficient of constant 'c'\n");
    scanf("%f", &c);

    printf("Your equation looks like\n");
    printf("%0.0fX+%0.0fY+%0.0f=0\n", a, b, c);

    x = 0;
    y = 0;
    for (int i = 0; i < 10; i++)
    {
        //  for x

        x = ((-c) + (-b * y)) / b;
        printf("The value of x is %0.2f\n", x);

        // for y

        y = ((-c) + (-a * x)) / a;
        printf("The value of y is %0.2f\n", y);

        // checking wheter the value of x and y satisfy the equation (which is equal to 0)
        sol = (a * x) + (b * y) + (c);
        printf("The value of sol is %0.2f\n", sol);

        x++;
        y++;
        if (sol == 0)
        {
            printf("The value of x at %d time is %0.2f\n", i, x);
            printf("The value of y at %d time is %0.2f\n", i, y);
        }
        else
            continue;
    }

    return 0;
}

【问题讨论】:

    标签: variables linear-equation


    【解决方案1】:

    我在选择变量类型时做错了吗

       int sol;
       …
           printf("The value of sol is %0.2f\n", sol);
    

    sol 选择类型int 是否错误取决于您想要的精度。在任何情况下,转换说明符 f 和类型 int 都不适合;你必须让他们排队。请注意,如果您为sol 选择float,则比较sol == 0 不太可能成功。

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      相关资源
      最近更新 更多