Member Avatar for Rahul47

Hello folks, This nitty problem is haunting me today. Here is the code I have written, but am not getting the expected output.
This is a program for printing matrix in spiral order.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int rows,cols,i,j;
    int tr=0,rc,br,lc=0;
    /*
        tr = top row index.
        rc = right column index.
        br = bottom row index.
        lc = left column index.
    */
    scanf("%d%d", &rows,&cols);
    br=rows-1; rc=cols-1;
    int *ptr=(int*)calloc((rows*cols),sizeof(int));
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
                scanf("%d", ((ptr+i*rows)+j));
            }
    }
    while(br!=0 && rc!=0){
        for(i=tr; i<rc; i++){
            printf("%d ", *((ptr+tr*cols)+i));   // Top row.    
        }
        for(i=tr; i<br; i++){
            printf("%d ", *((ptr+i*cols)+rc));   // Right column.   
        }
        for(i=rc; i>lc; i--){
            printf("%d " , *((ptr+br*cols)+i));  // Bottom row. 
        }
        for(i=rc; i>tr; i--){
            printf("%d ", *((ptr+i*cols)+lc));   // Left column.
        }
        tr++;   // Going Down.  
        rc--;   // Shifting Left.
        br--;   // Going up.
        lc++;   // Shifting Right.
    }
    return 0;
}

And, here is the ouput.
811b51d1b0bbb19aee282681f9b5ec95

Regards.

what is the expected output?

Member Avatar for Rahul47

It should show the matrix in SPIRAL order.

1--> 2 --> 3 --> 4
                 v
5--> 6 --> 7     8
^                v
9<-- 10 <--11<--12

Like shown by arrow. Opening a matrix clockwise. It will open up as following.

1 2 3 4 8 12 11 10 9 5 6

Once you get the 12 numbers why not sort them in ascending sequence then print them out like normal? Or is that considered cheating?

Member Avatar for Rahul47

Ummmm . . . There is no ascending pattern here.

Of course there is -- look at the expected output, it's in ascending order from top to bottom. But maybe that is just a coincidence???

Member Avatar for Rahul47

I don't see how it is ascending order when after 1 2 3 4 it is 8 10 12 11 ?

That isn't, what i said was to sort it in ascending order because the final output is in ascending order.

Member Avatar for Rahul47

I think you are mistaken Sir, the second line in that snapshot shows the elements entered in the array, while third line is supposed to show the opened up matrix in spiral order which is supposed to be 1 2 3 4 8 12 11 10 9 5 6.

Yes, but the numbers are all in ascending order if you read the lines from top to bottom, ignoring the arrows. It should be rather trivel to add the arrows.

1 2 3 4 
5 6 7 8
9 10 11 12

The flaw in my approach is if the data is in some random order in the output

1 4 2 3
9 10 7 8
11 5 6 12

If that is possible, then you can't use my suggestion.

Member Avatar for Rahul47

:D Sir, Arrows were directed for sequence of elements. Clockwise direction of Spiral. And yes data has to be random. I used consecutive numbers for simplicity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.