0

Can somebody tell me why does this code not working properly? Please?

https://code.sololearn.com/cQVPI95z2e4N/?ref=app

18th Aug 2019, 7:39 AM
Ensifer Rus
Ensifer Rus - avatar
2 Réponses
+ 1
There is an error in reasoning. Try scanf("%d", (m1.elem+3*i+j)); You have to multiply the first loop-variable with the number of rows.
18th Aug 2019, 8:18 AM
Michael
Michael - avatar
+ 1
It is because of *((m1.elem+i)+j) statements in both printf and scanf.. Suppose m1.elem points to address 2001 then "m1.elem+i+j" (i=0,j=0) will point to 2001 then j will increment and the location will be 2002 then 2003 and 2004.. Now when the control comes to outer loop..i will be 1 and j will be 0..So statement "m1.elem+i+j" will point to 2002 because m1.elem is pointing to first element and you are adding one so it will point to next location that is 2002..So bcoz of this your input is overwritten again and again.. Solution :- Make only one loop.. for(i=0;i<m1.rows*m1.cols;i++) { scanf("%d", m1.elem+i); } Same for printf().. Ps:- This is the way I thought about the bug and I m not sure if this is actually correct or not..But solution I gave is working perfectly..
18th Aug 2019, 8:25 AM
Alaska
Alaska - avatar