C ';对于';多维数组中的循环失败
问题是:检查两个矩阵,如果一个是另一个的子矩阵 我在这里面临的问题是代码中注释为“//problem”的C ';对于';多维数组中的循环失败,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,问题是:检查两个矩阵,如果一个是另一个的子矩阵 我在这里面临的问题是代码中注释为“//problem”的for循环。当程序第一次运行时,所提到的for循环不能正常工作 #include <stdio.h> #define N 10 int main () { char matrix1[N][N], matrix2[N][N]; int i, j, row1, col1, row2, col2, k, l, m, n, check = 0; //First Matri
for
循环。当程序第一次运行时,所提到的for
循环不能正常工作
#include <stdio.h>
#define N 10
int
main ()
{
char matrix1[N][N], matrix2[N][N];
int i, j, row1, col1, row2, col2, k, l, m, n, check = 0;
//First Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row1);
printf ("Enter the size of columns\n");
scanf ("%d", &col1);
printf ("Now enter the values please\n");
//Putting Values In First Matrix
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix1[i][j]);
}
}
//Second Matrix
printf ("Enter the data\n");
printf ("Enter the size of rows\n");
scanf ("%d", &row2);
printf ("Enter the size of columns\n");
scanf ("%d", &col2);
printf ("Now enter the values please\n");
//Putting Values In Second Matrix
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf ("Please enter the %dth row and %dth column\n", i + 1,
j + 1);
scanf ("%s", &matrix2[i][j]);
}
}
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
if (matrix1[i][j] == matrix2[0][0])
{
k = i;
l = j;
for (m = 0; m < row2; m++)
{
for (n = 0; n < col2; n++)
{ //problem
if (matrix1[k][l] == matrix2[m][n])
{
check++;
printf ("Checked\n");
}
l++;
}
l = j;
k++;
}
}
}
printf ("hello\n");
}
if (check == row2 * col2)
{
printf ("It exists\n");
}
else
{
printf ("It doesn't exist\n");
}
}
在开始查找子矩阵之前,需要将
check
重置为零。
还可以在找到它后立即断开(或使用标志指示是否找到它)
从您的输出来看,(假设您试图查找2x2矩阵),它发现它连续检查了打印次数,但它的值也是5次,第一次打印也是如此,这使得您的程序打印“它不存在”
比如:
int=0;
... //一些代码
//检查两个矩阵
对于(i=0;i
在开始查找子矩阵之前,需要将检查
重置为零。
还可以在找到它后立即断开(或使用标志指示是否找到它)
从您的输出来看,(假设您试图查找2x2矩阵),它发现它连续检查了打印次数,但它的值也是5次,第一次打印也是如此,这使得您的程序打印“它不存在”
比如:
int=0;
…//一些代码
//检查两个矩阵
对于(i=0;i
更准确地说,你的矩阵是如何定义的?是int[]]还是float[]]?这很重要,因为C中的float比较并不总是有效的。它是int…让我把整个代码放在一起,所以检查是5,但是row2和col2的值是什么?你应该使用前缀递增(++i
)如果可能的话。另外,将其全部封装在函数中肯定会有帮助。@tanzeeb它应该是4,它的2x2矩阵,正如你第一次看到它没有循环回。@Bartek你不认为这会是同样的问题。。请更精确地说,你的矩阵是如何定义的?它是int[][]还是float[]?这一点很重要,因为C中的浮点比较并不总是有效的。它的int…让我把整个代码放在一起,所以检查值是5,但是row2和col2的值是多少?您应该使用前缀递增(++i
)如果可能的话。另外,将其全部封装在函数中肯定会有帮助。@tanzeeb它应该是4,它的2x2矩阵,正如你第一次看到的那样,它不会循环回。@Bartek你不认为这会是同样的问题。@riz需要在每次测试之前重置,否则它也会因部分匹配而不断增加。@riz需要在eac之前重置h测试,否则,对于部分匹配,它也将继续递增。
Checked
hello
Checked
Checked
Checked
Checked
hello
hello
It doesn't exist
int is_found = 0;
... //some code
//Checking Both Matrices
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
check = 0; //reset check
if (matrix1[i][j] == matrix2[0][0])
{
... //your code to check matrix.
...
}//if end
if(check == row2*col2)
{
is_found = 1;
}
...
} //for j end
if(is_found)
break;
...
...
if(is_found)
printf("It exists\n");