15,904,023 members
See more:
given an input single dimensional array say a={1,2,3,4,5,6,7,8,9}
order=3;
then output should be in 2d array as
1 2 3
8 9 4
7 6 5
and if input array is{1,2,3,4,5,6}
order=3;
remaining elements in output array should be padded with 0's
1 2 3
0 0 4
0 6 5

give me little idea to enter the elements in specified order..
Posted

## Solution 3

This smells a bit like home-work but I am going to give you the benefit of the doubt on that one.

You can "walk" the target array and "turn right" whenever you end up outside of `order` by `order` dimensions or hit an already filled position.

Something like this might work;
C
```#include <stdio.h>
#include <stdlib.h>

void print(int* array, int order) {
int row;
int col;
for (row = 0; row < order; ++row) {
for (col = 0; col < order; ++col) {
printf("%i ", array[row * order + col]);
}
printf("\r\n");
}
}

void spiral_fill(int* source, int *target, int count, int order) {
int i;
int v;
int index = 0;
int x = 0;
int y = 0;
int tx = 0;
int ty = 0;
int dx = 1;
int dy = 0;

for (i = 0; i < order * order; ++i)
target[i] = INT_MIN; // Assuming that INT_MIN is never valid/present in source

for (i = 0; i < order * order; ++i) {
target[y * order + x] = i < count ? source[i] : 0;
// Create new temp position for target
tx = x + dx;
ty = y + dy;

// Check if moving in dx,dy ends up outside or on already covered position
// and if so, adjust dx, dy to make a "right turn"
if (dx == 1 && (tx >= order || target[ty * order + tx] != INT_MIN)) {
// Moving right and hit something
dx = 0;
dy = 1;
}
else {
if (dx == -1 && (tx < 0 || target[ty * order + tx] != INT_MIN)) {
// Moving left and hit something
dx = 0;
dy = -1;
}
else {
if (dy == 1 && (ty >= order || target[ty * order + tx] != INT_MIN)) {
// Moving down and hit something
dx = -1;
dy = 0;
}
else {
if (dy == -1 && (ty < 0 || target[ty * order + tx] != INT_MIN)) {
// Moving up and hit something
dx = 1;
dy = 0;
}
}
}
}

// At this point dx, dy are always correct, apply them to the x, y
x += dx;
y += dy;
}

}

int main() {

int i;
int count = 9; // count of items in source
int order = 3;
int* source = (int*)malloc(sizeof(int) * count);
int* target = (int*)malloc(sizeof(int) * order * order);

// Fill the source
for (i = 0; i < count; ++i)
source[i] = i + 1;

printf("Source:\r\n");
print(source, order);

spiral_fill(source, target, count, order);
printf("Target:\r\n");
print(target, order);

return 0;
}```

Hope this helps,
Fredrik

moyna coder 17-Dec-14 7:32am
ya thankz it will be helpful
Fredrik Bornander 17-Dec-14 7:46am

## Solution 2

You can use method like this:
C++
`int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};`

it will be like this
1 2 3
4 5 6
7 8 9

You can also use it like this:
C++
`int a[3][3]={{1},{4,5},{7,8,9}};`

it will return
1 0 0
4 5 0
7 8 9

v2
moyna coder 17-Dec-14 7:31am
yeah thankz..dis will be useful
Member 10641779 18-Dec-14 2:43am
U r Welcome :)

## Solution 1

The first bit is trivial: and since it's your homework, I'll let you do it yourself!
All you need to do is set up an index into the 1D array, and two loops - trivial.

The second is not possible.
Think about it: if you have this data
`{1,2,3,4,5,6}`
Should that be:
```1 2 3
0 0 4
0 6 5```
Or
```1 2 3
0 0 4
0 5 6```
Or
```1 2 3
0 4 5
0 0 6```
Or ... there are loads of other possible interpretations, and without defined rules and conditions no-one can tell which it should be.