Thank you very much keskiverto.
However, I am still confused as to how
1 2 3 4 5 6 7 8
|
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter value for element %d,%d: ", i, j);
scanf(" %c", array + i*cols + j);
}
}
|
works.
My logic must be wrong, as the program does in fact work this way, but this is what I think should be happening.
Say I give
rows
and
cols
a value of 3.
On the first iteration, i * cols is 0 and j is 0, so you are entering data into element 0,0.
On the second iteration, i * cols is 0 and j is 1, so you are entering data into element 0,1.
On the third iteration, i * cols is 0 and j is 2, so you are entering data into element 0,2.
(From here is where I think it should be going wrong).
On the fourth iteration, i * cols is 3 and j is 0, so you are entering data into element 3,0.
On the fifth iteration, i * cols is 3 and j is 1, so you are entering data into element 3,1.
On the sixth iteration, i * cols is 3 and j is 2, so you are entering data into element 3,2.
On the seventh iteration, i * cols is 6 and j is 0, so you are entering data into element 6,0.
On the eighth iteration, i * cols is 6 and j is 1, so you are entering data into element 6,1.
Finally, on the ninth iteration, i * cols is 6 and j is 2, so you are entering data into element 6,2.
Obviously this is not what is happening, but I feel this is what should be happening. I also feel that the line
|
scanf(" %c", array + i + j);
|
should work, but it doesn't.
I'm really confused about this. Would you be willing to explain it to me?