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.
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;
}
}
}
}
}