I'm trying to solve an exercise that requires:
- fill randomly a 3x3 two-dimensional array
- transform the array in a second one with dimension 6x6:
1 2 3 1 2 3 3 2 1
4 5 6 -> 4 5 6 6 5 4
7 8 9 7 8 9 9 8 7
7 8 9 9 8 7
4 5 6 6 5 4
1 2 3 3 2 1
I can't get it working tho' I think the logic must be right.
#include <stdio.h>
#include <stdlib.h>
#define DIM 3
int main()
{
int i, j, a[DIM][DIM],a1[DIM][DIM], a2[DIM][DIM], a3[DIM][DIM], b[2*DIM][2*DIM];
srand(time(NULL));
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
a[i][j] = rand() % 10;
}
}
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
a1[i][j] = a[i][DIM - 1 - j];
a2[i][j] = a[DIM - 1 -j][j];
a3[i][j] = a2[i][DIM - 1 - j];
if(i < DIM && j < DIM)
b[i][j] = a[i][j];
if(i < DIM && j >= DIM)
b[i][j] = a1[i][j];
if(i >= DIM && j < DIM)
b[i][j] = a2[i][j];
if(i >= DIM && j >= DIM)
b[i][j] = a3[i][j];
}
}
for (i = 0; i < 2*DIM; i++)
{
for (j = 0; j < 2*DIM; j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
return 0;
}