if 语句嵌套在 for 语句中将不起作用

发布于 2024-10-16 07:44:08 字数 1762 浏览 3 评论 0原文

我在互联网上进行了搜索,但在任何地方都找不到我的问题的答案。我正在尝试编写一个程序来查找 3x3 矩阵的逆矩阵,并且我正在尝试设置次要矩阵。

代码(不是所有代码,只是不起作用的部分):

for (int d = 0; d < rows; d++)  /** d is the row of the matrix of minors **/
{
    for (int m = 0; m < cols; m++) /** m is the column of matrix of minors **/
    {
        for (int i = 0; i < rows; i++) /** i is the row of the main martix **/
        {
            for (int j = 0; j < cols; j++) /** j is the column of the main matrix**/
            (d == i || m == j)? dothis = false : dothis = true;
                    /** This is to determin if the number at postion (i,j) is part
                      of the determent for the matrix of minors.  For postion (d,m)
                      of the matrix of minors, row d and colum m of the main matrix
                      are crossed out.**/

                if (dothis == 'true')
                {

                    /** postions for the determent matrix **/

                    detmin.set(mrtx.access(i,j), I, J);
                    /** this will set the determent matrix with the numbers
                      remaining from the cross-out prosecess. **/

                    I++;
                    J++;
                }

            }
        }

        detmin.display(3, 3); /** This makes sure the program did the 
                                if statement **/
        minor.set(detmin.twodert(), d, m);
        /** takes the determent of the determent matrix and puts it
          into the matrix of minors at posstion (d,m) **/

        cout << endl << endl;
    }
}

程序符合要求,但是当我运行它时,程序显示一个 3x3 的零矩阵。我认为最里面的 if 语句没有被程序识别,我不知道为什么。有谁知道我该如何解决这个问题?

编辑 现在的代码看起来就像上面一样,并且所有循环都在运行。它仍然没有达到我想要的效果,但我已经取得了进步。谢谢。

I've searched all over the internet, but I can't find an answer to my problem anywhere. I'm trying to write a program that will find the inverse of a 3x3 matrix and I'm trying to set up the matrix of minors.

Code(it's not all the code only the part that doesn't work):

for (int d = 0; d < rows; d++)  /** d is the row of the matrix of minors **/
{
    for (int m = 0; m < cols; m++) /** m is the column of matrix of minors **/
    {
        for (int i = 0; i < rows; i++) /** i is the row of the main martix **/
        {
            for (int j = 0; j < cols; j++) /** j is the column of the main matrix**/
            (d == i || m == j)? dothis = false : dothis = true;
                    /** This is to determin if the number at postion (i,j) is part
                      of the determent for the matrix of minors.  For postion (d,m)
                      of the matrix of minors, row d and colum m of the main matrix
                      are crossed out.**/

                if (dothis == 'true')
                {

                    /** postions for the determent matrix **/

                    detmin.set(mrtx.access(i,j), I, J);
                    /** this will set the determent matrix with the numbers
                      remaining from the cross-out prosecess. **/

                    I++;
                    J++;
                }

            }
        }

        detmin.display(3, 3); /** This makes sure the program did the 
                                if statement **/
        minor.set(detmin.twodert(), d, m);
        /** takes the determent of the determent matrix and puts it
          into the matrix of minors at posstion (d,m) **/

        cout << endl << endl;
    }
}

The program complies fine, but when I go to run it, the program displays a 3x3 matrix of zeros. I think that the inner most if statements are not being recognized by the program and I have no idea why. Does anyone know how I can fix this?

Edit The code now looks like it does above and all loops are running. It still doesn't do what I want it to, but I have made progress. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

给不了的爱 2024-10-23 07:44:08

第四个嵌套循环

for (int j = 0; j < 0; j++)

永远不会执行。看条件吧...

The fourth nested loop

for (int j = 0; j < 0; j++)

never executes. Look at the condition...

我是男神闪亮亮 2024-10-23 07:44:08

呃,你不是说if (dothis)吗?

Er, don't you mean if (dothis)?

别挽留 2024-10-23 07:44:08

“真”是不正确的。

for(int j = 0; j < 0; j++) 块不会被执行,因为 j 为零。
另外,为什么要在 IF 块中递增?

I++;
J++;

下次到达该块时,这些值将重置为 0。

'true' is incorrect.

The for(int j = 0; j < 0; j++) block won't be executed since j is zero.
Also, why do you increment in the IF block?

I++;
J++;

These values are reset to 0 the next time that block is reached.

霓裳挽歌倾城醉 2024-10-23 07:44:08
  1. dothis == '真'。在 C++ 下编译,但始终为 FALSE(“true”是整数常量 0x74727565,布尔值等于 0(假)或 1(真))。
  2. 您从未设置 dothis = true。
  3. 正如已经提到的,最里面的循环永远不会被执行。
  1. dothis == 'true'. Compiles under C++, but is always FALSE ('true' is integer constant 0x74727565 and boolean equals 0 (false) or 1 (true)).
  2. You never set dothis = true.
  3. As already mentioned, the innermost loop is never executed.
内心荒芜 2024-10-23 07:44:08

最里面的循环中缺少大括号,因此只有 dothis 赋值在循环中运行。但是,该分配是不必要的,因为它的值仅使用一次(并且不正确 - 它应该与 true 进行比较,而不是 "true")。

这是一种更好的写法:

            for (int j = 0; j < cols; j++) /** j is the column of the main matrix**/
            {
                    /** This is to determin if the number at postion (i,j) is part
                      of the determent for the matrix of minors.  For postion (d,m)
                      of the matrix of minors, row d and colum m of the main matrix
                      are crossed out.**/

                if (d == i || m == j)
                {

                    /** postions for the determent matrix **/

                    detmin.set(mrtx.access(i,j), I, J);
                    /** this will set the determent matrix with the numbers
                      remaining from the cross-out prosecess. **/

                    I++;
                    J++;
                }
            }
        }

You are missing braces in your innermost loop, so only the dothis assignment runs in the loop. However, that assignment is unnecessary because its value is only used once (and incorrectly -- it should be compared to true, not "true").

Here's a better way to write it:

            for (int j = 0; j < cols; j++) /** j is the column of the main matrix**/
            {
                    /** This is to determin if the number at postion (i,j) is part
                      of the determent for the matrix of minors.  For postion (d,m)
                      of the matrix of minors, row d and colum m of the main matrix
                      are crossed out.**/

                if (d == i || m == j)
                {

                    /** postions for the determent matrix **/

                    detmin.set(mrtx.access(i,j), I, J);
                    /** this will set the determent matrix with the numbers
                      remaining from the cross-out prosecess. **/

                    I++;
                    J++;
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文