I need to remove presentation error from following program. The output must be exactly as displayed and no leading or trailing spaces are allowed.
```c #include<stdio.h> int main() { int rows; int cols; int matrix[5][5]; scanf("%d %d", &rows, &cols); int i=0, j=0; int trows=rows, tcols=cols, ti=i, tj=j; for(i=0;i<rows;i++) for(j=0;j<cols;j++) scanf("%d", &matrix[i][j]); //printf("%d",matrix[0][0]); while(ti<=trows && tj<=tcols) { for(i=ti,j=tj; j<tcols; j++) printf(" %d",matrix[i][j]); for(i++,j--; i<trows; i++) printf(" %d",matrix[i][j]); for(i--,j--; j>=ti; j--) printf(" %d",matrix[i][j]); for(i--,j++; i>tj; i--) printf(" %d",matrix[i][j]); ti++; tj++; trows--; tcols--; } //printf("%d ",matrix[--ti][--tj]); return 0; } ```
nptel
iit lecture rite?
Yes. That presentation error thing is pretty annoying.
this is spiral matrix?
Yes, it is. The logic is right, but these presentation errors make life too tough.
shud i give u the code or help u thru it? ur call
I tried a quick fix like ```c #include<stdio.h> int main() { int rows; int cols; int matrix[5][5]; scanf("%d %d", &rows, &cols); int i=0, j=1; int trows=rows, tcols=cols, ti=i, tj=j++; for(i=0;i<rows;i++) for(j=0;j<cols;j++) scanf("%d", &matrix[i][j]); printf("%d",matrix[0][0]); while(ti<=trows && tj<=tcols) { for(i=ti/*,j=tj*/; j<tcols; j++) printf(" %d",matrix[i][j]); for(i++,j--; i<trows; i++) printf(" %d",matrix[i][j]); for(i--,j--; j>=ti; j--) printf(" %d",matrix[i][j]); for(i--,j++; i>tj; i--) printf(" %d",matrix[i][j]); ti++; tj++; trows--; tcols--; j=tj; } //printf("%d ",matrix[--ti][--tj]); return 0; } ``` but I don't know why putting j=tj at the end doesn't work.
I never ask for whole code, especially if it a lot different from what I've done. I'm just looking for small hints.
well u r not able to creating any space..
and are u sure it gives the write output? it's not jus presentation error
The program I first wrote is absolutely correct, just a presentation error. The second one doesn't work when I try putting j=tj at the end of loop.
thats cause u change j hence changing the for loop
It will change regardless at the beginning of loop, and there are no changes to j in between.
dont change anything jus play with the spacing u will eventually get it for eg while (m>=1 && n>=1) {for(j=p;j<=n;j++) { i=q; if(j!=1){ printf(" ");} printf("%d",a[i][j]); } here i get the correct answer for the first coloumn. try something simialr in other loops
and it does effect. it starts throwng grabage value
did u get i?
Thanks, I came up with ``` #include<stdio.h> int main() { int rows; int cols; int matrix[5][5]; scanf("%d %d", &rows, &cols); int i=0, j=0; int trows=rows, tcols=cols, ti=i, tj=j, elements = rows*cols; for(i=0;i<rows;i++) for(j=0;j<cols;j++) scanf("%d", &matrix[i][j]); while(ti<=trows && tj<=tcols) { for(i=ti,j=tj; j<tcols; j++) { printf("%d",matrix[i][j]); if(--elements !=0) printf(" "); } for(i++,j--; i<trows; i++) { printf("%d",matrix[i][j]); if(--elements !=0) printf(" "); } for(i--,j--; j>=ti; j--) { printf("%d",matrix[i][j]); if(--elements !=0) printf(" "); } for(i--,j++; i>tj; i--) { printf("%d",matrix[i][j]); if(--elements !=0) printf(" "); } ti++; tj++; trows--; tcols--; } return 0; } ``` but looks like I need to work more on it; the last time I checked it was working, now it isn't working on rectangular martices. Need to modify the conditions of while loop.
if is required jus for the frst 1.. but ut get the idea.. i am here if need help :)
Enough progress for today. For some reason, I don't feel like doing more than one assignment a day. :P
Join our real-time social learning platform and learn together with your friends!