I'm trying to get the for
loop to continue in order for the user to input the locations of the 1's in a sparse matrix. But I have not been able to see why the for
loop won't continue after one loop. This is only one part of my code, the rest is not necessary.
我试着让for循环继续为了让用户在稀疏矩阵中输入1的位置。但是我还不能理解为什么for循环在一个循环之后不会继续。这只是我代码的一部分,其余的没有必要。
int ** getBinarySparseMatrixFromUser()
{
int r, c, i, f, g;
int **p;
printf("Please enter the number of rows:\n");
scanf("%d", &r);
printf("Please enter the number of columns:\n");
scanf("%d", &c);
p= malloc(r* sizeof(int*));
for (i=0; i<r; i++)
{
p[i]= malloc(c* sizeof(int));
printf("in Main : *p[%d]= %d\n", i, p[i]);
}
for (i=1; i<r; i++)
{
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0)
{
printf("Please enter column location of the 1's in row: %d\n", i);
for (i=0; i<f; i++)
{
scanf("%d", &g);
p[i][g]= 1;
}
}
}
}
Revised code posted by request (still buggy):
按要求张贴的修订代码(仍然有问题):
int ** getBinarySparseMatrixFromUser()
{
int r, c, i, j, f, g;
int **p;
printf("Please enter the number of rows:\n");
scanf("%d", &r);
printf("Please enter the number of columns:\n");
scanf("%d", &c);
p= malloc(r* sizeof(int*));
for (i=0; i<r; i++)
{
p[i]= malloc(c* sizeof(int));
printf("in Main : *p[%d]= %d\n", i, p[i]);
}
for (i=0; i<r; i++)
{
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0)
{
printf("Please enter column location of the 1's in row: %d\n", i);
for (j=0; j<f; j++)
{
scanf("%d", &g);
p[i][g]= 1;
}
}
}
}
}
2 个解决方案
#1
1
Oh good grief, I see it now. You're using i
as the iteration variable in two nested loops.
哦,天哪,我现在看到了。您在两个嵌套循环中使用i作为迭代变量。
for (i = 1; i < r; i++) { // <---- Using i in outer loop
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0) {
printf("Please enter column location of the 1's in row: %d\n", i);
for (i = 0; i<f; i++) { // <--- Also using i in inner loop
scanf("%d", &g);
p[i][g] = 1;
}
}
}
#2
3
I wonder if the problem is in reusing the global variable "i" in both your inner and outer for loops in this part of your code:
我想知道的是,在代码的这一部分,在内部和外部for循环中重用全局变量I是否存在问题:
for (i=1; i<r; i++)
{
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0)
{
printf("Please enter column location of the 1's in row: %d\n", i);
for (i=0; i<f; i++)
{
scanf("%d", &g);
p[i][g]= 1;
}
}
Try using a different variable for this inside for loop.
在for循环中尝试使用不同的变量。
#1
1
Oh good grief, I see it now. You're using i
as the iteration variable in two nested loops.
哦,天哪,我现在看到了。您在两个嵌套循环中使用i作为迭代变量。
for (i = 1; i < r; i++) { // <---- Using i in outer loop
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0) {
printf("Please enter column location of the 1's in row: %d\n", i);
for (i = 0; i<f; i++) { // <--- Also using i in inner loop
scanf("%d", &g);
p[i][g] = 1;
}
}
}
#2
3
I wonder if the problem is in reusing the global variable "i" in both your inner and outer for loops in this part of your code:
我想知道的是,在代码的这一部分,在内部和外部for循环中重用全局变量I是否存在问题:
for (i=1; i<r; i++)
{
printf("Please enter the number of 1's in row %d :\n", i);
scanf("%d", &f);
if (f>0)
{
printf("Please enter column location of the 1's in row: %d\n", i);
for (i=0; i<f; i++)
{
scanf("%d", &g);
p[i][g]= 1;
}
}
Try using a different variable for this inside for loop.
在for循环中尝试使用不同的变量。