if 语句嵌套在 for 语句中将不起作用
我在互联网上进行了搜索,但在任何地方都找不到我的问题的答案。我正在尝试编写一个程序来查找 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
第四个嵌套循环
永远不会执行。看条件吧...
The fourth nested loop
never executes. Look at the condition...
呃,你不是说
if (dothis)
吗?Er, don't you mean
if (dothis)
?“真”是不正确的。
for(int j = 0; j < 0; j++) 块不会被执行,因为 j 为零。
另外,为什么要在 IF 块中递增?
下次到达该块时,这些值将重置为 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?
These values are reset to 0 the next time that block is reached.
最里面的循环中缺少大括号,因此只有
dothis
赋值在循环中运行。但是,该分配是不必要的,因为它的值仅使用一次(并且不正确 - 它应该与true
进行比较,而不是"true"
)。这是一种更好的写法:
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 totrue
, not"true"
).Here's a better way to write it: