2
\$\begingroup\$

I found an interview question which requires us to print the elements of the matrix in a spiral order starting from the top left. I need some pointers on how to improve my code:

#include <stdio.h>

//function that prints a row from startx,starty to endy
void printRow(int arr[][4],int startx,int starty,int endy)
{
    int yCtr;
    for(yCtr = starty; yCtr <= endy ; yCtr ++)
       printf("%d ",arr[startx][yCtr]);
}

//function that prints a row from startx,starty to endy (decreasing columns)
void printRowBackward(int arr[][4],int startx,int starty,int endy)
{
    int yCtr;
    for(yCtr = starty; yCtr >= endy ; yCtr --)
       printf("%d ",arr[startx][yCtr]);
}

//function that prints a column from startx,starty to endx
void printColumnBackward(int arr[][4],int startx,int starty,int endx)
{
   int xCtr;
    for(xCtr = startx; xCtr >= endx; xCtr --)
       printf("%d ",arr[xCtr][starty]);
}

// column backwards
void printColumn(int arr[][4],int startx,int starty,int endx)
{
    int xCtr;
    for(xCtr = startx; xCtr <= endx; xCtr ++)
       printf(" %d ",arr[xCtr][starty]);
}

// prints a section of the spiral
void printSpiralSection(int arr[][4],int startx,int starty,int size)
{
   printRow(arr,startx,starty,size - 1);
   printColumn(arr,startx + 1 ,size - 1 ,size -1);
   printRowBackward(arr,size - 1,size - 2,starty);
   printColumnBackward(arr,size - 2,starty,startx + 1);
}

int main()
{
    int array[4][4] = { 22 ,323,2342,222,
                          2,234,243,333,
                          21,13,23,444,
                          223,234,231,234};

    int startx =0, starty = 0, size = 4;
    // prints each section of the spiral ... 
    while(size >=1) {
       printSpiralSection(array,startx,starty,size);
       size --;
       startx++;
       starty++;
    }
    getchar();
    return 0;
}
\$\endgroup\$
2
\$\begingroup\$

I would test it with a bigger array. Since I created one to test it, I might as well post it here. I put the numbers in the order I expected them to be printed, which makes checking easier.

const int array[][ARR_SIZE] = {
    { 1,   2,  3,  4,  5,  6,  7,  8,  9 },
    { 32, 33, 34, 35, 36, 37, 38, 39, 10 },
    { 31, 56, 57, 58, 59, 60, 61, 40, 11 },
    { 30, 55, 72, 73, 74, 75, 62, 41, 12 },
    { 29, 54, 71, 80, 81, 76, 63, 42, 13 },
    { 28, 53, 70, 79, 78, 77, 64, 43, 14 },
    { 27, 52, 69, 68, 67, 66, 65, 44, 15 },
    { 26, 51, 50, 49, 48, 47, 46, 45, 16 },
    { 25, 24, 23, 22, 21, 20, 19, 18, 17 },
};

Define ARR_SIZE at the top to 9 and replace each explicit 4 (apart from those in the array of course) with ARR_SIZE. Make each function static and each array parameter const.

Having done this I found that it really does work :-)


Each of your for-loops would be better written:

for (int y = starty; y <= endy; y ++) {
   printf("%d ", arr[startx][y]);
}

Notice that I defind the loop variable within the loop and called it y instead of yCtr (the Ctr was just noise) and added some spaces. Note that it is normal to put a space after a comma (applies everywhere in your code - your use of spaces is occasionally inconsistent). I prefer start_x to startx and print_row to printRow, but that it just personal preference.

Also, a minor point, as the functions are named printRow and printColumn etc, it might be more natural to express the parameters in terms of rows and columns instead of x and y:

static void printRow(const int arr[][ARR_SIZE], int row, int col, int end_col)
{
    for (; col <= end_col ; ++col) {
       printf("%d ", arr[row][col]);
    }
}

The main loop would normally be expressed as a for-loop

int startx = 0;
int starty = 0;
for (int size = ARR_SIZE; size >= 1; --size) {...

Note that I defined the two start variables on separate line - again this is considered best practice.

Final point, your comments are mostly noise.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks. Whats the benefit of making the functions static ? \$\endgroup\$ – Samhan Salahuddin Jun 30 '13 at 15:38
  • \$\begingroup\$ None at all in a single-file program. It is just best to get into the habit of making all local functions (those that are not used outside of the file) static. For bigger programs spanning many files, static functions offer better encapsulation and more chance for compiler optimisation. \$\endgroup\$ – William Morris Jun 30 '13 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.