【问题标题】:Calculating A Determinant From A 2x3 Array Matrix从 2x3 数组矩阵计算行列式
【发布时间】:2013-12-31 10:33:39
【问题描述】:

所以我编写了下面的代码作为求解二维线性方程组的程序。

#include <stdio.h>

int main( )
{
  int eq[2][3];

  int D, Dx, Dy;

  int sol[2];

  printf("Enter cofficients of first equation: ");
  scanf("%d %d %d", &eq[0][0], &eq[0][1], &eq[0][2]); 

  printf("Enter cofficients of second equation: ");
  scanf("%d %d %d", &eq[1][0], &eq[1][1], &eq[1][2]);

  D = eq[0][0]*eq[1][1] - eq[1][0]*eq[0][1];
  Dx = eq[0][2]*eq[1][1] - eq[1][2]*eq[0][1];
  Dy = eq[0][0]*eq[1][2] - eq[1][0]*eq[0][2];

  if(D != 0){
    sol[0] = Dx/D;   // x solution
    sol[1] = Dy/D;   // y solution

    printf("x = %d, y = %d \n", sol[0], sol[1]);
  }
  else{
    printf("No unique solutions exist. \n");
  }

  return 0;
}

我现在的任务是使用原型将其转换为函数:

 bool determinantFunction(int e[][3], int s[]);

我的问题是我不知道从哪里开始。我已经尽可能多地阅读了在 C 中使用布尔值的内容,但我不明白如何或为什么将其实现为行列式函数。

【问题讨论】:

  • 我相信我们将使用布尔返回值来确定是否实际计算了解决方案。所以是的,对于非零行列式,它应该返回 true,对于零行列式,它应该返回 false。
  • “行列式”是根据 2x2(不是 2x3)矩阵计算得出的。解是根据 2x3 矩阵计算得出的。

标签: c arrays matrix boolean


【解决方案1】:

因此,只需将您现有的代码放入这样的函数中(我并不是说您的代码是对还是错),您会得到如下内容:

bool determinantFunction(int e[][3], int s[])
{
    int D, Dx, Dy;

    // calculate determinant
    D  = e[0][0]*e[1][1] - e[1][0]*e[0][1];
    Dx = e[0][2]*e[1][1] - e[1][2]*e[0][1];
    Dy = e[0][0]*e[1][2] - e[1][0]*e[0][2];

    // if non-singular ...
    if (D != 0)
    {
        // success
        s[0] = Dx/D;   // return x solution
        s[1] = Dy/D;   // return y solution
        return true;
    }

    // no solution
    return false;
}

然后你的main 变成这样(未测试):

int main( )
{
    int eq[2][3];
    int sol[2];

    printf("Enter cofficients of first equation: ");
    scanf("%d %d %d", &eq[0][0], &eq[0][1], &eq[0][2]); 

    printf("Enter cofficients of second equation: ");
    scanf("%d %d %d", &eq[1][0], &eq[1][1], &eq[1][2]);

    if (determinantFunction(eq, sol))
    {
        printf("x = %d, y = %d \n", sol[0], sol[1]);
    }
    else{
        printf("No unique solutions exist. \n");
    }

    return 0;
}

对于您的示例:4x - 3y = -14 和 3x - 5y = -5,这与:

4x - 3y + 14 = 0
3x - 5y + 5 = 0

你会得到:

好的,最后一次更新 - 硬编码系数:

int eq[2][3] = {{4, -3, 14}, {3, -5, 5}};
int sol[2];
if (determinantFunction(eq, sol))
{
    printf("x = %d, y = %d \n", sol[0], sol[1]);
}
else{
    printf("No unique solutions exist. \n");
}

【讨论】:

  • 比如说,我想求解这个方程组: (4x - 3y = -14) (3x - 5y = -5) 我如何将其转换为函数调用?跨度>
  • 如果我想硬编码方程组,我需要在 main 中调用函数,对吗?我该怎么做呢?
  • 非常有帮助!太感谢了。我想我想太多了,如何将 bool 实现到我以前的代码中,现在这当然更有意义了。谢谢@Roger!
  • @Roger Rowland s[0] = Dx/D; 是否可能是由于截断而导致损失的除法?
  • @chux 是的,这都是整数运算,这就是为什么我说“我不是说你的代码是对还是错” - 它适用于示例方程,但对其他人来说不准确.这是由 OP 修复的 - 很容易。
【解决方案2】:

这样的函数可能起作用的一种方法是如果有唯一的解决方案则返回true,否则返回false。如果确实找到了解决方案,它将存储在作为第二个参数传递的数组中。

基本上,您只需将现有代码移动到函数中即可。您的 sol 数组将作为第一个参数传递给您。

int main()
{
    int eq[2][3];
    // ...
    int sol[2];
    if (determinantFunction(eq, sol)) {
        printf("%d %d\n", sol[0], sol[1]);
    } else {
        printf("No unique solutions exist.\n");
    }
}

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多