2

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



}

}
Leo.p
  • 43
  • 7
  • `p= malloc(r* sizeof(int*));` vs. `p[i]= malloc(c* sizeof(int));` Any reason for one being `int*` while the other being a regular `int`? In this case it doesn't do any difference. – Emz Nov 21 '15 at 20:48
  • How are you entering your data when you run it? It wants whitespace between entries. – Tom Zych Nov 21 '15 at 20:48
  • one has a * because it is for the number of rows and the other does not because it is for the number of columns i believe @Emz – Leo.p Nov 21 '15 at 20:53
  • @TomZych I'm sorry I am very new at this and I do not understand what you mean. – Leo.p Nov 21 '15 at 20:54
  • Are you entering the numbers one at a time? Or separated by spaces? Anything else, the program may have trouble because you're using `scanf`. – Tom Zych Nov 21 '15 at 20:55
  • Try print r and c after read them. – terence hill Nov 21 '15 at 20:56
  • @Emz, `p` is an array of `int *` and each `p[i]` is an array of `int`. I think that part of the code is correct. – Tom Zych Nov 21 '15 at 20:57
  • Also here printf("in Main : *p[%d]= %d\n", i, p[i]); p[i] is a pointer. – terence hill Nov 21 '15 at 20:58
  • @TomZych I am entering them one at a time, separated by enters and not by spaces. Is there something else which would work better then `scanf` – Leo.p Nov 21 '15 at 20:59
  • I try to avoid using `scanf` for user input, it's too fragile. For production code (as opposed to an example), I'd use `fgets` and parse the resulting string. Don't worry about it yet, you're still learning. – Tom Zych Nov 21 '15 at 21:03

2 Answers2

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

clay
  • 1,757
  • 2
  • 20
  • 23
1

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;
        }
    }
}
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • Thank you i will try exactly that! even if someone beat you to it :P – Leo.p Nov 21 '15 at 21:02
  • Well...he still accepted your answer instead of mine. – clay Nov 21 '15 at 21:13
  • @clay: Yeah, I wonder why? Well, you got more upvotes. Pretty even, I guess. – Tom Zych Nov 21 '15 at 21:14
  • @Tom -- yeah. The pay is the same either way. :) – clay Nov 21 '15 at 21:16
  • 1
    i would accep both if it was possible @clay. – Leo.p Nov 21 '15 at 21:17
  • if there is an etiquette i should know? @clay – Leo.p Nov 21 '15 at 21:19
  • @Leo.p - it's really up to you. People often pick the first correct answer if all things are equal; however, it often happens that multiple answers will pop and someone will find a subsequent answer more helpful, even if an earlier answer is technically correct. In this case, Tom and I were wondering what you found more helpful in his answer. However, the truth is that it's up to you and the fun--at least for me--is in finding a solution. If you got something out of SO today then I'm honestly satisfied. – clay Nov 21 '15 at 21:21
  • Bottom line: SO is cool because of the community--at least to me--not the votes. – clay Nov 21 '15 at 21:23
  • @clay awesome thank you but maybe you can still help once more. now that i changed the `i` to `j` my loop prints for the number of rows i have but minus one so : if i want 3 rows it will only loop for 2 rows. – Leo.p Nov 21 '15 at 21:25
  • Does it do the first 2? If so, try changing < to <= and see if that fixes it. Also, you might want to consider picking more descriptive variable names. – clay Nov 21 '15 at 21:30
  • `for (i = 1; i < r; i++)` - you should start at 0, not 1. Overlooked that. – Tom Zych Nov 21 '15 at 21:33
  • @clay yes it prints for 1 and 2 (if i input 3 rows) but crashes after that – Leo.p Nov 21 '15 at 21:41
  • @clay if row is 3 and column is 4 it will crash after the input of `Please enter column location of the 1's in row 3:` – Leo.p Nov 21 '15 at 21:45
  • @Leo.p: `for (i = 1; i < r; i++)` should be `for (i = 0; i < r; i++)`. – Tom Zych Nov 21 '15 at 21:46
  • @TomZych thank you i changed that but it still crashes at the same place – Leo.p Nov 21 '15 at 21:50
  • @Leo.p: Please edit the question. Add `---` at the end, then paste in your current code. – Tom Zych Nov 21 '15 at 22:12
  • @Leo.p: no, I've rolled it back. Go to the end, put in a blank line then `---` and another blank line, then paste in the code as it is now. Leave the original code on top as it was, for future readers. – Tom Zych Nov 21 '15 at 22:20
  • @Leo.p: you're still using `i` for the inner loop? You need to change the line `for (i=0; i – Tom Zych Nov 21 '15 at 22:27