#include <stdio.h>
#include <stdlib.h>
void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen);
void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen);
int main(void)
{
int a[5][4] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
{17,18,19,20}
};
print_layer_top_right(a,0,0,3,4);
}
//
// prints the top and right shells of the matrix
//
void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen)
{
int i = 0, j = 0;
// print the row
for(i = col; i<=collen; i++)
{
}
//print the column
for(j = row + 1; j <= rowlen; j++)
{
}
// see if we have more cells left
if(collen-col > 0)
{
// 'recursively' call the function to print the bottom-left layer
print_layer_bottom_left(a, col, row + 1, collen-1, rowlen);
}
}
//
// prints the bottom and left shells of the matrix
void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen)
{
int i = 0, j = 0;
//print the row of the matrix in reverse
for(i = collen; i>=col; i--)
{
}
// print the last column of the matrix in reverse
for(j = rowlen - 1; j >= row; j--)
{
}
if(collen-col > 0)
{
// 'recursively' call the function to print the top-right layer
print_layer_top_right(a, col+1, row, collen, rowlen-1);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4Kdm9pZCBwcmludF9sYXllcl90b3BfcmlnaHQoaW50IGFbXVs0XSwgaW50IGNvbCwgaW50IHJvdywgaW50IGNvbGxlbiwgaW50IHJvd2xlbik7CnZvaWQgcHJpbnRfbGF5ZXJfYm90dG9tX2xlZnQoaW50IGFbXVs0XSwgaW50IGNvbCwgaW50IHJvdywgaW50IGNvbGxlbiwgaW50IHJvd2xlbik7CgppbnQgbWFpbih2b2lkKQp7CmludCBhWzVdWzRdID0gewp7MSwyLDMsNH0sCns1LDYsNyw4fSwKezksMTAsMTEsMTJ9LAp7MTMsMTQsMTUsMTZ9LAp7MTcsMTgsMTksMjB9Cn07CgpwcmludF9sYXllcl90b3BfcmlnaHQoYSwwLDAsMyw0KTsKfQoKLy8KL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