How to print an a 4x4 array in clockwise direction
-
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.
-
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.
without actually writing the code, i would think something like this would work: xMin = 0, xMax = arrayWidth yMin = 0, yMax = arrayHeight printed_something = false do { loop across top edge from xMin to xMax, print the values increase yMin loop across right edge from yMin to yMax, print the values decrease xMax loop across bottom edge from xMax to xMin, print the values decrease yMax loop across left edge from yMax to yMin, print the values increase xMin } while (printed_something)
-
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.
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. :)
2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
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. :)
2+2=5 for very large amounts of 2 (always loved that one hehe!)
This works perfectly! I have not yet understood how it works though but will think on it. I was thinking on the idea to print the top row of the matrix then rotate the matric anticlockwise while removing the top row and printing the next top row and so on. But then I needed a function to rotate the matrix anticlockwise, which was not easy to implement when you consider you have to eliminate the top row as well. Your code is much simpler and very neat. I still have to understand it fully though. What do you think about my idea. Can you explain a little bit of your code. Thanks a lot for sharing :)
-
This works perfectly! I have not yet understood how it works though but will think on it. I was thinking on the idea to print the top row of the matrix then rotate the matric anticlockwise while removing the top row and printing the next top row and so on. But then I needed a function to rotate the matrix anticlockwise, which was not easy to implement when you consider you have to eliminate the top row as well. Your code is much simpler and very neat. I still have to understand it fully though. What do you think about my idea. Can you explain a little bit of your code. Thanks a lot for sharing :)
Happy to be of help. :) About rotating the matrix, it would take more time and more space, so I think it's not a good solution. Yeah I know that my code wasn't that readable, C pointer math can be hellish... It works by using a single iterator on rows
r
. This iterator goes from the first row (0) to the middle row ((rows/2)-1). First I print the row pointed by r:for (i=r; i<(columns - r); i++) printf("%d ",array[(r*columns)+i]);
then I proceed printing the rightmost column, starting from the last item I printed for row
r
:for (i=r+2; i<=(rows-r); i++) printf("%d ",array[(i*columns)-1-r]);
I continue with the lowest row, which is printed right to left:
for (i=(columns-r-2); i>=r; i--) printf("%d ",array[((rows-r-1)*columns)+i]);
and finally with the first column, bottom to top:
for (i=rows-r-2; i>r; i--) printf("%d ",array[(i*columns)+r]);
With each iteration
r
grows by one. You will see that it's used in all thefor
loops, in order to move them towards the centre of the matrix one row and one column each. Hope it's more clear now, if you think it would help I can rewrite this using distinct row and column indexes on the array, in order to make it clearer.2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
Happy to be of help. :) About rotating the matrix, it would take more time and more space, so I think it's not a good solution. Yeah I know that my code wasn't that readable, C pointer math can be hellish... It works by using a single iterator on rows
r
. This iterator goes from the first row (0) to the middle row ((rows/2)-1). First I print the row pointed by r:for (i=r; i<(columns - r); i++) printf("%d ",array[(r*columns)+i]);
then I proceed printing the rightmost column, starting from the last item I printed for row
r
:for (i=r+2; i<=(rows-r); i++) printf("%d ",array[(i*columns)-1-r]);
I continue with the lowest row, which is printed right to left:
for (i=(columns-r-2); i>=r; i--) printf("%d ",array[((rows-r-1)*columns)+i]);
and finally with the first column, bottom to top:
for (i=rows-r-2; i>r; i--) printf("%d ",array[(i*columns)+r]);
With each iteration
r
grows by one. You will see that it's used in all thefor
loops, in order to move them towards the centre of the matrix one row and one column each. Hope it's more clear now, if you think it would help I can rewrite this using distinct row and column indexes on the array, in order to make it clearer.2+2=5 for very large amounts of 2 (always loved that one hehe!)
Thanks for explaining. I got the idea. This is a beautiful logic but it also requires some arithmetic and pointer skills :) If you could implement it in general arrow notation, that would be great. But of course not required :) You are absolutely right, the rotate array matrix approach would be too much waste of time and computationally expensive. I think this is great logic. I would not have seen it unless it was implemented. Thanks again. I greatly appreciate your help :) BTW Brave for implementing it in such a short amount of time!
-
Thanks for explaining. I got the idea. This is a beautiful logic but it also requires some arithmetic and pointer skills :) If you could implement it in general arrow notation, that would be great. But of course not required :) You are absolutely right, the rotate array matrix approach would be too much waste of time and computationally expensive. I think this is great logic. I would not have seen it unless it was implemented. Thanks again. I greatly appreciate your help :) BTW Brave for implementing it in such a short amount of time!
No problem, here is the version with separated row and column indexes:
#include <stdio.h>
void PrintArrayClockwise(int array[][4], 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][i]);
printf("\n");
for (i=r+1; i<(rows-r); i++) printf("%d ",array[i][columns-r-1]);
printf("\n");
for (i=(columns-r-2); i>=r; i--) printf("%d ",array[rows-r-1][i]);
printf("\n");
for (i=rows-r-2; i>r; i--) printf("%d ",array[i][r]);
printf("\n");
r++;
}
}int main()
{
int numbs[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};PrintArrayClockwise(numbs, 4, 4); return 0;
}
Unluckily there's no way of passing a bydimensional array to a function without indicating the second dimension, at least none that I know of (but my ansi C is VERY rusty hehe). It sure can be done with
int**
anyway. :)2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
No problem, here is the version with separated row and column indexes:
#include <stdio.h>
void PrintArrayClockwise(int array[][4], 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][i]);
printf("\n");
for (i=r+1; i<(rows-r); i++) printf("%d ",array[i][columns-r-1]);
printf("\n");
for (i=(columns-r-2); i>=r; i--) printf("%d ",array[rows-r-1][i]);
printf("\n");
for (i=rows-r-2; i>r; i--) printf("%d ",array[i][r]);
printf("\n");
r++;
}
}int main()
{
int numbs[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};PrintArrayClockwise(numbs, 4, 4); return 0;
}
Unluckily there's no way of passing a bydimensional array to a function without indicating the second dimension, at least none that I know of (but my ansi C is VERY rusty hehe). It sure can be done with
int**
anyway. :)2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
Thanks again! I appreciate it. I am definitely a fan of your programming skills. How much of experience do you have in programming btw. You have personnel contact? :)
Huh well thanks, you are definitely overvauling me hehe. :) I'm into programming ever since I was 11 (started with a Philips game console which could be programmed in a kind of pseudo-assembler, then a year later I passed to a C64 and so on - typical for my generation), and have been working as a software engineer ever since 1989. Quite some time I'd say. :P Don't get me wrong, but I won't be giving my personal contact in a forum. I've nothing against you in particular, but it's simply not good practice! If you ever need any more help post here. If I'll be able to help I'll gladly do it, or someone else will. Besides, forums are good also for other people, who can learn by reading the answers to your questions.
2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
Huh well thanks, you are definitely overvauling me hehe. :) I'm into programming ever since I was 11 (started with a Philips game console which could be programmed in a kind of pseudo-assembler, then a year later I passed to a C64 and so on - typical for my generation), and have been working as a software engineer ever since 1989. Quite some time I'd say. :P Don't get me wrong, but I won't be giving my personal contact in a forum. I've nothing against you in particular, but it's simply not good practice! If you ever need any more help post here. If I'll be able to help I'll gladly do it, or someone else will. Besides, forums are good also for other people, who can learn by reading the answers to your questions.
2+2=5 for very large amounts of 2 (always loved that one hehe!)
Good to know a little bit about you. I actually looked at your webpage on codeproject but there was not much there except that you are from Italy :-O Well thanks for the little background. Codeproject is my resource for technical questions so I will definitely post here if i need any help. Thanks again.