//code sketch
row_len=R;
col_len=C;
r_start=0,col_start=0;
while (x < R*C)
{
for i=r_start to col_len
keep extracting value from 1D and add it to mat[r_start][i]=arr[p++];
r_start++;
for i=r_start to row_len
keep extracting value from 1D and add it to mat[i][col_len]=arr[p++];
col_len--;
for( i=col_len;i>=col_start;i--)
keep extracting value from 1D and add it to mat[row_len][i]=arr[p++];
row_len--;
for( i=row_len ; i >=r_start;i--)
keep extracting value from 1D and add it to
mat[i][col_start]=arr[p++];
col_start++;
}
keep on running above 4 loops till R*C times .
note : take care of 1D array bound , if all values are consumed then fill
with zero , add this checking in every loop.
On Thu, Apr 18, 2013 at 12:34 PM, w.s miller <[email protected]
> wrote:
> given a 1D array.The task is to convert it in to a 2D array and values
> should be filled spirally while filling from 1D array
>
> the size of 1D array is multiple of a constant say n.
> the number of rows and columns of 2D array will be given.
>
> say number of rows =R
> say number of columns = C
>
> k*n <= R*C. where k*n =number of elements in 1D array
> if (R*C > number of elements in 1D array)
> then rest of the values will be zeros.
> e.g.
>
> n=5; k=3
> R= 6
> C= 3
>
> input 1D array=[1,0,0,0,1,0,0,0,0,0,1,1,1,1,0]
>
> output 2D array
>
> 1 0 0 0 1 0
> 1 0 0 0 0 0
> 1 1 1 0 0 0
>
> here as 5*3<6*3 so ..18 -15 = 3
>
> i.e 3 remaining values are filled as zeros.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.