简体   繁体   中英

Input values in 2D array in a for loop in C

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



}

}

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=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.

Oh good grief, I see it now. You're using i as the iteration variable in two nested loops.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM