13,086,662 members (56,412 online)
Rate this:
See more:
Do I have to use advance loops? I thought about it for quite a while but could not figure it out. Example:

```int numbs[4][4] =
[1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16];```

When i print it, it should print like this.

1 2 3 4, then 8, 12,16, 15,14,13,9,5,67,11,10, (ie clockwise direction).

The function should be generic for any size matrix. Any help will be appreciated.
Posted 15-Aug-09 8:59am
sharp_k646
Updated 25-Aug-09 10:19am
v2

Rate this:

## Solution 1

Consider this as a rough idea:

```#include <stdio.h>

void PrintArrayClockwise(int *array, int rows, int columns)
{
int r = 0;
int rh = rows / 2;
int i;
while (r < rh)
{
for (i=r; i<(columns - r); i++) printf("%d ",array[(r*columns)+i]);
printf("\n");
for (i=r+2; i<=(rows-r); i++) printf("%d ",array[(i*columns)-1-r]);
printf("\n");
for (i=(columns-r-2); i>=r; i--) printf("%d ",array[((rows-r-1)*columns)+i]);
printf("\n");
for (i=rows-r-2; i>r; i--) printf("%d ",array[(i*columns)+r]);
printf("\n");
r++;
}
}

int main()
{
int numbs[16];
int i;
for (i=0; i<16; i++) numbs[i]=i+1;

PrintArrayClockwise(numbs, 4, 4);

return 0;
}```

It should work with arrays of different sizes, both square and not. I didn't check it thoroughfully but it should be a good starting point. :)

v2
Niklas Lindquist 3-Jun-10 17:12pm

That sounded like a homework to me

Top Experts
Last 24hrsThis month
 Jochen Arndt 190 Graeme_Grant 180 RyanDev 140 Karthik Bangalore 140 TheRealSteveJudge 95
 OriginalGriff 3,105 Graeme_Grant 1,529 ProgramFOX 1,367 Jochen Arndt 1,270 ppolymorphe 1,227