Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (rbx):

I need to remove presentation error from following program. The output must be exactly as displayed and no leading or trailing spaces are allowed.

OpenStudy (rbx):

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

OpenStudy (anonymous):

nptel

OpenStudy (anonymous):

iit lecture rite?

OpenStudy (rbx):

Yes. That presentation error thing is pretty annoying.

OpenStudy (anonymous):

this is spiral matrix?

OpenStudy (rbx):

Yes, it is. The logic is right, but these presentation errors make life too tough.

OpenStudy (anonymous):

shud i give u the code or help u thru it? ur call

OpenStudy (rbx):

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.

OpenStudy (rbx):

I never ask for whole code, especially if it a lot different from what I've done. I'm just looking for small hints.

OpenStudy (anonymous):

well u r not able to creating any space..

OpenStudy (anonymous):

and are u sure it gives the write output? it's not jus presentation error

OpenStudy (rbx):

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.

OpenStudy (anonymous):

thats cause u change j hence changing the for loop

OpenStudy (rbx):

It will change regardless at the beginning of loop, and there are no changes to j in between.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

and it does effect. it starts throwng grabage value

OpenStudy (anonymous):

did u get i?

OpenStudy (rbx):

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.

OpenStudy (anonymous):

if is required jus for the frst 1.. but ut get the idea.. i am here if need help :)

OpenStudy (rbx):

Enough progress for today. For some reason, I don't feel like doing more than one assignment a day. :P

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!